我试图在dropwizard hibernate中使用SaveOrUpdateDateListener扩展DefaultSaveOrUpdateEventListener。当我在Applicaion类的EventListenerGroup中注册它时,如下所示:
@Override
public void run(DFConfiguration configuration,
Environment environment) throws ClassNotFoundException {
//register custom listener into EventListener
SessionFactory sessionFactory = hibernate.getSessionFactory();
EventListenerRegistry listenerRegistry = sessionFactory.getSessionFactoryOptions().getServiceRegistry().getService(EventListenerRegistry.class);
EventListenerGroup<SaveOrUpdateEventListener> evlg = listenerRegistry.getEventListenerGroup(EventType.SAVE_UPDATE);
evlg.appendListener(new SaveOrUpdateDateListener());
}
这是SaveOrUpdateDateListener类。 公共类SaveOrUpdateDateListener扩展DefaultSaveOrUpdateEventListener {
@Override
public void onSaveOrUpdate(SaveOrUpdateEvent event) {
logger.debug("Entering onSaveOrUpdate()");
if (event.getObject() instanceof AuditableBase) {
AuditableBase object = (AuditableBase) event.getObject();
// set the Updated date/time
object.setUpdated(new Date().getTime());
// set the Created date/time
if (object.getCreated() == 0) {
object.setCreated(new Date().getTime());
}
}
}
}
它在部署时抛出异常:
请求未知服务[org.hibernate.event.service.spi.EventListenerRegistry],堆栈跟踪:org.hibernate.service.UnknownServiceException:请求未知服务[org.hibernate.event.service.spi.EventListenerRegistry] 在org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:126)
有什么想法吗?谢谢!
答案 0 :(得分:0)
我找到了办法。因为我处理了createdTime和updatedTime,所以@CreationTimestamp和@UpdateTimestamp为我做了诀窍。所以我根本不需要任何自定义监听器。是啊...
@Column(name="createdTime", nullable = false, insertable = true,updatable = false)
@Temporal(value = TemporalType.TIMESTAMP)
@CreationTimestamp
private Date createdTime;
@Column(name="updatedTime", nullable = false, insertable = true, updatable = true)
@Temporal(value = TemporalType.TIMESTAMP)
@UpdateTimestamp
private Date updatedTime;