Sunday, June 3, 2012

Hibernate Object phases

Merge

In session, Hibernate guarantees no two Persistent objects represent the same row.  Again, this guarantee no 
longer holds with Detatched objects. In fact, this problem can create very unwanted consequences. Explore 
the code below.


Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
BallPlayer p1 = (BallPlayer)session.get(BallPlayer.class, 1L);
transaction.commit();
session.close();
//p1 is now detached.
session = sessionFactory.openSession();
transaction = session.beginTransaction();
BallPlayer p2 = (BallPlayer)session.get(BallPlayer.class, 1L);
//Oops!  p2 represents the same persistent row as p1.
//When an attempt to reattach p1 occurs, an exception is thrown
session.update(p1);
transaction.commit();
session.close();


This code throws an exception when an attempt to reattach the Detached object at p1 is made.

Exception in thread "main" org.hibernate.NonUniqueObjectException: a  
different object with the same identifier value was already associated
with the session: [com.intertech.domain.BallPlayer#1]
        at
org.hibernate.engine.StatefulPersistenceContext.checkUniqueness
(StatefulPersistenceContext.java:613)
...


This is because Hibernate is trying to reinforce the guarantee that only a single instance of a Persistent object 
exist in memory.

The merge operation, helps to deal with this situation.


Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
BallPlayer p1 = (BallPlayer)session.get(BallPlayer.class, 1L);
transaction.commit();
session.close();
//p1 is now detached.
session = sessionFactory.openSession();
transaction = session.beginTransaction();
BallPlayer p2 = (BallPlayer)session.get(BallPlayer.class, 1L);
BallPlayer p3 = (BallPlayer) session.merge(p1);
if (p2 == p3) {
  System.out.println("They're equal");
}
transaction.commit();
session.close();

The merge() method is a little complex and works differently depending on what is in/out of the persistence 
context. Hibernate will first check whether a Persistent instance of that type already exists in the persistent 
context. It uses the object identifiers to check on this existence. If another instance exists, it copies the state 
of the Detached object (p1 above) into the existing Persistence object (p2 above). If no other instance exists, 
Hibernate just reattaches the Detached object.

In the example above, p2==p3 and one Persistent object exists.  In the example below, two Persistent objects 
now exist and p2!=p3


Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
BallPlayer p1 = (BallPlayer)session.get(BallPlayer.class, 3L);
transaction.commit();
session.close();
//p1 is now detached.
session = sessionFactory.openSession();
transaction = session.beginTransaction();
BallPlayer p2 = (BallPlayer)session.get(BallPlayer.class, 1L);
BallPlayer p3 = (BallPlayer) session.merge(p1);
if (p2 == p3) {
  System.out.println("They're equal");
}
System.out.println(p2);
System.out.println(p3);
transaction.commit();
session.close();



No comments:

Post a Comment