Hibernate Interview Questions

Hibernate Interview Questions

What is the Session factory interface ?

It creates new hibernate sessions by referencing immutable and thread safe objects. Application using hibernate are usually allowed and desgined to implement single instance of the class using this interface. Only single instance of a class can be used which is using this interface.

What is the session interface?


What is the Steps involved in creating database applications with Java using Hibernate ?

Explain how you can configure hibernate.cfg.xml ?
Hibernate can be configured with two types of files out of which hibernate.cfg.xml is widely used and popular feature. Hibernate consults hibernate.cfg.xml file for its operating properties such as database dialect, connection string and mapping files. These files are searched on class path.

Can a single Hibernate Session object be used across multiple threads ?

No, Hibernate Session is basically single-threaded and not to be used across
multiple threads.

No, Hibernate SessionFactory is basically thread-safe, thus can be re-used
across multiple threads and multiple Transacions as well.

What is session.save() : Save does an insert and will fail if the primary key is already persistent.

What is session.saveOrUpdate() : saveOrUpdate does a select first to determine if it needs to do an insert or an update. Insert data if primary key not exist otherwise update data.

What is session.persist() : Does the same like session.save().
But session.save() return Serializable object but session.persist() return void.

What is lazy loading in hibernate and how is it done ?

Lazy fetching decides whether to load child objects while loading the Parent Object.
You need to do this setting respective hibernate mapping file of the parent class.
Lazy = true (means not to load child)
By default the lazy loading of the child objects is true. This make sure that the child objects are not loaded unless they are explicitly invoked in the application by calling getChild() method on parent.In this case hibernate issues a fresh database call to load the child when getChild() is actully called on the Parent object.But in some cases you do need to load the child objects when parent is loaded.
Just make the lazy=false and hibernate will load the child when parent is loaded from the database.

In the Employee.hbm.xml file

What is the dirty checking feature of Hibernate ?
