Ads 468x60px

Pages

Thursday, 26 July 2012

Hibernate interview questions



15. How can a whole class be mapped as immutable?
Mark the class as mutable="false". This specifies that instances of the class are (not) mutable. Immutable classes, may not be updated or deleted by the application. Default value is true.


16. What is the use of dynamic-insert and dynamic-update attributes in a class mapping?
Criteria is a simplified API for retrieving entities by composing Criterion objects. This is a very convenient approach for functionality like "search" screens where there is a variable number of conditions to be placed upon the result set.
  • dynamic-update (defaults to false): Specifies that UPDATE SQL should be generated at runtime and contain only those columns whose values have changed.
  • dynamic-insert (defaults to false): Specifies that INSERT SQL should be generated at runtime and contain only the columns whose values are not null. 
17. What do you mean by fetching strategy ?
A fetching strategy is the strategy Hibernate will use for retrieving associated objects if the application needs to navigate the association. Fetch strategies may be declared in the O/R mapping metadata, or over-ridden by a particular HQL or Criteria query. 

18. What are Callback interfaces?
Callback interfaces allow the application to receive a notification when something interesting happens to an object. For example, when an object is loaded, saved, or deleted. Hibernate applications don't need to implement these callbacks, but they're useful for implementing certain kinds of generic functionality.

19. What are the types of Hibernate instance states ?
Three types of instance states:
  • Transient -The instance is not associated with any persistence context.
  • Persistent -The instance is associated with a persistence context having identity value representing record of the table.This object maintains synchronization with Database table record.
  • Detached -The instance was associated with a persistence context which has been closed – currently not associated. 
By using various methods of Hibernate API we can change Hibernate pojo class object from one state to another state.
We can make Transient object as Persistent object. But we can't make Detached object and Persistent object as Transient object.

20. What is automatic dirty checking?
Automatic dirty checking is a feature that saves us the effort of explicitly asking Hibernate to update the database when we modify the state of an object inside a transaction.

21. What is the difference between updating record by calling update() and merge()?
Both methods are given to update the record.If given record is not available update() does not perform any default operation,but merge() inserts the new record into the Database by having the data of given Hibernate pojo class object.
By using update() we can't gather the persistent state base Hibernate pojo class object representing the updated record.
By using merge() this operation is possible.

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>

Hibernate interview questions


1. What is Hibernate?
Hibernate is a pure Java object-relational mapping (ORM) and persistence framework that allows you to map plain old Java objects to relational database tables using (XML) configuration files.Its purpose is to relieve the developer from a significant amount of relational data persistence-related programming tasks.

2. What is ORM ?
ORM stands for object/relational mapping. ORM is the automated persistence of objects in a Java application to the tables in a relational database.

3. What are the ORM levels ?
The ORM levels are:
  • Pure relational
  • Light objects mapping
  • Medium object mapping
  • Full object Mapping

4. What is a pure relational ORM?
The entire application, including the user interface, is designed around the relational model and SQL-based relational operations.

5. What is a meant by light object mapping?
The entities are represented as classes that are mapped manually to the relational tables. The code is hidden from the business logic using specific design patterns. This approach is successful for applications with a less number of entities, or applications with common, metadata-driven data models. This approach is most known to all.

6. What is a meant by medium object mapping?
The application is designed around an object model. The SQL code is generated at build time. And the associations between objects are supported by the persistence mechanism, and queries are specified using an object-oriented expression language. This is best suited for medium-sized applications with some complex transactions. Used when the mapping exceeds 25 different database products at a time.

7. What is meant by full object mapping?
Full object mapping supports sophisticated object modeling: composition, inheritance, polymorphism and persistence. The persistence layer implements transparent persistence; persistent classes do not inherit any special base class or have to implement a special interface. Efficient fetching strategies and caching strategies are implemented transparently to the application.

8. What does ORM consists of ?
An ORM solution consists of the followig four pieces:
  • API for performing basic CRUD operations
  • API to express queries refering to classes
  • Facilities to specify metadata
  • Optimization facilities : dirty checking,lazy associations fetching
9. Why do you need ORM tools like hibernate?
The main advantage of ORM like hibernate is that it shields developers from messy SQL. Apart from this, ORM provides following benefits:
  • Improved productivity
    • High-level object-oriented API
    • Less Java code to write
    • No SQL to write
  • Improved performance
    • Sophisticated caching
    • Lazy loading
    • Eager loading
  • Improved maintainability
    • A lot less code to write
  • Improved portability
ORM framework generates database-specific SQL for you.