Search your topic in Java2share
Home > FAQs
eXTReMe Tracker

Hibernate FAQs

13.What role does the Session interface play in Hibernate?
The main runtime interface between a Java application and Hibernate The Session interface is the primary interface used by Hibernate applications. It is a single-threaded, short-lived object representing a conversation between the application and the persistent store. It allows you to create query objects to retrieve persistent objects.
The main function of the Session is to offer create, read and delete operations for instances of mapped entity classes. Instances may exist in one of three states:

transient: never persistent, not associated with any Session
persistent: associated with a unique Session
detached: previously persistent, not associated with any Session

Session session = sessionFactory.openSession();
Session interface role:

  • Wraps a JDBC connection
  • Factory for Transaction
  • Holds a mandatory (first-level) cache of persistent objects, used when navigating the object graph or looking up objects by identifier

14.What role does the SessionFactory interface play in Hibernate?

SessionFactorys are immutable. The behaviour of a SessionFactory is controlled by properties supplied at configuration time. These properties are defined on Environment.
The application obtains Session instances from a SessionFactory. There is typically a single SessionFactory for the whole application—created during application initialization. The SessionFactory caches generate SQL statements and other mapping metadata that Hibernate uses at runtime. It also holds cached data that has been read in one unit of work and may be reused in a future unit of work
Implementors must be threadsafe.
SessionFactory sessionFactory = configuration.buildSessionFactory();

15.What are the most common ways to specify the  Hibernate configuration properties?
The most common methods of Hibernate configuration are:

  • Programmatic configuration

        By using setProperty(-) method of org.hibernate.cfg.Configuration.

  • XML configuration (hibernate.cfg.xml)
  • By using .properties file
  • By Using annotaions.(from Hibernate 3.3 on words)

16.How do you map Java Objects with Database tables?

  • First we need to write Java domain objects (beans with setter and getter).
  • Write hbm.xml, where we map java class to table and database columns to Java class variables.

Example :
  <class name="com.durgasoft.EmployeeBean"  table="EMPLOYEE">
     <id name=”eid” colume=”id”/>
   <property name="ename" column="NAME" length="255"
     not-null="true"  type="java.lang.String"/>
   <property name="address" column="ADDR" length="255"
     not-null="true"  type="java.lang.String"/>

17.How do you define sequence generated primary key algorithm in hibernate?
By using <id>, <generator> tags we can configure the primary key and primary key generation algorithm.
<id name="userid" column="USER_ID" type="java.lang.Long">
   <generator class="sequence">
     <param name="table">SEQ_NAME</param>

18.What is component mapping in Hibernate?

  • A component is an object saved as a value, not as a reference
  • A component can be saved directly without needing to declare interfaces or identifier properties
  • Required to define an empty constructor
  • Shared references not supported