Saturday, 7 September 2013

Hibernate Interview Questions

Interview Questions



Question: - What is Session in Hibernate?

Answer: - Session is an interface in hibernate. We can get Session objects using SessionFactory references. Session is a wrapper for “java.sql.Connection”.It performs manipulation on DB entities. This is the basic interface for hibernate framework. Session object is light weight and can be created and destroyed without expensive process. It is short lived object and not thread safe. It works as a factory for org.hibernate.Transaction”.It maintains the first level cache by default and this can be used while searching for the objects using identifier.

Question: - What session.refresh() method will do?

Answer: - session.refresh():- During the bulk update to the DB with hibernate, the changes made are not replicated to the entities stored in the current session. So calling session.refresh will load the modifications to session entities.

Question: - What session.flush () method will do?

Answer: - session.flush ():- This tells Hibernate to execute the SQL statements needed to synchronize the JDBC connection's state with the state of objects held in the session-level cache. session.flush ()call will save the object instances to the database.

Question: - What session.clear () method will do?

Answer: - session.clear():- session.clear() method call can be used to clear the persistance context. It completely clears the session. This method call evicts all the objects from the session.

Question: - What session.evict (Object obj) method will do?

Answer: - session.evict(Object object): This method call will remove the instance that is passed as an argument in the evict method from the session cache and make a persistent object as a detached object.

Question: - What session.save() method will do?

Answer: - session.save() :This method call does an insert and will fail if the primary key is already available.

Question: - What session.saveOrUpdate()method will do?

Answer: - session.saveOrUpdate() :This method call does a select first to determine if it needs to do an insert or an update. Insert data if primary key is not already available in database otherwise do update data.

Question: - What session.persist () method will do?

Answer: - session.persist() :This method call does the same like session.save().
But session.save() returns Serializable object but session.persist() returns void.
session.save() returns the generated identifier (Serializable object) and session.persist() doesn’t.

Question: - What session.delete()method will do?

Answer: - session.delete(Object obj):- This method call will remove a persistent instance from the datastore.

Question: - What are the Fetching Strategies in hibernate?
Answer: -Fetching Strategies

Below are the fetching strategies.

1. fetch-”join” = this disable the lazy loading, always load all the collections and entities.
2. fetch-”select” (default) = this will lazily load all the collections and entities.
3. fetch-”subselect” = this will group its collection into a sub select statement.

Note: - These stretegies we can use in FetchMode.

Question: - What is Lazy and Eager loading strategies?

Answer: - The EAGER strategy is a hint given to the persistence provider that at runtime the data must be eagerly fetched (fetch in one query).
The LAZY strategy is a hint given to the persistence provider that at runtime the data should be fetched lazily when it is first accessed (fetch when needed as sub-queries).
  •  EAGER Fetch — get results in one query ( parent and child both ) 
  • LAZY Fetch – get results as sub-query.
For Example:
Suppose we have an entity called Student and another entity called Department.
The Student entity might have some basic properties such as id, name etc. as well as a property called Department.
Now Using Eager Loading, when we load a Student from the database, Hibernate will load student id, name fields and it also loads related Department details using the getDepartment() method -This is called  eager loading.
When a Student is associated with many Departments then it is not efficient to load its entire Department because it will create overhead on performance as they are not needed. So in such cases, we can declare that we want Department to be loaded when they are actually needed. This is called lazy loading.


Question: - what is the difference between fetch = FetchType.LAZY and FetchMode.SELECT.

Answer: -
FetchType:- There are two FetchType categories:-
LAZY and EAGER
Fetch type  refers to when Hibernate will fetch the association, whether in advance when it fetches the entity (eager), or whether it waits for the code to ask for the association (lazy).
Fetch mode refers to howHibernate will fetch the association, i.e. does it use an extra SELECT statement, or does it use a join. For example:-
@Fetch(FetchMode.SELECT)

      @OneToOne(mappedBy = "customer", cascade = CascadeType.ALL, fetch = FetchType.LAZY)

      privateCustomerAddress customerAddress;

No comments:

Post a Comment