Search your topic in Java2share
Home > FAQs
eXTReMe Tracker

Hibernate FAQs

25 . What are the general considerations or best practices for defining your Hibernate persistent classes?

1.You must have a default no-argument constructor for your persistent classes and there should be getXXX()and setXXX() methods for all your persistable instance variables.

2.You should implement the equals() and hashCode() methods based on your business key and it is important not to use the id field in your equals() and hashCode() definition if the id field is a surrogate key (i.e. Hibernate managed identifier). This is because the Hibernate only generates and sets the field when saving the object.

3. It is recommended to implement the Serializable interface. This is potentially useful if you want to migrate around a multi-processor cluster.

4.The persistent class should not be final because if it is final then lazy loading cannot be used by creating proxy objects.

26 . Difference between session.update() and session.lock() in Hibernate ?

The session.update method is used to update the persistence object in the in the database.
 The session.lock() method simply reattaches the object to the session without checking or updating the database on the assumption that the database in sync with the detached object. It is the best practice to use either session.update(..) or session.saveOrUpdate(). Use session.lock() only if you are absolutely sure that the detached object is in sync with your detached object or if it does not matter because you will be overwriting all the columns that would have changed later on within the same transaction.

27.What are the Collection types in Hibernate ?

  • Set
  • List
  • Array
  • Map
  • Bag

28.What is the difference between sorted and ordered collection in hibernate?
sorted collection vs. order collection :-

sorted collection 

order collection 

A sorted collection is sorting a collection by utilizing the sorting features provided by the Java collections framework. The sorting occurs in the memory of JVM which running Hibernate, after the data being read from database using java comparator. 

Order collection is sorting a collection by specifying the order-by clause for sorting this collection when retrieval. 

If your collection is not large, it will be more efficient way to sort it. 

If your collection is very large, it will be more efficient way to sort it . 

29.What are the ways to express joins in HQL?
HQL provides four ways of expressing (inner and outer) joins:-

  • An implicit association join
  • An ordinary join in the FROM clause
  • A fetch join in the FROM clause.
  • A theta-style join in the WHERE clause.

30.What do you mean by Named – SQL query?

Named SQL queries are defined in the mapping xml document and called wherever required.
<sql-query name = "empdetails">
   <return alias="emp" class="com.durgasoft.Employee"/>
      SELECT emp.EMP_ID AS {emp.empid},
                 emp.EMP_ADDRESS AS {emp.address},
                 emp.EMP_NAME AS {}
      FROM Employee EMP WHERE emp.NAME LIKE :name

Invoke Named Query :
List people = session.getNamedQuery("empdetails")
                     .setString("TomBrady", name)