Note that there are some explanatory texts on larger screens.

plurals
  1. POJPA/Hibernate preUpdate doesn't update parent object
    primarykey
    data
    text
    <p>In my application I defined following classes:</p> <pre><code>@Entity @Table(name = "forums") public class Forum { @Id @GeneratedValue(generator = "system-uuid") @GenericGenerator(name = "system-uuid", strategy = "uuid") private String id; private String name; private Date lastActivity; @OneToMany(mappedBy = "forum", cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE }) private List&lt;Post&gt; posts; </code></pre> <hr> <pre><code>@Entity @Table(name = "posts") public class Post { @Id @GeneratedValue(generator = "system-uuid") @GenericGenerator(name = "system-uuid", strategy = "uuid") private String id; private String username; private String content; private Date creationDate; @ManyToOne(optional = false, cascade = { CascadeType.MERGE, CascadeType.PERSIST }) private Forum forum; public Post() { creationDate = new Date(); } @PrePersist private void onPersist() { System.out.println(getClass().getName() + ": onPersist"); if (creationDate == null) { creationDate = new Date(); } forum.setLastActivity(creationDate); } @PreUpdate private void onUpdate() { forum.setLastActivity(new Date()); } </code></pre> <p>If I try adding new posts to forum entity, <code>lastActivity</code> field is correctly updated in database by <code>@PrePersist</code> callback. But if I try to update post entity using following code:</p> <pre><code> entityManager.getTransaction().begin(); Post post = entityManager.find(Post.class, "postId"); post.setContent("New post text"); entityManager.merge(post); entityManager.getTransaction().commit(); </code></pre> <p>only post data are updated and <code>lastActivity</code> field value doesn't change. In my opinion <code>@PreUpdate</code> method should do the trick and update Forum entity. Is this a bug or am I missing something?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload