8:31 pm - Wednesday July 18, 2018

Hibernate Interview Question12

Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

1.            What are the different levels of ORM quality?

There are four levels defined for ORM quality.

  • Pure relational
  • Light object mapping
  • Medium object mapping
  • Full object mapping

2.            What is Hibernate?

Hibernate is a powerful, high performance object/relational persistence and query service. This lets the users to develop persistent classes following object-oriented principles such as association, inheritance, polymorphism, composition, and collections.


3.            What is the method we have to use for enabling second level caching in code?

Hibernate offers naturally a first level cache for entities called a persistence context via the notion ofSession. This cache is contextual to the use case at hand. Some entities however are shared by many different use cases and are barely changed. You can cache these in what is called the second level cache.

By default, entities are not part of the second level cache. While we do not recommend that, you can override this by setting the shared-cache-mode element in your persistence.xml file or by using thejavax.persistence.sharedCache.mode property. The following values are possible:

  • ENABLE_SELECTIVE (Default and recommended value): entities are not cached unless explicitly marked as cacheable.
  • DISABLE_SELECTIVE: entities are cached unless explicitly marked as not cacheable.
  • ALL: all entities are always cached even if marked as non cacheable.
  • NONE: no entity are cached even if marked as cacheable. This option can make sense to disable second-level cache altogether.

The cache concurrency strategy used by default can be set with the hibernate.cache.default_cache_concurrency_strategyproperty:

  • read-only
  • read-write
  • nonstrict-read-write
  • transactional

4.            What is the hibernate filter?

Hibernate3 provides an innovative new approach to handling data with “visibility” rules. AHibernate filteris a global, named, parameterized filter that can be enabled or disabled for a particular Hibernate session.

Hibernate3 has the ability to pre-define filter criteria and attach those filters at both a class level and a collection level. A filter criteria allows you to define a restriction clause similar to the existing “where” attribute available on the class and various collection elements. These filter conditions, however, can be parameterized. The application can then decide at runtime whether certain filters should be enabled and what their parameter values should be. Filters can be used like database views, but they are parameterized inside the application.


Filter in Hibernate ——



public class User


private int id;

private String username;

private boolean activated;



public boolean isActivated()


return activated;


public void setActivated(boolean activated)


this.activated = activated;


public int getId()


return id;


public void setId(int id)


this.id = id;


public String getUsername()


return username;


public void setUsername(String username)


this.username = username;




<?xml version='1.0' encoding='utf-8'?>

<!DOCTYPE hibernate-mapping

PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"




<class name="User">

<id name="id" type="int">




<property name="username" type="string" length="32"/>

<property name="activated" type="boolean"/>

<filter name="activatedFilter" condition=":activatedParam = activated"/>


<filter-def name="activatedFilter">

<filter-param name="activatedParam" type="boolean"/>




Save and Fetch using filter example

User user1 = new User();






User user2 = new User();






User user3 = new User();






User user4 = new User();





All the four user saved to Data Base User Table.


Now Fetch the User using Filter..

Filter filter = session.enableFilter("activatedFilter");

filter.setParameter("activatedParam",new Boolean(true));

Query query = session.createQuery("from User");

Iterator results = query.iterate();

while (results.hasNext())


User user = (User) results.next();

System.out.print(user.getUsername() + " is ");




Guess the Result :




Because Filer is filtering ( only true value) data before query execute.

5.            Difference between save and save or update?

save – save method stores an object into the database. That means it insert an entry if the identifier doesn’t exist, else it will throw error. If the primary key already present in the table, it cannot be inserted.

update – update method in the hibernate is used for updating the object using identifier. If the identifier is missing or doesn’t exist, it will throw exception.

saveOrUpdate – This method calls save() or update() based on the operation. If the identifier exists, it will call update method else the save method will be called.

6.            Explain about session.flush()?

Flushing the Session simply gets the data that is currently in the session synchronized with what is in the database. However, just because you have flushed, doesn’t mean the data can’t be rolled back.



7.            Difference between hibernate and EJB

EJB 3.0 used for developing Business component witch required container support like transaction,Security,logging,MDB’s and many more. for persistence EJB3.0 used JPA which can be plug gable to any persistence mechanism like Hibernate (Open source) or TopLink (Oracle Persistence mechanism). Hibernate is just a Persistence Mechanism.


Filed in: Z-A Interview Questions

No comments yet.

Leave a Reply