正如主题所说,我的注释Hibernate回调方法在它们应该的时候不会触发。这是我的配置,也许有人可以建议我缺少什么。我试图遵循基本指令here,虽然他们的示例代码中似乎有一个拼写错误(“Audit.class”???)虽然我们没有正式使用JPA注释(而是使用hbm。 xml文件),它们应该仍然可以hibernate-entitymanager
正确吗?
Java对象:
@Entity
@EntityListeners(Objectlistener.class)
public class Bannedword implements java.io.Serializable {
private Date createddate;
private Date lastmodifieddate;
/**/
public Date getCreateddate() {
return createddate;
}
public void setCreateddate(Date createddate) {
this.createddate = createddate;
}
public Date getLastmodifieddate() {
return lastmodifieddate;
}
public void setLastmodifieddate(Date lastmodifieddate) {
this.lastmodifieddate = lastmodifieddate;
}
}
对象监听器:
public class Objectlistener {
/**
* automatic property set before any database persistence
*/
@PreUpdate
public void setLastmodifieddate(Bannedword word) {
System.out.println("Object listner!");
word.setLastmodifieddate(new Date());
}
@PrePersist
public void setCreateddate(Bannedword word){
System.out.println("Object listner!");
word.setCreateddate(new Date());
}
}
Bannedword.hbml.xml (我意识到createddate和lastmodifieddate)尚未映射 - 但我认为他们不需要为了触发触发器而触发?
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Oct 14, 2013 5:24:09 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="objects.Bannedword" table="bannedword">
<id name="id" type="string">
<column name="id" />
<generator class="uuid2" />
</id>
<many-to-one name="client" class="objects.Client" fetch="select">
<column name="clientid" />
</many-to-one>
<property name="word" type="string">
<column name="word" />
</property>
</class>
</hibernate-mapping>
与Hibernate相关的 POM.xml 条目:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>4.2.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.0.CR1</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search</artifactId>
<version>4.4.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.2.2.Final</version>
最后是 sessionfactory 的内容,我尝试根据阅读here
添加反思管理器的内容private SessionFactory configureSessionFactory() throws HibernateException{
Configuration configuration = new Configuration().configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
ReflectionManager reflectionManager = configuration.getReflectionManager();
// EntityCallbackHandler callbackHandler = new EntityCallbackHandler();
// callbackHandler.add(Bannedword.class, null, null);
// EventListenerRegistry registry = sessionFactory.getServiceRegistry().getService(EventListenerRegistry.class);
//
return sessionFactory;
}