JPA / Hibernate preUpdate不会更新父对象 [英] JPA/Hibernate preUpdate doesn't update parent object

查看:94
本文介绍了JPA / Hibernate preUpdate不会更新父对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我定义了以下类:

In my application I defined following classes:

@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<Post> posts;







@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());
    }

如果我尝试向论坛实体添加新帖子,通过 @PrePersist 回调,lastActivity 字段在数据库中正确更新。
但是,如果我尝试使用以下代码更新post实体:

If I try adding new posts to forum entity, lastActivity field is correctly updated in database by @PrePersist callback. But if I try to update post entity using following code:

  entityManager.getTransaction().begin();
  Post post = entityManager.find(Post.class, "postId");
  post.setContent("New post text");
  entityManager.merge(post);
  entityManager.getTransaction().commit();

只有发布数据已更新, lastActivity 字段值不会改变。在我看来, @PreUpdate 方法应该做到这一点,并更新论坛实体。
这是一个错误还是我错过了什么?

only post data are updated and lastActivity field value doesn't change. In my opinion @PreUpdate method should do the trick and update Forum entity. Is this a bug or am I missing something?

推荐答案

这不是bug,即使是快速尝试这对我的工作方式,你的预期。消极的消息是,它不能保证工作,因为:

It is not bug, even that with a fast try this worked for me way you expected. Negative news is that it is not guaranteed to work, because of:

从JPA 2.0规范的第93页开始:

From page 93 in JPA 2.0 specification:

通常,可移植应用程序的生命周期方法不应
调用EntityManager或Query操作,访问其他实体
实例或修改相同持久性$ b $内的关系b背景。[43]生命周期回调方法可能会修改其被调用的实体的
非关系状态。

In general, the lifecycle method of a portable application should not invoke EntityManager or Query operations, access other entity instances, or modify relationships within the same persistence context.[43] A lifecycle callback method may modify the non-relationship state of the entity on which it is invoked.

和第95页:

And page 95:


与实现有关,回调方法在生命周期事件级联之前还是之后调用

相关实体。应用程序不应该依赖于此顺序。

It is implementation-dependent as to whether callback methods are invoked before or after the cascading of the lifecycle events to related entities. Applications should not depend on this ordering.

这篇关于JPA / Hibernate preUpdate不会更新父对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆