Hibernate Object Identifier

Hibernate has different object Identifiers.Hibernate provide different class objects.  They are

  1. Configuration object
  2. SessionFactory object
  3. session object
  4. Transaction object
  5. Query object
  6. Criteria object
Hibernate Object Identifier
Hibernate Object Identifier

 

 

 

 

1)      Configuration object

The first Hibernate object is the configuration object. We can create this object in any Hibernate application and usually created once during application initialization. The configuration object consists of two keys component. They are

a)      Database Connection:- It helps to handle one or more configuration files supported by Hibernate. The configuration files are   hibernate.properties and hibernate.cfg.xml

b)      Class Mapping Setup:- This helps to create the connection between the java classes and database tables.

2)      SessionFactory Object

Configuration object is used to create an object that configures inturn Hibernate SessionFactory for the application using the supplied configuration file and allows a Session object to run. The SessionFactory is a thread object safe and used by all application threads. The SessionFactory is heavy for what is normally created during application start-up and kept for later use. You would have a SessionFactory object per database using a separate configuration file. So if you are using multiple databases, then you would create multiple SessionFactory objects.

3)      Session Object

A session is used to obtain a physical connection to a database. The Session object is lightweight and is designed to run every time you need to interact with the database. Persistent objects are stored and retrieved through a Session object. Session object must not be kept open for long because they are usually thread safe and should be created and destroyed as needed.

4)      Transaction Object

A transaction represents a unit of work with the database and most RDBMS supports the functionality of the transaction. Hibernate transactions are handled by an underlying transaction manager and transaction (JTA or JDBC). This is an optional object and Hibernate applications may choose not to use this interface, instead of managing transactions on your own applications code.

5)      Query Object

Query object mainly use SQL or Hibernate Query Language (HQL) string and is used to extract data from the database and also create objects. A Query instance is used to bind query parameters, limit the number of results returned by the query and finally to execute the query.

6)      Criteria Object

These objects are used to create and execute object oriented criteria queries to retrieve objects.

Hibernate Object State

Hibernate support three different object state. They are

  1. Transient
  2. Persistent
  3. Detached

1)      Transient

An object is transient if it has just created with the new operator, and is not associated with a Hibernate session. It has no persistent representation in the   database and no identifier value has been assigned. Transitional bodies will be destroyed by the garbage collector if the application has no more reference. Use the Hibernate Session to make an object persistent (and let Hibernate take care of SQL statements to be executed for this transition).

2)      Persistent

A persistent instance has a representation in the database and an identifier value. Maybe I just have been saved or loaded, however, is, by definition, within a session. Hibernate will detect any changes made to an object in persistent state and synchronize the state with the database when the complete working unit. Developers do not execute statements manual Update or DELETE statements when an object must be transient.

3)      Detached

A detached instance is an object that has been persistent, but your session has been closed. The object reference is still valid, of course, and the detached instance might even be modified in this state. A detached instance can be reattached to a new session at a later point in time, making it (and all the modifications) persistent again. This feature enables a programming model for operation of the units that require long working time the user. We call upon application transactions, i.e., a unit of work from the user point of view.

Persistent objects do have an identifier. In fact, Hibernate persistent manager is based on the existence of an identifier to determine the state of an object. If an object has an identifier, then the object is persistent or independent (if not a session). No identifier, then the object is transient. This is what allows the saveOrUpdate () to work. When Session.saveOrUpdate () is called on an object without an ID, Hibernate calls save () to preserve what it sees as a transient object. When the session.saveOrUpdate () is called on an object with an ID, Hibernate calls update () to re-unite what looks like a Separate object. This is the reason why you should set the ID property of an object to null when the method session.delete () is called! In essence, this restores an object completely to its transient state. Otherwise, a call to saveOrUpdate () let Hibernate thinking of an update () is the order which results in an exception thrown.

The identifier property of the persistent classes has been covered type objects.

Eg: – Long versus Java primitive long: – The identifier is used to determine state of an object. For setting a primitive property to null we are using two methods.

  1. Use object types (Long, Integer etc) as the type for identifier.
  2. Use primitive for an identifier, but then unsaved value attribute must be provided in the <id> element for the mapping file.
public class BallPlayer {
private long id;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}

In this example, the value of 0 is used as an unsaved value for BallPlayers.

<id unsaved-value="0">
<generator class="sequence">
<param name="sequence">common_seq</param>
</generator>
</id>

When deleting a Persistent object that uses a primitive as an identifier, set the identifier property back to the unsaved value.

Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
BallPlayer p = (BallPlayer)session.get(BallPlayer.class, 2L);
session.delete(p);
transaction.commit();
session.close();
p.setId(0)

The identifier is also important for another reason. Hibernate guarantees that during a session, a persistent object is represented by one and only one Java object. Consider the following example code. The == conditional will always be true.

Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
BallPlayer p1 = (BallPlayer)session.get(BallPlayer.class, 2L);
BallPlayer p2 = (BallPlayer)session.get(BallPlayer.class, 2L);
if (p1==p2) {
System.out.println("These two object references point to the same
instance!");
}
transaction.commit();
session.close();

This guarantee ensures that two instances of persistent objects are not trying to update the database simultaneously. This is especially critical in multi-threaded situations. It also helps to conserve space Hibernate persistence context. The guarantee, however, does not hold when working with objects that are independent