本文介绍了Hibernate使用自定义修订实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试扩展我的修订表。按照手册我创建了自定义修订实体:
I'm trying to extend my revision table. Following the manual I created custom revision entity:
package com.terminal.audit;
import org.hibernate.envers.DefaultRevisionEntity;
import org.hibernate.envers.RevisionEntity;
import org.hibernate.envers.RevisionNumber;
import org.hibernate.envers.RevisionTimestamp;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Transient;
import java.util.Date;
@Entity
@RevisionEntity(CustomRevisionListener.class)
public class CustomRevisionEntity extends DefaultRevisionEntity {
@Id
@GeneratedValue
@RevisionNumber
private int id;
@RevisionTimestamp
private long timestamp;
private String username;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Transient
public Date getRevisionDate() {
return new Date(timestamp);
}
public long getTimestamp() {
return timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
和自定义修订版监听器:
and custom revision listener:
package com.terminal.audit;
import org.hibernate.envers.RevisionListener;
public class CustomRevisionListener implements RevisionListener {
public void newRevision(Object revisionEntity) {
CustomRevisionEntity revision = (CustomRevisionEntity) revisionEntity;
revision.setUsername("username"); //for testing
}
}
我的hibernate envers版本如下:
My hibernate envers version is the following:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
<version>3.6.10.Final</version>
</dependency>
问题是我的修订实体未被使用。什么可能是错误的建议?
The problem is my revision entity is not being used. Any suggestions what could possibly be wrong?
推荐答案
问题在于我的hibernate配置中没有映射自定义实体。
The problem was with custom entity not mapped in my hibernate configuration.
<mapping class="com.terminal.audit.CustomRevisionEntity"/>
已解决)
这篇关于Hibernate使用自定义修订实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!