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>
运行应用程序时出现异常。