3:53 pm - Monday March 24, 4892

Hibernate Interview Questions and Answers Part 5

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

1. What is a Session Factory? Is it a thread-safe object?

SessionFactory is Hibernates concept of a single data store and is threadsafe so that many threads can access it concurrently and request for sessions and the immutable cache of compiled mappings for a single database. A SessionFactory is usually only built once at startup. SessionFactory should be wrapped in some kind of singleton so that it can be easily accessed in application code.

SessionFactory sessionFactory = new Configuration().configure().buildSessionfactory();

2. What is a Session? Can you share a session object between different thread?

A session is light weight and a non-threadsafe object (No, you cannot share it between threads) that represents a single unit-of-work with the database. Sessions are opened by a SessionFactory and then are closed when all work is complete. A session is the primary interface for the persistence service. A session obtains a database connection lazily (i.e. only when required). To avoid creating too many sessions ThreadLocal class can be used as shown below to get the current session no matter how many times you make call to the currentSession() method.


public class HibernateUtil {


public static final ThreadLocal local = new ThreadLocal();

public static Session currentSession() throws HibernateException {

Session session = (Session) local.get();

//open a new session if this thread has no session

if(session == null) {

session = sessionFactory.openSession();



return session;



It is also vital that you close your session after your unit of work completes.

Note: Keep your Hibernate Session API handy.

3. What are the benefits of detached objects?

Detached objects can be passed across layers all the way up to the presentation layer without having to use any DTOs (Data Transfer Objects). You can, later on, re-attach the detached objects to another session.

4. How does Hibernate distinguish between transient(i.e. newly instantiated ) and detached object?

Hibernate uses the version property if there is one.

If not uses the identifier value. No identifier value means a new object. This does work only for Hibernate managed surrogate keys. Does not work for natural keys and assigned (i.e. not managed by Hibernate) surrogate keys.

” Write your own strategy with Interceptor.isUnsaved().

5. What are the pros and cons of detached objects?


When long transactions are required due to user think-time, it is the best practice to break the long transaction up into two or more transactions. You can use detached objects from the first transaction to carry data all the way up to the presentation layer. These detached objects get modified outside a transaction and later on re-attached to a new transaction via another session.


In general, working with detached objects is quite cumbersome, and better to not clutter up the session with them if possible. It is better to discard them and re-fetch them on subsequent requests. This approach is not only more portable but also more efficient because – the objects hang around in Hibernate’s cache anyway.

Also from pure rich domain driven design perspective, it is recommended to use DTOs (DataTransferObjects) and DOs (DomainObjects) to maintain the separation between Service and UI tiers.

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

  • You must have a default no-argument constructor for your persistent classes and there should get XXX() (i.e accessor/getter) and set XXX( i.e. mutator/setter) methods for all your persistable instance variables.
  • 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.
  • It is recommended to implement the Serializable interface. This is potentially useful if you want to migrate around a multi-processor cluster.
  • The persistent class should not be final because if it is final then lazy loading cannot be used by creating proxy objects.
  • Use XDoclet tags for generating your *.hbm.xml files or Annotations (JDK 1.5 onwards), which are less verbose than *.hbm.xml files.

7. Explain about the transparent persistence of Hibernate?

Transparent persistence is provided for Plain old Java objects or POJOs. For proper functioning of the importance of the application should be given to the methods equals() and hashCode() methods. It has a requirement which should be strictly followed in the applications which are a no-argument constructor.

8. Explain about the Primary feature of Hibernate?

The primary feature of hibernate is to Java classes to database tables. Data query and retrieval is also possible with Hibernate. Application portability is a key feature in Hibernate it allows developers to port applications to almost all SQL databases

9. Explain about transaction file?

Transactions denote a work file which can save changes made or revert back the changes. A transaction can be started by session.beginTransaction() and it uses JDBC connection, CORBA or JTA. When this session starts several transactions may occur.

10. What is the effect when a transient mapped object is passed onto a sessions save?

When a Sessions save() is passed to a transient mapped object it makes the method to become more persistent. Garbage collection and termination of the Java virtual machine stays as long as it is deleted explicitly. It may head back to its transient state.

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

Filed in: Z-A Interview Questions

No comments yet.

Leave a Reply