Hibernate Interview Questions Part II

One To Many Bi-directional Relation in Hibernate?

Bi-Directional One to Many Relation- EXAMPLE

PROCESS_TYPE_LOV (PROCESS_TYPE_ID number, PROCESS_TYPE_NAME varchar) - TABLE
PROCESS (PROCESS_ID number,PROCESS_NAME varchar,PROCESS_TYPE_ID number)- TABLE

One To Many Mapping Using List ?

WRITER (ID INT,NAME VARCHAR) - TABLE

STORY (ID INT,INFO VARCHAR,PARENT_ID INT) - TABLE

One writer can have multiple stories..

Mapping File...




Many To Many Relation In Hibernate ?

Best Example for Many to Many in Hibernate..

EVENTS (uid int, name VARCHAR) Table
SPEAKERS (uid int, firstName VARCHAR) Table
EVENT_SPEAKERS (elt int, event_id int, speaker_id int) Table



Event.hbm.xml


Speaker.hbm.xml


Save and Fetch Example

What is the main difference between Entity Beans and Hibernate ?
  1. In Entity Bean at a time we can interact with only one data Base. Where as in Hibernate we can able to establishes the connections to more than One Data Base. Only thing we need to write one more configuration file.

  2. EJB need container like Weblogic, WebSphare but hibernate don't need. It can be run on tomcat.

  3. Entity Beans does not support OOPS concepts where as Hibernate does.

  4. Hibernate supports multi level caching, where as Entity Beans doesn't.

  5. In Hibernate C3P0 can be used as a connection pool.

  6. Hibernate is container independent. EJB not.

Hibernate session.close does not call session.flush ?

session.close() don't call session.flush() before closing the session.

This is the session.close() code in hibernate.jar

What is Hibernate proxy?
  • By default Hibernate creates a proxy for each of the class you map in mapping file. This class contain the code to invoke JDBC. This class is created by hibernate using CGLIB.

  • Proxies are created dynamically by subclassing your object at runtime. The subclass has all the methods of the parent, and when any of the methods are accessed, the proxy loads up the real object from the DB and calls the method for you.

  • Very nice in simple cases with no object hierarchy. Typecasting and instanceof work perfectly on the proxy in this case since it is a direct subclass.

Equal and Not Equal criteria query.

Equal and Not Equal criteria query- Example

  • List of organisation where town equals to pune.
    List organizationList = session.createCriteria(Organization.class).add(Restrictions.eq("town","pune")).list();

  • List of organisation where town not equals pune.
    List organizationList = session.createCriteria(Organization.class).add(Restrictions.ne("town","pune")).list();

What is the Filter in Hibernate?

Filter in Hibernate- Example

USER (ID INT, USERNAME VARCHAR, ACTIVATED BOOLEAN) - TABLE



Save and Fetch using filter example.

Guess the Result :
name2
name3

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

Learn about Hibernate from our Experts