无法在数据库中生成审核表

时间:2019-05-19 08:52:12

标签: java hibernate audit-tables

package com.ct.org.dto;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.hibernate.envers.Audited;
@Entity
@Audited
public class UserDetails {
@Id
private int userId;
private String userName;
public int getUserId() {
    return userId;
}
public void setUserId(int userId) {
    this.userId = userId;
}
public String getUserName() {
    return userName;
}
public void setUserName(String userName) {
    this.userName = userName;
}
 }

package com.ct.org.client;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory; 
import org.hibernate.cfg.Configuration;
import org.hibernate.envers.AuditReader;
import org.hibernate.envers.AuditReaderFactory;
import com.ct.org.dto.UserDetails;

 public class HibernateTest {
   public static void main(String[] args) {

    UserDetails user = new UserDetails();
    user.setUserId(1);
    user.setUserName("waseem");


    SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();


    session.save(user);

    session.close();

    Session session2 = sessionFactory.openSession();
    UserDetails userObject = (UserDetails) session2.get(UserDetails.class, 1);

    session2.beginTransaction();
    userObject.setUserName("FT");
    session2.update(userObject);
    session2.getTransaction().commit();
    session2.close();

    AuditReader auditReader = AuditReaderFactory.get(session2);

    List<Number> revisionNumbers = auditReader.getRevisions(UserDetails.class, userObject.getUserId());

    for (Number rev : revisionNumbers) {
        UserDetails auditedUser = auditReader.find(UserDetails.class, userObject.getUserId(), rev);
    }

}

}

<session-factory>
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/ct``</property>
    <property name="connection.username">root</property>
    <property name="connection.password">root</property>

    <property name="connection.pool_size">1</property>


<!-- Depending on what we configure model objects it will create a schema 
    which requires to save that model object -->
<property name="hbm2ddl.auto">update</property>

<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- Dialect is class. What kind of language we are going to use to connect 
    to database so we can use Dialect -->

<property name="show_sql">true</property>


    <mapping class="com.ct.org.dto.UserDetails" />

</session-factory>

运行应用程序时出现异常。

  1. 休眠:从UserDetails userdetail0_中选择userdetail0_.userId作为userId0_0_,userdetail0_.userName作为userName0_0_,其中userdetail0_.userId =? 线程“主”中的异常org.hibernate.envers.exception.AuditException:您需要安装org.hibernate.envers.event.AuditEventListener类作为后插入,更新和删除事件侦听器。 在org.hibernate.envers.AuditReaderFactory.get(AuditReaderFactory.java:71) 在com.kpit.org.client.HibernateTest.main(HibernateTest.java:61)

0 个答案:

没有答案