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