@ PrePersist / @ PreUpdate is called when the object is loaded from the database
Current setup: Hibernate 4.3.6, Wildfly 8.
I've done some migration of relationships between entities and to ensure compatibility with the previous data, I use entity callbacks to process the new field from the old field.
@Entity
public class MyEntity{
@NotNull
@Deprecated
private String previousField;
@OneToOne(cascade=ALL)
private MyNewFieldType newField;
@PrePersist
@PreUpdate
void movePreviousToNewField() {
this.newField = movePrevious(this.previousField);
}
}
and
@Entity
public class MyOtherEntity{
@OneToOne(cascade = ALL)
@NotNull
private MyEntity myEntity;
}
When I do the following:
@PersistenceContext
private EntityManager em;
....
//This works fine, the @PreUpdate/@PrePersist is not called on the MyEntity
MyOtherEntity entity = em.findEntity(...);
//This on loading the relations, calls @PreUpdate/@PrePersist??
List<MyOtherEntity> entities = em.findEntities(...);
For loading the list, I am getting an exception:
object references an unsaved transient instance - save the transient instance before flushing on (MyNewFieldType.id)
I am not trying to save anything, I am just loading the list.
Note that when I don't have a @ PreUpdate / @ PrePersist callback and execute it manually and then update the object everything works fine. The new cascade object field is saved as expected.
+3
source to share
No one has answered this question yet
Check out similar questions: