Ads 468x60px

Pages

Tuesday, 24 July 2012

Hibernate interview questions

10. What is the need for Hibernate xml mapping file?
Hibernate mapping file tells Hibernate which tables and columns to use to load and store objects.

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0"?>
 <!DOCTYPE hibernate-mapping PUBLIC
<hibernate-mapping>
 <class name="sample.MyPersistanceClass" table="MyPersitaceTable">
 <id name="id" column="MyPerId">
 <generator class="increment"/>
 </id>
 <property name="text" column="Persistance_message"/>
 <many-to-one name="nxtPer" cascade="all" column="NxtPerId"/>
 </class>
</hibernate-mapping>
Everything should be included undertag. This is the main tag for an xml mapping document.

11. What the Core interfaces are of hibernate framework?
There are many benefits from these. Out of which the following are the most important one.
  1. Session Interface – This is the primary interface used by hibernate applications. The instances of this interface are lightweight and are inexpensive to create and destroy. Hibernate sessions are not thread safe.
  2. SessionFactory Interface – This is a factory that delivers the session objects to hibernate application. Generally there will be a single SessionFactory for the whole application and it will be shared among all the application threads.
  3. Configuration Interface – This interface is used to configure and bootstrap hibernate. The instance of this interface is used by the application in order to specify the location of hibernate specific mapping documents.
  4. Transaction Interface – This is an optional interface but the above three interfaces are mandatory in each and every application. This interface abstracts the code from any kind of transaction implementations such as JDBC transaction, JTA transaction.
  5. Query and Criteria Interface – This interface allows the user to perform queries and also control the flow of the query execution.
12. What role does the Session interface play in 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.

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.

13. What is the general flow of Hibernate communication with RDBMS?
The general flow of Hibernate communication with RDBMS is :
  • Load the Hibernate configuration file and create configuration object. It will automatically load all  Hibernate mapping files
  • Create session factory from configuration object
  • Get one session from this session factory
  • Create HQL Query
  • Execute query to get list containing Java objects 
14. 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 :
<hibernate-mapping>
  <class name="com.test.User"  table="user">
   <property  column="USER_NAME" length="255"
      name="userName" not-null="true"  type="java.lang.String"/>
   <property  column="USER_PASSWORD" length="255"
     name="userPassword" not-null="true"  type="java.lang.String"/>
 </class>
</hibernate-mapping>

0 comments:

Post a Comment