-->
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.  [ 4 posts ] 
Author Message
 Post subject: Transient annotation and merge
PostPosted: Thu Jul 10, 2008 8:13 am 
Newbie

Joined: Thu Jul 10, 2008 7:09 am
Posts: 3
Hi,

I have a question about @Transient fields, merge and persist:

Today, hibernate entityManager does not copy Transient annotated fields in a merge operation:

Code:
@Entity
public MyEntity implements Serializable {
@Id
private Long id

private String persisted

@Transient
private String myTransient

public MyEntity() {}

public MyEntity(String persisted, String myTransient) {
this.persisted = persisted;
this.myTransient = myTransient;
}

//Get/Setters

}

//Main code
MyEntity  myEntity = new MyEntity('persistedString','myTransientString');
entityManager1.persist(myEntity)
//myEntity.myTransient = "myTransientString";

//Detach myEntity!!!!!!
MyEntity merged = entityManager2.merge(myEntity)
//merged.myTransient = null;
//myEntity.myTransient = "myTransientString";



So, to sum up, when I do a persist, My real object becomes an entity, but with merge, I get a new entity and my object is left "untouched" (this is written in docs or specs)

The problem with this is that I have some logic using transient fields, and after detach it and merge again, I must copy all variables to the managed entity again (this is not written in docs or specs), leading to different codes for persist and merge, and that is not nice. As a second example, I could have some PreUpdate/PrePersist/AfterLoad that uses this transient fields to modify "persisted". But it will fail on @PreUpdate because @Transient fields will be null.

Should this be the normal behavior? I looked in the forum/googled but I did not find conclusive answers, so is this a bad practice?

Thanks for your help!

====
hibernate-entitymanager 3.3.1.ga
hibernate-annotations 3.3.0.ga
hibernate 3.2.6.ga


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 10, 2008 5:17 pm 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
Yes, this is the behaviour of Hibernate regarding @Transient attributes. I'm also working with detached objects, where some of them have transient fields. So after each merge operation I have to call some helper methods to restore the values of transient fields to their former value when the merge call returned the updated object.

Anyone here who uses another JPA tool (Toplink etc) and has other experiences with this problem?

_________________
Carlo
-----------------------------------------------------------
please don't forget to rate if this post helped you


Top
 Profile  
 
 Post subject: More problematic than I thought
PostPosted: Thu Jul 17, 2008 9:31 pm 
Newbie

Joined: Thu Jul 10, 2008 7:09 am
Posts: 3
And just to add something...

If I call persist directly over the entity, I will have my transient values filled.
If I call persist in a "owner" Entity, using cascades, my transients fields will not be filled anymore.

Kind of erratic to me... but maybe this post saves the time of someone else.

I will try toplink later, and I will post results here


Top
 Profile  
 
 Post subject: Re: Transient annotation and merge
PostPosted: Thu Aug 27, 2009 2:58 pm 
Newbie

Joined: Thu May 29, 2008 3:14 am
Posts: 4
I am having the same problem. is it still unresolved ?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.