views:

94

answers:

2

I need this roadmap of a Hibernate managed object instance.

First, I create an instance with initial properties and persist this object in a db. Then session associated with this object is closed. But still, I serialize my object and on the next step deserialize it, invoke some setters, and again, I need to update what changed in a database.

What methods of Hibernate session should I use? persist() or save() on the first step and saveOrUpdate() on the second?

In fact I see that saveOrUpdate() can be used on each step. The only problem is that as I see, it does not return an associated PK. Do you know how to get it when we persist the object for the first time? I suppose, after we persist the object, it gets an id field set, so after invokation of saveOrUpdate(myObject), myObject.getId() returns the PK?

What would you recommend?

+2  A: 

If I do understand your problem, you want to reattach a transient object to a new hibernate session. Then you should use Session.merge(object)

merge

Object merge(Object object) throws HibernateException

Copy the state of the given object onto the persistent object with the same identifier. If there is no persistent instance currently associated with the session, it will be loaded. Return the persistent instance. If the given instance is unsaved, save a copy of and return it as a newly persistent instance. The given instance does not become associated with the session. This operation cascades to associated instances if the association is mapped with cascade="merge".

HeDinges
Be aware that when you say `session.merge(transientObject)`, it will not convert transientObject into a managed object. You need to say `managedObject = session.merge(transientObject)`. Then you can make more changes to `managedObject` and they will be saved when the session closes.
John
@John: +1 Apparently I had just added the same comment!
Elister
+1  A: 

From the hibernate documentation:

Transient - an object is transient if it has just been instantiated using the new operator, and it is not associated with a Hibernate Session. It has no persistent representation in the database and no identifier value has been assigned. Transient instances will be destroyed by the garbage collector if the application does not hold a reference anymore. Use the Hibernate Session to make an object persistent (and let Hibernate take care of the SQL statements that need to be executed for this transition).

Detached - a detached instance is an object that has been persistent, but its Session has been closed. The reference to the object 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 long running units of work that require user think-time. We call them application transactions, i.e., a unit of work from the point of view of the user.

By these definitions, your object would initially be transient. You would use persist to save it to the database making it persistent until the session is closed. After that, my understanding is that the object would be considered detached meaning you could modify it to your wishes and merge it with a new session: obj = entityManager.merge(obj)

The difference is that persist modifies the object you provide as a parameter and is meant to initially persist a new object while merge returns a modified instance (rather than modifying the one passed as a parameter) and is ment to allow updating.

Tomislav Nakic-Alfirevic