创建 Envers 自定义修订实体

发布于 2024-12-17 12:58:45 字数 3052 浏览 3 评论 0原文

我正在尝试为我们的项目设置审核。 我从默认配置开始,效果很好。

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

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
}

}

以下是日志中的一些行:

调试:org.hibernate.envers.configuration.metadata.AuditMetadataGenerator - 为实体生成首次通过审核映射 com.csbi.samples.domain.Property。
调试: org.hibernate.envers.configuration.metadata.AuditMetadataGenerator - 为实体生成第二遍审核映射 com.csbi.samples.domain.Property。
信息:org.hibernate.cfg.HbmBinder - 映射类:com.csbi.samples.domain.Property_AUD -> PROPERTIES_AUD
信息:org.hibernate.cfg.HbmBinder - 映射类: org.hibernate.envers.DefaultRevisionEntity ->更新信息

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

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

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()) + ")";
}

}

And also custom listener:

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:

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

↘人皮目录ツ 2024-12-24 12:58:45

解决了。 Hibernate 目录未扫描实体。

Solved. Entity is not in scanned by Hibernate directory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文