创建Envers自定义修订版实体 [英] Creating Envers custom revision entity

查看:103
本文介绍了创建Envers自定义修订版实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to setup audit for our project. I started from the default configuration which works fine.

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

The next step is to store the user which has made changes. Following the manual I created custom entity revision:

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
}

}

这里有一些日志中的行:

Here is some lines from log:


调试:org.hibernate.envers.configuration.metadata.AuditMetadataGenerator-
生成首过审核映射对于实体
com.csbi.samples.domain.Property。
调试:
org.hibernate.envers.configuration.metadata.AuditMetadataGenerator-
生成第二次审核实体
的映射com.csbi.samples.domain.Property。

INFO:org.hibernate.cfg.HbmBinder
-映射类:com.csbi.samples.domain.Property_AUD -> PROPERTIES_AUD

信息:org.hibernate.cfg.HbmBinder-映射类:
org.hibernate.envers.DefaultRevisionEntity-> REVINFO

DEBUG: org.hibernate.envers.configuration.metadata.AuditMetadataGenerator - Generating first-pass auditing mapping for entity com.csbi.samples.domain.Property.
DEBUG: org.hibernate.envers.configuration.metadata.AuditMetadataGenerator - Generating second-pass auditing mapping for entity com.csbi.samples.domain.Property.
INFO : org.hibernate.cfg.HbmBinder - Mapping class: com.csbi.samples.domain.Property_AUD -> PROPERTIES_AUD
INFO : org.hibernate.cfg.HbmBinder - Mapping class: org.hibernate.envers.DefaultRevisionEntity -> REVINFO

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

Take a look at the last line of the output. There is still DefaultRevisionEntity mapped instead of CustomRevisionEntity.

我不知道怎么了。有什么建议么?

I have no idea what is wrong. Any suggestions?

推荐答案

已解决。实体不在通过Hibernate目录扫描。

Solved. Entity is not in scanned by Hibernate directory.

这篇关于创建Envers自定义修订版实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆