【发布时间】:2017-01-04 08:53:16
【问题描述】:
我使用纯 java 来休眠独立应用程序和 Hibernate Envers 以获取对表列所做更改的更新,我使用 sql server 作为我的数据库,我是 envers 的新手。
这是我的“CustomRevisionEntity.java”
@Entity
@AuditTable("REVINFO")
@RevisionEntity(CustomRevisionListener.class)
public class CustomRevisionEntity {
@Column (name = "USERNAME", length = 50)
private String username;
@Id
@GeneratedValue
@RevisionNumber
@Column (name = "REV", unique = true, nullable = false)
private int id;
@Temporal(TemporalType.DATE)
@Column (name = "REVTSTMP", nullable = false, length = 15)
@RevisionTimestamp
private Date timestamp;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Date getTimestamp() {
return timestamp;
}
public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
CustomRevisionListener.java
public class CustomRevisionListener implements RevisionListener {
public void newRevision(Object revisionEntity) {
CustomRevisionEntity revision = (CustomRevisionEntity) revisionEntity ;
String userName = Hibernate_Connection.getloggedUser();
revision.setUsername(userName);
}
}
Hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;instance=SQLEXPRESS_2012;DatabaseName=ETS_V11_DEV;integratedSecurity=true</property> -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> -->
<property name="show_sql">true</property>
<property name="use_sql_comments">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping class= "Domain_hibernate_SQLServer.Domain"/>
<mapping class= "Domain_hibernate_SQLServer.CustomRevisionEntity"/>
</session-factory>
</hibernate-configuration>
问题:使用 hbm.xml 文件时,它会在用户名列上添加值, 但是当我使用注释来获取值时,时间取空值,因为它没有识别我添加的额外列属性,但是 使用注解时,会在用户名列中插入空值
在控制台上查看 sql 代码时,它会采用这样的值和注释
/* insert org.hibernate.envers.DefaultRevisionEntity
*/ insert
into
REVINFO
(REVTSTMP)
values
(?)
表只有 3 列,1 列 REV,即自动增量,2 列 REVTSTMP,3 列 USERNAME,它不带用户名, 我错过了什么,如果您需要更多信息,请发表评论
【问题讨论】:
-
已修复,愚蠢的错误,我的 cfg.xml DOCTYPE 有问题
标签: java sql-server hibernate