Thursday, 3 November 2016

Flow of Hibernate



Explain the general flow of Hibernate communication with RDBMS.
- The general flow of Hibernate communication with RDBMS is :
1. The Hibernate configuration is to be loaded and creation of configuration object is done. The mapping of all hbm files will be performed automatically.
2. Creation of session factory from the configuration object.
3. Obtain a session from the session factory.
4. Creation of HQL Query
5. Execution of the query in order to get the list of containing java objects.

Explain the general flow of Hibernate communication with RDBMS.
- The general flow of Hibernate communication with RDBMS is :
1. Load the Hibernate configuration file and create configuration object. It will automatically load all hbm mapping files.
2. Create session factory from configuration object.
3. Get one session from this session factory.
4. Create HQL Query.
5. Execute query to get list containing Java objects

Advanced Problems in hibernate

Here are the solutions to some frequent advanced problems.

Role of Session interface 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

Hibernate object State or Object Life cycle





1. Transient State:
    A New instance of  a persistent class which is not associated with a Session, has no representation in the database and no identifier value is considered transient by Hibernate:

UserDetail user = new UserDetail();  
user.setUserName("sankesh");  
// user is in a transient state

UserDetail user = new UserDetail();
user.setUserName("sankesh");
// user is in a transient state
2. Persistent State:
    A persistent instance has a representation in the database , an identifier value and is associated with a Session. You can make a transient instance persistent by associating it with a Session:

Long id = (Long) session.save(user);  
// user is now in a persistent state

Long id = (Long) session.save(user);
// user is now in a persistent state
3. Detached State:
    Now, if we close the Hibernate Session, the persistent instance will become a detached instance: it isn't attached to a Session anymore (but can still be modified and reattached to a new Session later though).

session.close();  
//user in detached state

session.close();
//user in detached state
Difference between Transient and Detached States:
Transient objects do not have association with the databases and session objects. They are simple objects and not persisted to the database. Once the last reference is lost, that means the object itself is lost. And of course , garbage collected. The commits and rollbacks will have no effects on these objects. They can become into persistent objects through the save method calls of Session object.

The detached object have corresponding entries in the database. These are persistent and not connected to the Session object. These objects have the synchronized data with the database when the session was closed. Since then, the change may be done in the database which makes this object stale. The detached object can be reattached after certain time to another object in order to become persistent again.

Core interfaces and classes of Hibernate framework?



The five core interfaces are used in just about every Hibernate application. Using these interfaces, you can store and retrieve persistent objects and control transactions.

Session interface
Session-factory interface
Configuration interface
Transaction interface
Query and Criteria interfaces 

What is Hibernate proxy?

The proxy attribute enables lazy initialization of persistent instances of the class.
Hibernate will initially return CGLIB proxies which implement the named interface.
The actual persistent object will be loaded when a method of the proxy is invoked.

Implement lazy loading in Hibernate

Lazy=false(fetching child object when fetching parent object)
Lazy=true(child object not fetching when fetching parent object)

What is ORM?

ORM stands for Object-Relational Mapping (ORM) is a programming technique for converting data between relational databases and object oriented programming languages such as Java, C# etc.
An ORM system has following advantages over plain JDBC S.N.
  Advantages
 1 Lets business code access objects rather than DB tables.
 2 Hides details of SQL queries from OO logic.
 3 Based on JDBC 'under the hood'
 4 No need to deal with the database implementation.
 5 Entities based on business concepts rather than database structure.
 6 Transaction management and automatic key generation.
 7 Fast development of application.

Hibernate Architecture

The above diagram shows that Hibernate is using the database and configuration data to provide persistence services (and persistent objects) to the application.

To use Hibernate, it is required to create Java classes that represents the table in the database and then map the instance variable in the class with the columns in the database. Then Hibernate can be used to perform operations on the database like select, insert, update and delete the records in the table. Hibernate automatically creates the query to perform these operations.

Hibernate architecture has three main components:

1. Connection Management: Hibernate Connection management service provide efficient management of the database connections. Database connection is the most expensive part of interacting with the database as it requires a lot of resources of open and close the database connection.

2. Transaction management: Transaction management service provide the ability to the user to execute more than one database statements at a time.

3. Object relational mapping: Object relational mapping is technique of mapping the data representation from an object model to a relational data model. This part of the hibernate is used to select, insert, update and delete the records form the underlying table. When we pass an object to a Session.save() method, Hibernate reads the state of the variables of that object and executes the necessary query.

Hibernate is very good tool as far as object relational mapping is concern, but in terms of connection management and transaction management, it is lacking in performance and capabilities. So usually hibernate is being used with other connection management and transaction management tools. For example apache DBCP is used for connection pooling with the Hibernate.

Hibernate framework simplifies the development of java application to interact with the database. Hibernate is an open source, lightweight, ORM (Object Relational Mapping) tool. An ORM tool simplifies the data creation, data manipulation and data access. It is a programming technique that maps the object to the data stored in the database.

Tuesday, 1 November 2016

Difference between getCurrentSession() and openSession() in Hibernate ?

My title page contents getCurrentSession() :

The "current session" refers to a Hibernate Session bound by Hibernate behind the scenes, to the transaction scope. A Session is opened when getCurrentSession() is called for the first time and closed when the transaction ends. It is also flushed automatically before the transaction commits. You can call getCurrentSession() as often and anywhere you want as long as the transaction runs.

To enable this strategy in your Hibernate configuration:

set hibernate.transaction.manager_lookup_class to a lookup strategy for your JEE container set hibernate.transaction.factory_class to org.hibernate.transaction.JTATransactionFactory

Only the Session that you obtained with sf.getCurrentSession() is flushed and closed automatically.

openSession() : If you decide to use manage the Session yourself the go for sf.openSession() , you have to flush() and close() it. It does not flush and close() automatically.