This section focuses on the interview questions and answers in the Hibernate stream. These hibernate questions will improve your knowledge about why certain things are done in the hibernate world.
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?
This represents hibernate session which perform the manipulation on the database entities. Some of the activities performed by session interface are as follows they are managing the persistence state, fetching persisted ones and management of the transaction demarcation.
What is the Steps involved in creating database applications with Java using Hibernate ?
Creating Database applications with Java is made simpler with Hibernate. First Plain old java object needs to be written, XML mapping file should be created which shows relationship between database and class attributes. Hibernate APIs can be used to store persistent objects.
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
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
<set name=”address” inverse=”true” cascade=”delete” lazy=”false”>
<key column=”a_id” />
<one-to-many class=”beans Address”/>
What is the dirty checking feature of Hibernate ?
Dirty checking feature of the Hibernate allows users or developers to avoid time consuming data base write actions. This feature makes necessary updations and changes to the fields which require a change, remaining fields are left unchanged or untouched.