2:03 am - Saturday January 21, 2017

Hibernate Interview Question

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

Hibernate Interview Question

 

1.            What is the general flow of Hibernate communication with RDBMS?

The general flow of Hibernate communication with RDBMS is :

  •              Load the Hibernate configuration file and create configuration object. It will automatically load all hbm mapping files
  •             Create session factory from configuration object
  •             Get one session from this session factory
  •             Create HQL Query
  •             Execute query to get list containing Java objects

2.            What is Hibernate Query Language (HQL)

Hibernate offers a query language that embodies a very powerful and flexible mechanism to query, store, update, and retrieve objects from a database. This language, the Hibernate query Language (HQL), is an object-oriented extension to SQL.

 

3.            How do you map Java Objects with Database tables?

  •             First we need to write Java domain objects (beans with setter and getter).
  •              Write hbm.xml, where we map java class to table and database columns to Java class variables.

Example :

 


<hibernate-mapping>

<class name="com.test.User"  table="user">

<property  column="USER_NAME" length="255"

name="userName" not-null="true"  type="java.lang.String"/>

<property  column="USER_PASSWORD" length="255"

name="userPassword" not-null="true"  type="java.lang.String"/>

</class>

</hibernate-mapping>

4.            What’s load() vs. get() :-

load()

get()

Only use the load() method if you are sure that the object exists. 

 

If you are not sure that the object exists, then use one of the get()methods.
load() method will throw an exception if the unique id is not found in the database. 

 

get() method will return null if the unique id is not found in the database.
load() just returns a proxy by default and database won’t be hit until the proxy is first invoked

 

get() will hit the database immediately.

Hibernate offers a query language that embodies a very powerful and flexible mechanism to query, store, update, and retrieve objects from a database. This language, the Hibernate query Language (HQL), is an object-oriented extension to SQL.

5.            What is the difference between and merge and update ?

Use update() if you are sure that the session does not contain an already persistent instance with the same identifier, and merge() if you want to merge your modifications at any time without consideration of the state of the session.

6.            How do you define sequence generated primary key in hibernate?

Using <generator> tag.

 

Example:-

<id column=”USER_ID” name=”id” type=”java.lang.Long”>

<generator class=”sequence”>

<param name=”table”>SEQUENCE_NAME</param>

<generator>

</id>

7.            Define cascade and inverse option in one-many mapping?

cascade – enable operations to cascade to child entities.

cascade=”all|none|save-update|delete|all-delete-orphan”

 

inverse – mark this collection as the “inverse” end of a bidirectional association.

inverse=”true|false”

Essentially “inverse” indicates which end of a relationship should be ignored, so when persisting a parent who has a collection of children, should you ask the parent for its list of children, or ask the children who the parents are?

8.            What do you mean by Named-SQL query?

Named SQL queries are defined in the mapping xml document and called wherever required.

Example:

<sql-query name = “empdetails”>

<return alias=”emp”/>

SELECT emp.EMP_ID AS {emp.empid},

emp.EMP_ADDRESS AS {emp.address},

emp.EMP_NAME AS {emp.name}

FROM Employee EMP WHERE emp.NAME  LIKE :name

</sql-query>

Invoke Named Query :

List people = session.getNamedQuery(“empdetails”)

.setString(“TomBrady”, name)

.setMaxResults(50)

.list();

9.            How do you invoke Stored Procedures?

<sql-query name=”selectAllEmployees_SP” callable=”true”>

<return alias=”emp”>

<return-property name=”empid” column=”EMP_ID”/>

<return-property name=”name” column=”EMP_NAME”/>

<return-property name=”address” column=”EMP_ADDRESS”/>

{ ? = call selectAllEmployees() }

</return>

</sql-query>

10.          Explain Criteria API

Criteria is a simplified API for retrieving entities by composing Criterion objects. This is a very convenient approach for functionality like “search” screens where there is a variable number of conditions to be placed upon the result set.

Example :

List employees = session.createCriteria(Employee.class)

.add(Restrictions.like(“name”, “a%”) )

.add(Restrictions.like(“address”, “Boston”))

.addOrder(Order.asc(“name”) )

.list();

 

1 2 3 4 5 6 7 8 9 10 11 12


Filed in: Z-A Interview Questions

No comments yet.

Leave a Reply