[2021] Most Important Hibernate 5 Interview Questions
Table of Contents
Difference between save() and saveOrUpdate() methods ?
save() method saves the data only when the primary key is unique else it fails to insert the record/data to the table, since the same primary key already exists in the table.
saveOrUpdate() method saves the data if primary key is unique/updates it when already same primary key exists in the table.
What is dirty checking ?
Inorder to reduce the database write action hibernate has dirty checking feature, which updates only the changes required fields.
Difference between session get() and load() ?
get() vs load() with Example
1. session.load() will return the hibernate proxy object, but session.get() will return real object.
2. session.load() will not hit the database, but session.get() will hit the database all the time.
3. session.load() will throw object not found exception when no row found, but session.get() will return null when no row found in db.
Prefer to use load() if you sure about the data existence.
Not sure about the data existence then use session.get().
How to create a session factory ?
Session factory can be created using the below snippet. but knowing how to create it alone does not work. Personally when I attended Java backend developer interview in infosys, they asked me to write the below snippet.
Detailed explanations on what is session factory and how to create it can be found here.
import org.hibernate.HibernateException; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; public class HibernateConfig { private static SessionFactory sessionFactory; // Uncomment this if you are planning to use way 2 to create session factory private static ServiceRegistry serviceRegistry; static { try { Configuration configuration = new Configuration().configure(); // way 1 // deprecated in 4/4.3+ // sessionFactory = configuration.buildSessionFactory(); // way 2 recommended for 4.1.8 to 4.3 // serviceRegistry = new // ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServic//eRegistry(); // sessionFactory = // configuration.buildSessionFactory(serviceRegistry); // way 3 - recommended for 4.3.6+ StandardServiceRegistryBuilder standardSrvcRgstryBuilder = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()); sessionFactory = configuration.buildSessionFactory(standardSrvcRgstryBuilder.build()); } catch (HibernateException e) { System.out.println("Problem while creating a sessionfactory!"); } } public static SessionFactory getSessionFactory() { return sessionFactory; }}}catch( HibernateException e) { System.out.println("Problem while creating a sessionfactory!"); } } public static SessionFactory getSessionFactory() { return sessionFactory; }
Difference between session and sessionfactory ?
Basically session will be created from the sessionfactory instances.
Sessionfactory:
It is one instance per datasource/database.
It is thread safe.
It is a heavy weight object, because it maintains datasources, mappings, hibernate configuration information’s etc.
Sessionfactory will create and manage the sessions.
If you have 5 datasources/databases, then you must create 5 session factory instances.
session:
It is one instance per client/thread/one transaction.
It is not thread safe.
It is light weight.
sessions will be opened using sessionfactory.openSession() and some database operations will be done finally session will be closed using session.close().
session vs stateless session ?
Sessionfactory provides 3 methods namedly openSession(), getCurrentSession() and openStatelessSession().
openSession() – creates always the new session.
getCurrentSession() – hibernate.current_session_context_class property must be configured in the hibernate configuration file.
It returns the current session.
openStatelessSession() –