在自动触发的默认侦听器之前触发Hibernate自定义事件侦听器 [英] Fire Hibernate custom event listener before auto-triggered default listeners

查看:128
本文介绍了在自动触发的默认侦听器之前触发Hibernate自定义事件侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个定制的Hibernate Event侦听器,用于扩展org.hibernate.event.PreInsertEventListener。
自定义侦听器重写onPreInsert方法,并在将其保存在带有DAO的数据库之前设置Contact实体的字段。



问题是,字段是null在侦听器给它一个值之前,默认的hibernate事件侦听器在我的自定义侦听器之前自动触发。当他们检查ddl时,他们看到字段上的非空约束并在让我的自定义事件侦听器给该字段赋值之前抛出空检查异常。 (使用spring AOP而不是hibernate自定义侦听器时会发生同样的问题:默认的hibernate侦听器在我的aspect方法之前执行)

因此,可以调整触发顺序的Hibernate侦听器知道我使用了Spring会话工厂?



谢谢

解决方案

我有一个为你创建个人实体数据记录的例子。希望它有帮助。



我必须制作一个辅助界面

<$ p $公共接口DataHistoryAware {

public DataHistory getDataHistory();

public void setDataHistory(DataHistory dataHistory);

这是侦听器实现

  public class DataHistoryListener {
@PrePersist
public void setCreateDataHistory(DataHistoryAware model){
// set creationDate
DataHistory dataHistory = model.getDataHistory()== null?新的DataHistory():model.getDataHistory();
dataHistory.setCreationDate(new Date());
model.setDataHistory(dataHistory);
}
@PostUpdate
public void setUpdateDataHistory(DataHistoryAware model){
DataHistory dataHistory = model.getDataHistory()== null?新的DataHistory():model.getDataHistory();
dataHistory.setLastModificationDate(new Date());
model.setDataHistory(dataHistory);


个人实体

  @Entity 
@EntityListeners(DataHistoryListener.class)
@Table(name =person)
public class Person实现Serializable,DataHistoryAware {

@Column(name =full_name,length = 255,nullable = false)
private String fullName;

@OneToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name =data_history_id,nullable = false)
private DataHistory dataHistory;

public String getFullName(){
return this.fullName;
}

public void setFullName(String fullName){
this.fullName = fullName;
}
public DataHistory getDataHistory(){
return dataHistory;
}

public void setDataHistory(DataHistory dataHistory){
this.dataHistory = dataHistory;






这个实现在persist之前为person实体创建一个datahistory记录,并在合并之前更新它。 datahistory属性有一个非空约束,所以这与你的联系实体的非空属性的问题是一样的。希望它有用。


I made a custom Hibernate Event listener extending org.hibernate.event.PreInsertEventListener. The custom listener overrides onPreInsert method and sets a field of a "Contact" entity before saving it in DB with a DAO.

Problem is, the field was null before the listener gives it a value and default hibernate event listeners are automatically triggered BEFORE my custom listener. As they check the ddl, they see the not-null constraint on the field and throw a null check exception before letting my custom event listener give the field its value. (same problem happens when using spring AOP instead of hibernate custom listeners: the default hibernate listener is executed before my aspect method)

So, it is possible to tune the triggering order of the hibernate listeners knowing that I use a spring session factory ?

Thanks

解决方案

I have an example for you which creates data history for person entity. Hope it helps.

I had to make a helper interface:

public interface DataHistoryAware {

    public DataHistory getDataHistory();

    public void setDataHistory(DataHistory dataHistory);
}

This is the listener implementation:

public class DataHistoryListener {
   @PrePersist
    public void setCreateDataHistory(DataHistoryAware model) {
        //set creationDate
        DataHistory dataHistory = model.getDataHistory() == null ? new DataHistory() : model.getDataHistory();
        dataHistory.setCreationDate(new Date());
        model.setDataHistory(dataHistory); 
    }
@PostUpdate
    public void setUpdateDataHistory(DataHistoryAware model) {
        DataHistory dataHistory = model.getDataHistory() == null ? new DataHistory() : model.getDataHistory();
        dataHistory.setLastModificationDate(new Date());
        model.setDataHistory(dataHistory);
    }
}

The person entity:

@Entity
@EntityListeners(DataHistoryListener.class)
@Table(name="person")
public class Person implements Serializable, DataHistoryAware {

@Column(name = "full_name", length = 255, nullable = false)
    private String fullName;

@OneToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "data_history_id", nullable = false)
private DataHistory dataHistory;

public String getFullName() {
        return this.fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }
public DataHistory getDataHistory() {
        return dataHistory;
    }

    public void setDataHistory(DataHistory dataHistory) {
        this.dataHistory = dataHistory;
    }
}

This implementation creates a datahistory record for person entity before persist, and will update it before merge. The datahistory property has a not-null constraint, so this is the same thing like your problem with the not-null property of the contact entity. Hope it was useful.

这篇关于在自动触发的默认侦听器之前触发Hibernate自定义事件侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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