Alphalearningschool
        

<<< Prev

Up
Hibernate Interview Question

  1. What is Hibernate?

It is an open source and lightweight ORM tool that is used to store, retrieve and manipulate data from a database. It is one of the most widely used ORM tools for Java applications.

  1. What is HQL?

Hibernate Query Language (HQL) is the same as Structure Query Language. It is known as an object-oriented query language. Here we use an object to store our database and it is independent of the database.

  1. What is ORM?

Object Relation Mapping (ORM). It is a programming strategy to map object with the data stored in the database. It simplifies data creation, data manipulation, and data access.

  1. Explain the advantages of Hibernate?

Some of the advantages of Hibernate are:

  • It provides Simple Querying of data.
  • An application server is not required to operate.
  • The complex associations of objects in the database can be manipulated.
  • Database access is minimized with smart fetching strategies.
  • It manages the mapping of Java classes to database tables without writing any code.
  • Properties of an XML file is changed in case of any required change in the database.
  1. What are the Key components/core interfaces of Hibernate?

The Key components/core interfaces of Hibernate framework are:

  • Session: It is used to get a physical network with a database.
  • Transaction: It represents the unit of work with a database.
  • Query: It uses SQL and HQL string to retrieve the data from the database and create objects.
  • Criteria: It is used to create and execute object-oriented queries and retrieve the objects.
  • Configuration: It represents the properties of files required by Hibernate
  • Session Factory: It configures hibernate for the application using the provided configuration file and instantiates the session object.

 

  1. What are the advantages of using ORM over JDBC?

ORM has the following advantages over JDBC:

  • Application development is fast.
  • Management of transaction.
  • Generates key automatically.
  • Details of SQL queries are hidden.

 

  1. Define criteria in terms of Hibernate.

Criteria objects are used to create and execute object-oriented Queries to retrieve the objects.

 

  1. Mention some of the databases that Hibernate supports?

Hibernate supports all the major RDMS. Following are the list of database engines supported by Hibernate:

  • HSQL Database Engine
  • DB2/NT
  • Oracle
  • Microsoft SQL Server Database
  • Sybase SQL Server
  • Informix Dynamic Server
  • MySQL
  • PostgreSQL
  • FrontBase
  1. Mention two components of Hibernate configuration object.
  • Database Connection
  • Class Mapping Setup
  1. How is SQL query created in Hibernate?

        Syntax of creating SQL query:  Session.createSQLQuery       

  1. How is the HQL query created?

        Syntax of creating HQL query:  Session.createQuery

  1. Define persistent classes.

Those classes objects are stored in a database table are called a persistent class.

  1. What is SessionFactory?

SessionFactory provides the instance of Session. It is a factory of Session. It holds the data of second level cache that is not enabled by default.

  1. Is SessionFactory a thread-safe object?

Yes, many threads cannot access it simultaneously.

  1. What is Session?

It maintains a connection between the hibernate application and database.

It provides methods to store, update, delete or fetch data from the database such as persist(), update(), delete(), load(), get() etc.

It is a factory of Query, Criteria and Transaction i.e. it provides factory methods to return these instances.

 

  1. Is Session a thread-safe object?

No, Session is not a thread-safe object; many threads can access it simultaneously. We can share it between threads.

  1. What are the states of the object in hibernate?

There are 3 states of the object (instance) in hibernate.

  1. Transient: The object is in a transient state if it is just created but has no primary key (identifier) and not associated with a session.
  2. Persistent: The object is in a persistent state if a session is open, and you just saved the instance in the database or retrieved the instance from the database.
  3. Detached: The object is in a detached state if a session is closed. After detached state, the object comes to persistent state if you call lock() or update() method.

 18. How to make an immutable class in hibernate?

By marking a class as mutable=”false”, it will be treated as an immutable class. By default, it is mutable=”true”.

  1. What is automatic dirty checking in hibernate?

The automatic dirty checking feature of Hibernate, calls update statement automatically on the objects that are modified in a transaction.

Example:

SessionFactory factory = cfg.buildSessionFactory();

Session session1 = factory.openSession();

Transaction tx=session2.beginTransaction();

Employee e1 = (Employee) session1.get(Employee.class, Integer.valueOf(100));

e1.setSalary(50000);

tx.commit();

session1.close();

Here, after getting employee instance e1 and we are changing the state of e1.

After changing the state, we are committing the transaction. In such a case, the state will be updated automatically. This is known as dirty checking in hibernate.

  1. How many types of association mapping are possible in hibernate?

There can be 4 types of association mapping in hibernate.

  1. One to One
  2. One to Many
  3. Many to One
  4. Many to Many
  1. Is it possible to perform collection mapping with One-to-One and Many-to-One?

No, collection mapping can only be performed with One-to-Many and Many-to-Many.

  1. What inheritance mapping strategies are available in Hibernate?

Hibernate have 3 ways of inheritance mapping, they are

  1. Table per hierarchy
  2. Table per concrete class
  3. Table per subclass 

  23. What is lazy loading in hibernate?

Lazy loading in hibernate improves the performance. It loads the child objects on demand.

Since Hibernate 3, lazy loading is enabled by default, and we don't need to do lazy="true". It means not to load the child objects when the parent is loaded.

  1. What is the difference between first level cache and second level cache?

First Level Cache

Second Level Cache
  • First Level Cache is associated with Session.
  • Second Level Cache is associated with SessionFactory
  • It is enabled by default.
  • It is not enabled by default.

 

  1. What is the difference between update and merge method?
The update() method merge() method
  • Update means to edit something.
  • Merge means to combine something.
  • update() should be used if the session doesn't contain an already persistent state with the same id. It means an update should be used inside the session only. After closing the session, it will throw the error.
  • merge() should be used if you don't know the state of the session, means you want to make the modification at any time.

 

   26. What is the difference between get and load method?

get() load()
  • Returns null if an object is not found.
  • Throws ObjectNotFoundException if an object is not found.
  • get() method always hit the database.
  • load() method doesn't hit the database.
  • It returns the real object, not the proxy.
  • It returns proxy object.
  • It should be used if you are not sure about the existence of instance.
  • It should be used if you are sure that instance exists.
  1. What is Hibernate caching?

Hibernate caches Query data and makes the application run faster.

If used correctly, the hibernate cache can be very useful in achieving the faster application running performance.

The main idea lying behind the cache is reducing the number of database queries, which results in reduced throughput time of the application.

 28. Explain the persistent classes in Hibernate?

In hibernate, the Java classes whose instances and objects are stored in database classes are called persistent classes.

  1. Which annotation is used to declare a class as a hibernate bean?

@Entity annotation is used to declare a class as an entity.

Example: 

@Entity
@Table(name="emp")
public class Emp{
String name;
String description;
}

 

  1. How to write composite key mapping for hibernate?

If the table has a primary key then in the hibernate mapping file we need to configure that column by using <id /> element right..!

in order to configure these primary key columns in the hibernate mapping file we need to use one new element called <composite-id …..> </composite-id>

 

  1. What is the difference between save and persist in hibernate?

Save return - Returns Serializable object

Persist return - Returns Void

  1. What are the important interfaces of Hibernate framework?

Important interfaces of Hibernate framework are:

  • SessionFactory (org.hibernate.SessionFactory)
  • Session (org.hibernate.Session)
  • Transaction (org.hibernate.transaction)
  1. What is the difference between Lazy and Eager loading in hibernate?

Eager Loading is a design pattern in which data initialization occurs on the spot. it fetches the child entities along with parent.

Lazy Loading is a design pattern which is used to defer initialization of an object as long as it’s possible. It fetches the child entities lazily, that is, at the time of fetching parent entity it just fetches proxy of the child entities and when you access any property of child entity then it is actually fetched by hibernate.


↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑