Hibernate包含自定义修订实体

时间:2015-04-23 13:58:43

标签: java hibernate orm audit hibernate-envers

我正在尝试扩展我的修订表。按照手册我创建了自定义修订实体:

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

和自定义修订监听器:

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版本如下:

<dependency>
     <groupId>org.hibernate</groupId>
     <artifactId>hibernate-envers</artifactId>
     <version>3.6.10.Final</version>
</dependency>

问题是我的修订实体未被使用。有什么建议可能是错的吗?

1 个答案:

答案 0 :(得分:1)

问题在于我的休眠配置中没有映射自定义实体。

<mapping class="com.terminal.audit.CustomRevisionEntity"/>

解决)