CascadeType.ALL 和生命周期回调的问题

发布于 2024-11-15 15:28:44 字数 653 浏览 10 评论 0原文

我在存储这个简单的映射时遇到一些问题:

@Entity
public class Account extends UUIDBase {
    private Profile profile;

    @OneToOne(cascade = CascadeType.ALL, optional = false)
    public Profile getProfile() {
       return profile;
    }

    public void setProfile(Profile profile) {
        this.profile = profile;
    }
}

@Entity
public class Profile extends UUIDBase {
  ...
}

我们的实体具有所有属性“creationDate”和“lastUpdated”。这些属性放置在映射的超类 UUIDBase 中。当实体被持久化或更新时,两个字段都将在 @PrePersist 和 @PreUpdate 回调中更新。除了级联的情况之外,这工作得很好。

当我们存储帐户时,个人资料也将始终被存储。账户的creationDate和lastUpdated属性将通过回调方法初始化。配置文件的回调方法将不会被调用。你有提示出什么问题了吗?

I have some problems with storing this simple mapping:

@Entity
public class Account extends UUIDBase {
    private Profile profile;

    @OneToOne(cascade = CascadeType.ALL, optional = false)
    public Profile getProfile() {
       return profile;
    }

    public void setProfile(Profile profile) {
        this.profile = profile;
    }
}

@Entity
public class Profile extends UUIDBase {
  ...
}

Our entities have all the attribute "creationDate" and "lastUpdated". These attributes are placed in the mapped superclass UUIDBase. When a entity is persisted or updated both fields will be updated in @PrePersist and @PreUpdate callback. This works fine except in the case of cascading.

When we store the Account the Profile will always be stored, too. The creationDate and lastUpdated attribute of the account will be initialized through the callback methods. The callbacks methods for the Profile will not be called. Do you have a hint what´s going wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

相思故 2024-11-22 15:28:44

他们应该被召唤。确保您正确注册回调。

MappedSuperclass 上的回调未被调用的问题最近已修复,因此您可能需要在 2.2 中将回调添加到子类。

您确定回调没有被调用,或者值只是没有更新?

如果直接在配置文件上调用 persist 会调用回调吗?

如何注册回调?

They should be called. Ensure that you are registering the callbacks correctly.

There was an issue with callbacks on a MappedSuperclass not getting called that was fixed recently, so you may need to add the callbacks to the subclass in 2.2.

Are you sure the callbacks are not called, or do the values just not get updated?

If you directly call persist on the profile is the callback called?

How do you register the callback?

别想她 2024-11-22 15:28:44

谢谢您的回答。我想我现在修好了。我在上面描述了两个实体“帐户”和“个人资料”。我上面的描述中缺少实体“员工”。这是映射:

@Entity
public class Employee extends UUIDBase {
    public Account account;

    @OneToOne(cascade = {CascadeType.REFRESH, CascadeType.REMOVE}, orphanRemoval = true)
    public Account getAccount() {
        return this.account;
    }

    public void setAccount(Account account) { 
        this.account = account;
    } 

}

映射是“员工可以有一个帐户。帐户必须有一个个人资料”。问题出在服务类中:

public void saveEmployee(Employee data) {
    Employee savedEmployee = empDao.saveEmployee(data);
    accountService.saveAccount(data.getAccount()); <-- Here is the failure
}

首先我保存员工并取回保存的员工对象。之后,我尝试通过其自己的服务保存员工帐户。当我考虑保存的员工对象时,一切正常并且回调被调用。当我从“data”参数获取帐户时,不会调用回调。

Thank you for your answer. I think I fixed it now. I described the two entities "Account" and "Profile" above. The entity "Employee" is missing in my description above. Here is the mapping:

@Entity
public class Employee extends UUIDBase {
    public Account account;

    @OneToOne(cascade = {CascadeType.REFRESH, CascadeType.REMOVE}, orphanRemoval = true)
    public Account getAccount() {
        return this.account;
    }

    public void setAccount(Account account) { 
        this.account = account;
    } 

}

The mapping is "A employee could have a account. A account must have a profile". The problem is in the service class:

public void saveEmployee(Employee data) {
    Employee savedEmployee = empDao.saveEmployee(data);
    accountService.saveAccount(data.getAccount()); <-- Here is the failure
}

First I save the employee and get the saved employee object back. After that I try to save the employees account through its own service. When I take the account of the saved employee object everything works and the callbacks are called. When I take the account from the "data" parameter the callbacks are not called.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文