-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 posts ] 
Author Message
 Post subject: Loosing non-persistent field values in EntityManager.merge()
PostPosted: Wed Feb 14, 2007 10:53 am 
Newbie

Joined: Wed Jun 28, 2006 3:32 pm
Posts: 16
Hibernate version: 3.2.0.GA

I have a non-persistent field on a super class of my persistent class. When I call EntityManager.merge() with an instance of my persistent class, the field is reset to its default value in the returned object. Is there a way I can tell Hibernate to copy the correct value over? I wouldn't expect to loose data during a merge().


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 15, 2007 8:17 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
they have to be persistent

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 16, 2007 9:02 pm 
Newbie

Joined: Wed Jun 28, 2006 3:32 pm
Posts: 16
Thanks for the reply. Do you think it is okay for me to open a bug for this?

It don't think it is reasonable to strip out all non-persistent data when doing a merge(). After all, those are POJOs and should have POJO behavior, which is a deep clone in this case.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 17, 2007 1:13 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
its a deep clone of the *persistent* data; not of random/all fields.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 17, 2007 6:20 pm 
Newbie

Joined: Wed Jun 28, 2006 3:32 pm
Posts: 16
I understand that, but I think it is inconsistent with what this is trying to be: idiomatic POJO persistence. And this behavior also creates a major problem for me: I am trying to keep track of which new object in a graph is which on the client, but since all non-persistent attributes are wiped out I cannot do that.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 18, 2007 4:44 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
so following your reasoning

Code:
s.saveOrUpdate(owithtransientfields);
s.close();

s = sf.openSession();
loadedo = s.get(O.class, owithtransientfields.getId();


loadedo should also contain the right values for the transient fields to be what you call "idomatic persistence" - but that does not make sense to save things that are not persistent.

If you want to keep the exact same instance use .saveOrUpdate(); .merge() is for handling the *persistent* state.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 18, 2007 11:30 am 
Newbie

Joined: Wed Jun 28, 2006 3:32 pm
Posts: 16
In the example you mention transient or non-persistent fields cannot be there. Session.get() asks for a persistent object by its id, which means that the persistent object is loaded from the database, so only persistent fields can be populated.

What I am trying to say is that EntityManager.merge() should behave like Session.saveOrUpdate(), leaving the entire object graph intact, including non-persistent fields. Hm, thinking about this further, it is a little strange that EntityManager.merge() returns the merged graph, instead of simply updating it like Session.saveOrUpdate(). But if I remember correctly, EntityManager.persist() fails if it encounters an already persisted object, right?

I didn't invent the term "idiomatic Java persistence", that comes from Hibernate ;). I just took it to mean a form of persistence that preserves the usual Java behavior wherever possible. In that respect I really like the behavior of Session.saveOrUpdate() in that it simply lets me persist an existing graph without worrying if the objects in it are already persistent or not.

Unfortunately I cannot easily use Session.saveOrUpdate() since we try to stick with the EJB3 API, where EntityManager.merge() seems to be the only equivalent.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 19, 2007 2:12 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
yes, but merge != saveOrUpdate.

merge takes the existing *persistent* properties/objectgraph and *merges* into the session - that means any non-persistent part is not available on the returned object....just as with the get operation.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 19, 2007 12:53 pm 
Newbie

Joined: Wed Jun 28, 2006 3:32 pm
Posts: 16
The difference between merge() and get() is that get() retrieves the object by primary key from the database, so there is no way that any non-persistent fields could be populated. But merge() takes an existing graph of objects and could easily perform a deep clone. So why is all the non-persistent data stripped out? I fail to see the benefit of stripping out the non-persistent data, but I know some drawbacks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 19, 2007 1:16 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
why on earth (and how) should hibernate know how to do a deep clone of your objects besides the properties that you have told hibernate how to access/modify ? doing a field by field copy would be the (almost) the same as serializing the object....and how deep should it go for non-persistent associations ? 0 ? 2.5 ? infinite ?

Think about it ...and you will see it makes perfect sense (since the alternative will have much worse consequences)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 19, 2007 3:49 pm 
Newbie

Joined: Wed Jun 28, 2006 3:32 pm
Posts: 16
Once again, thanks for taking the time to reply.

I see your point, I guess the only solution would be a change to the EJB3 standard that brings the saveOrUpdate() functionality into it somehow; or maybe a special annotation that controls the cloning. Since that is unlikely to happen in the near future, should I expect any problems when I get the Hibernate Session from the EntityManager and use saveOrUpdate() from there?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 20, 2007 4:00 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
No.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.