我正在使用弹簧和休眠。Hibernate 4.2Ejb3Configuration
将类声明为已弃用,并且它没有允许设置侦听器的方法getEventListeners 。这个类用来搞定EntityManagerFactory
。
我用它来进行数据库试听(更新、插入、删除事件的记录)。我应该如何获得带有试听配置的 EntityManagerFactory?
public class HibernateExtendedPersistenceProvider extends HibernatePersistence {
private PostInsertEventListener[] postInsertEventListeners;
private PostUpdateEventListener[] postUpdateEventListeners;
private PostDeleteEventListener[] postDeleteEventListeners;
@SuppressWarnings("rawtypes")
@Override
public EntityManagerFactory createEntityManagerFactory(String persistenceUnitName, Map properties) {
Ejb3Configuration cfg = new Ejb3Configuration();
setupConfiguration(cfg);
Ejb3Configuration configured = cfg.configure( persistenceUnitName, properties );
return configured != null ? configured.buildEntityManagerFactory() : null;
}
@SuppressWarnings("rawtypes")
@Override
public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map properties) {
Ejb3Configuration cfg = new Ejb3Configuration();
setupConfiguration(cfg);
Ejb3Configuration configured = cfg.configure( info, properties );
return configured != null ? configured.buildEntityManagerFactory() : null;
}
private void setupConfiguration(Ejb3Configuration cfg) {
// 4.2: Ejb3Configuration deprecated and has no method getEventListeners
cfg.getEventListeners().setPostInsertEventListeners(postInsertEventListeners);
cfg.getEventListeners().setPostDeleteEventListeners(postDeleteEventListeners);
cfg.getEventListeners().setPostUpdateEventListeners(postUpdateEventListeners);
}
public void setPostInsertEventListeners(PostInsertEventListener[] postInsertEventListeners) {
this.postInsertEventListeners = postInsertEventListeners;
}
public void setPostUpdateEventListeners(PostUpdateEventListener[] postUpdateEventListeners) {
this.postUpdateEventListeners = postUpdateEventListeners;
}
public void setPostDeleteEventListeners(PostDeleteEventListener[] postDeleteEventListeners) {
this.postDeleteEventListeners = postDeleteEventListeners;
}
}