小编典典

onSave()(适用于使用Hibernate / Spring数据存储库保存的任何实体)

java

如果我的实体已计算字段,应先保存字段,然后再保存到数据库(db insertupdate),如何在Hibernate或Spring Data
Repository之前挂接方法调用save()


阅读 177

收藏
2020-11-16

共1个答案

小编典典

我认为最适合您的方法是EntityListener使用@PrePersist@PreUpdate注释,为实体侦听器创建配置,然后您将可以访问要保存的每个实例,每次尝试保留或更新内容时都会调用此方法。休眠或春季数据存储库

public class EntityToPersistListener{

   @PrePersist
   @PreUpdate
   public void methodExecuteBeforeSave(final EntityToPersist reference) {
      //Make any change to the entity such as calculation before the save process
      reference.setAmount(xxxx)
    }

}

您只需要在实体bean上方添加注释

@Entity
@Table(name = "", schema = "", catalog = "")
@EntityListeners(EntityToPersistListener.class)
public class EntityToPersist implements Serializable {

检查此链接以获取更多参考

2020-11-16