创建Envers自定义修订实体

时间:2011-11-23 12:51:06

标签: entity revision hibernate-envers

我正在尝试为我们的项目设置审核。 我从默认配置开始工作正常。

下一步是存储已进行更改的用户。 按照手册我创建了自定义实体修订:

package com.csbi.samples.utils.audit;

import java.io.Serializable;
import java.text.DateFormat;
import java.util.Date;

import org.hibernate.envers.RevisionNumber;
import org.hibernate.envers.RevisionTimestamp;
import org.hibernate.envers.RevisionEntity;

import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name="REVISIONS")
@RevisionEntity(CustomRevisionListener.class)
public class CustomRevisionEntity implements Serializable {
private static final long serialVersionUID = -1255842407304508513L;

@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;
}

public boolean equals(Object o) {
    if(this == o) return true;
    if(!(o instanceof CustomRevisionEntity)) return false;

    CustomRevisionEntity that = (CustomRevisionEntity) o;

    if(id != that.id) return false;
    if(timestamp != that.timestamp) return false;
    if(timestamp != that.timestamp) return false;
    if(username != that.username) return false;

    return true;
}

public int hashCode() {
    int result;
    result = id;
    result = 31 * result + (int) (timestamp ^ (timestamp >>> 32));
    return result;
}

public String toString() {
    return "DefaultRevisionEntity(user = " + username + "id = " + id + ", revisionDate = " + DateFormat.getDateTimeInstance().format(getRevisionDate()) + ")";
}

}

还有自定义监听器:

package com.csbi.samples.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
}

}

以下是日志中的一些行:

  

DEBUG:org.hibernate.envers.configuration.metadata.AuditMetadataGenerator -   为实体生成首次审核映射   com.csbi.samples.domain.Property。
  DEBUG:   org.hibernate.envers.configuration.metadata.AuditMetadataGenerator -   为实体生成第二遍审计映射   com.csbi.samples.domain.Property。
  信息:org.hibernate.cfg.HbmBinder    - 映射类:com.csbi.samples.domain.Property_AUD - > PROPERTIES_AUD
  INFO:org.hibernate.cfg.HbmBinder - Mapping类:   org.hibernate.envers.DefaultRevisionEntity - > REVINFO

看一下输出的最后一行。 仍有DefaultRevisionEntity映射而不是CustomRevisionEntity

我不知道出了什么问题。有什么建议?

1 个答案:

答案 0 :(得分:2)

解决。 Hibernate目录不扫描实体。