Save detached hibernate

Solo disponible en BuenasTareas
  • Páginas : 6 (1453 palabras )
  • Descarga(s) : 0
  • Publicado : 19 de enero de 2011
Leer documento completo
Vista previa del texto
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 andworks 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 instanceexists,
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();

A complete diagram of thelifecycle now makes sense, given a description of the states and methods that
transition the objects between states.
[pic]
Detached State

Once a session is closed, a Persistent object is still stored in the database. The object is still a valid Java object
and can be manipulated (have its data changed). But without a session, the Hibernate persistence manager has
no way of synchronizing thedata in the Persistent object with the database. These objects are no longer part
of the persistence context.

Persistent objects become Detached objects when the session is closed. Calling session.evict() on the object
reference or session.clear() will also remove an object from session. These later session operations will be
covered later in class when cache is discussed.

[pic]...
tracking img