@ 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
java hibernate jpa wildfly


source to share


No one has answered this question yet

Check out similar questions:

2108
How can I call one constructor from another in Java?
341
Create the perfect JPA object
4
Field Level Encryption with Spring Data JPA and JPA EntityListener
3
@PreUpdate does not preserve the parent when it is updated
3
"object refers to an unsaved transient instance" in bidirectional one-to-one
1
How do I use the cascading MERGE and PERSIST types for many save or update operations?
1
Hibernate: CasecadeType.ALL vs {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH, CascadeType.REFRESH, CascadeType.REMOVE}
0
JPA query JPQL where object1 = object2
0
java.lang.IllegalStateException: transaction is not active
0
Replaying JPA listener preRemove not called for remote entity on cascade



All Articles
Loading...
X
Show
Funny
Dev
Pics