Hibernate Firefighter custom event listener before default startup listeners

I created a custom Hibernate event listener extending org.hibernate.event.PreInsertEventListener. The user listener overrides the PreInsert method and sets the Contact object field before saving it to the database using the DAO.

The problem is that the field was null before the listener gave it a value, and by default, event listeners in sleep mode automatically start before my user listener. When they check the ddl, they see a non-empty constraint in the field and throw a null check exception before letting my custom event listener specify the field with its value. (the same problem occurs when using spring AOP instead of hibernate custom listeners: by default, the hibernate listener runs before my method aspect)

So you can configure the startup order of the sleep listeners, knowing that I am using a spring factory session?

thanks

+8
java spring database listener hibernate
source share
2 answers

I have an example for you that creates a data history for a face. Hope this helps.

I had to create a helper interface :

public interface DataHistoryAware { public DataHistory getDataHistory(); public void setDataHistory(DataHistory dataHistory); } 

This is a 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); } } 

Face Object:

 @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 data history record for the person before it is saved and updates it before merging. The datahistory property has a non-null constraint, so this is the same as your problem with a non-empty contact object property. Hope this was helpful.

+1
source share

IMHO, what you do is too great. You can achieve the same as Interceptor.

If you already know this and prefer to override PreInsetEventListener for some reason (interesting to know), then you need to start by overriding the standard implementations of org.hibernate.event.def.AbstractSaveEventListener. For example, org.hibernate.event.def.DefaultSaveEventListener.

See that the failure constraint check and the foreign key check occur in "AbstractSaveEventListener.performSaveOrReplicate" and EntityActions are added to the action queue. These actions are performed during the flush session, and this happens when you receive a call to your PreInsertEventListener from EntityActions.

+1
source share

All Articles