Previous Page
Hibernate FAQs
Next Page


13.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


14.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.

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

  • First we need to write Java domain objects (beans with setter and getter). The variables should be same as database columns.
  • 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>

 

16.What’s the difference between load() and get()?
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. 


17.What is the difference between 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.

18.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>

 

 


Previous Page
Next Page