Apr 12

Difference between load() and get() in hibernate?

Difference between load() and get() in hibernate is one of the popular interview questions for both who attends telephonic as well as face to face interview. Here are list of differences if you find any more differences more than this you can post in comments section. I will include those valid comments in this tutorial.


Transaction tx = session.beginTransaction();Employee employee = (Employee) session.load(Employee.class, new Long(294912));

Employee employee1 = (Employee) session.get(Employee.class, new Long(294912));



                         get()                    load()
If object is present in the database it return object otherwise it returns  null. If object is present in the database it return object otherwise it returns  objectNotFoundException if object is not found.load() never returns null.
get() never returns proxy object. Proxy object means object which has id or primarykey property and remaining properties are uninitialized. load() always returns proxy object.
get() eagerly loads the data i.e always hit the database load() lazily loads data.load hits the database on demand i.e whenever we are calling getter methods on pojo other than id property,proxied object gets initialized.
Performance wise it is not recommended to use get because it always hits the database. Performance wise it is recommended to use load because load will hit the database on demand.
We have four overloaded methods for get() method. We have five overloaded methods .public void load(Object object, Serializable id) throws HibernateException

This is an extra method which takes object as argument.


When to use get() and load() in database?

Both get() and load() are used to retrieve records from database.

1)    Use get() only when we are sure about record is existed in database other wise it is better to use load() method.

2)    load() performs lazy loading performance it is recommended to use load() method rather than get() which hits database everytime record is requested.

Mar 06

Hibernate interview questions for experienced-part 2

    6)      How many xml files are required to develop hibernate application?

Two files are required :

  1. hibernate.cfg.xml (Configuration file)
  2. mappingfile.hbm.xml (Mapping file)


      7)      Explain How to configure database and connection settings programatically in Hibernate?

We use hibernate API configuration class to load the hbm files , specify drivers and dialect.

Configuration configuration = new Configuration()


.setProperty(“hibernate.dialect”, “org.hibernate.dialect.Oracle10gDialect”)

.setProperty(“hibernate.connection.driver_class”, “oracle.jdbc.driver.OracleDriver”)

.setProperty(“hibernate.connection.url”, ” “)

.setProperty(“hibernate.connection.username”, “test”)

.setProperty(“hibernate.connection.password”, “test”);

SessionFactory factory = configuration.buildSessionFactory();

We use addResource(“ “) to load the mapping file. We use setProperty() to specify dialect,driver class, connection url , username and password.

We can also use addClass() method in Configuration class instead of addResource() to load mapping file.


Configuration configuration = new Configuration()


.setProperty(“hibernate.dialect”, “org.hibernate.dialect.Oracle10gDialect”)

.setProperty(“hibernate.connection.driver_class”, “oracle.jdbc.driver.OracleDriver”)

.setProperty(“hibernate.connection.url”, ” “)

.setProperty(“hibernate.connection.username”, “test”)

.setProperty(“hibernate.connection.password”, “test”);

SessionFactory factory = configuration.buildSessionFactory();

When we use addClass() method Hibernate loads mapping definition file corresponding to this class.


8)      Explain How to configure database and connection settings using xml file in Hibernate?

This Configuration file should be placed in root of class path.


<?xml version=”1.0″ encoding=”UTF-8″?>

<!DOCTYPE hibernate-configuration PUBLIC

“-//Hibernate/Hibernate Configuration DTD 3.0//EN”




<property name=”connection.driver_class”>



<property name=”connection.url”> </property>

<property name=”connection.username”>test</property>

<property name=”connection.password”>test</property>

<property name=”dialect”>org.hibernate.dialect.Oracle10gDialect</property>

<mapping resource=”employee.hbm.xml” />



We use hibernate.cfg.xml to specify connection properties, connection pooling properties, caching properties , transaction properties and miscellaneous properties.

We can specify any name for this hibernate configuration file but it is better practice to name this file as hibernate.cfg.xml.


9)      How to load hibernate configuration file ?

Configuration cfg=new Configuration();


configure() method in Configuration class is used to load the default configuration file(hibernate.cfg.xml).

Configuration cfg = new Configuration();


We use configure overloaded method in Configuration class to load the userdefined configuration xml file (which is not hibernate.cfg.xml).

10)      What is Dialect in hibernate ?

Hibernate is a database independent framework. To generate sql queries specific to specific database Dialect is used. Different databases have different features such as datatypes, queries etc. Those features are specific to database. Hibernate abstracts all these features in to dialect classes. As of Hibernate 3.0 there are 20 dialect classes.

Here are some of dialects :

Oracle (any version) org.hibernate.dialect.OracleDialect
Oracle 10g org.hibernate.dialect.Oracle10gDialect
MySQL org.hibernate.dialect.MySQLDialect
PostgreSQL org.hibernate.dialect.PostgreSQLDialect
Microsoft SQL Server org.hibernate.dialect.SQLServerDialect
Mar 06

Hibernate interview questions 2013 | part-1

1)      What is persistence?

Persistence means saving the data to reuse at later point of time and the data should be same as when we saved.

There are two kinds of persistence:

  1. Files
  2. Relational Databases
  1. Files stored on disk or our computer is most common form of persistence.
  2. Relational databases is another popular form of persistence used in real world.

Almost all real word applications need persistent data. Persistence in java means storing data in relational databases.

2)      What are the problems with persistence using JDBC?

  1. JDBC based persistence logic is database dependent.SQL queries changes from database to database.
  2. Exceptional handling and transaction management is difficult in jdbc.
  3. In JDBC user is responsible to open and close the connection otherwise leads to memory errors.
  4. Working with relationships is difficult in jdbc.
  5. In jdbc we hard code the connection properties in java code.
  6. JDBC does not support cache mechanism by default.
  7. JDBC does not send the data in form of object , it allows to send the data in the form of text or values.
  8. In JDBC it is the developers responsibility to handle jdbc resultset and convert it to java code.
  9. In jdbc there is no versioning concept. Developer has to manually maintain versioning.
  10. In jdbc developer has to manually map java objects with tables and viceversa.

 3)      What is ORM?

ORM stands for Object relational mapping. It is the process of mapping java class with table and properties with columns and object data with row data is called orm.

4)      Features of ORM?

  1. Productivity :

ORM significantly reduces development time. ORM takes care of mapping java object with relational database.

  1. Maintainability :

ORM concentrates on business logic rather than unnecessary plumbing of code. In ORM frameworks it is easy to maintain when there are updations such as (Tablename,columnnames etc).

  1. Vendor independent :

ORM frameworks are vendor independent . ORM abstracts from sql queries. ORM supports different databases.

 5)      What is hibernate?

Hibernate is an open source framework for mapping objects to relational databases.

Started by Gavin King in 2001.

Developed by Redhat developers now part of JBOSS community.