Skip to content

Instantly share code, notes, and snippets.

@lgueye
Created February 5, 2012 12:44
Show Gist options
  • Save lgueye/1745351 to your computer and use it in GitHub Desktop.
Save lgueye/1745351 to your computer and use it in GitHub Desktop.
Configure hibernate listeners prior to hibernate 4.0.0.Final
/**
* @author [email protected]
*/
@Component
public class HibernateListenersConfigurer {
@Autowired
private EntityManagerFactory entityManagerFactory;
@Autowired
private PreInsertEventListener preInsertEventListener;
@Autowired
private PreUpdateEventListener preUpdateEventListener;
@Autowired
private PreDeleteEventListener preDeleteEventListener;
@PostConstruct
public void registerListeners() {
HibernateEntityManagerFactory hibernateEntityManagerFactory = (HibernateEntityManagerFactory) this.entityManagerFactory;
SessionFactoryImpl sessionFactoryImpl = (SessionFactoryImpl) hibernateEntityManagerFactory.getSessionFactory();
sessionFactoryImpl.getEventListeners().setPreInsertEventListeners(
new org.hibernate.event.PreInsertEventListener[] { this.preInsertEventListener });
sessionFactoryImpl.getEventListeners().setPreUpdateEventListeners(
new org.hibernate.event.PreUpdateEventListener[] { this.preUpdateEventListener });
sessionFactoryImpl.getEventListeners().setPreDeleteEventListeners(
new org.hibernate.event.PreDeleteEventListener[] { this.preDeleteEventListener });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment