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



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

@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"one_answers"lastUpdated"。这些属性被放置在映射的超类UUIDBase中。当实体被持久化或更新时,@PrePersist和@PreUpdate回调中的两个字段都将被更新。除级联情况外,此操作效果良好。

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

应该调用它们。请确保您正确注册了回调。

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

你确定没有调用回调,还是只是没有更新值?

如果你直接调用persistent上的配置文件,回调是调用的吗?

如何注册回调?

感谢您的回答。我想我现在已经修好了。我在上文中描述了"账户"one_answers"档案"这两个实体。我上面的描述中缺少实体"雇员"。这是映射:

@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
}

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

最新更新