为什么审计会返回null版本

时间:2017-05-28 09:31:50

标签: java hibernate maven hibernate-envers

像这样的pom:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>a</groupId>
    <artifactId>a</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <exec.mainClass>Test</exec.mainClass>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-envers</artifactId>
            <version>4.3.10.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <version>2.3.2</version>
        </dependency>
    </dependencies>
</project>

像这样的主要类/实体:

import java.util.Calendar;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Version;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.dialect.HSQLDialect;
import org.hibernate.envers.AuditReader;
import org.hibernate.envers.AuditReaderFactory;
import org.hibernate.envers.Audited;
import org.hibernate.internal.SessionFactoryImpl;

@Entity
@Audited
public class Test {
    @SuppressWarnings("deprecation")
    public static void main(String[] args) {
        Configuration c = new Configuration();
        c.setProperty(Environment.DIALECT, HSQLDialect.class.getCanonicalName());
        c.setProperty(Environment.URL, "jdbc:hsqldb:mem:.");
        c.setProperty("hibernate.connection.username", "sa");
        c.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
        c.addAnnotatedClass(Test.class);
        SessionFactoryImpl sf = (SessionFactoryImpl) c.buildSessionFactory();
        Session s = sf.openSession();

        Transaction insert = s.beginTransaction();
        Test e = new Test();
        e.text = "Hello World!";
        e.id = 1;
        s.save(e);
        s.flush();
        System.out.println(e.version.getTimeInMillis());
        insert.commit();

        Transaction update = s.beginTransaction();
        e.text = "Foobar";
        s.persist(e);
        s.flush();
        System.out.println(e.version.getTimeInMillis());
        update.commit();

        AuditReader r = AuditReaderFactory.get(s);
        System.out.println(r.find(Test.class, e.id, 1).version);
        System.out.println(r.find(Test.class, e.id, 2).version);

        sf.getServiceRegistry().destroy();
    }

    @Id
    Integer id;

    @Version
    Calendar version;

    @Column
    String text;
}

我得到了这个输出:

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building a 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ a ---
[INFO] Deleting C:\Users\devel\workspace\a\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ a ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\devel\workspace\a\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ a ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 1 source file to C:\Users\devel\workspace\a\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ a ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\devel\workspace\a\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ a ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ a ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ a ---
[INFO] Building jar: C:\Users\devel\workspace\a\target\a-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ a ---
[INFO] Installing C:\Users\devel\workspace\a\target\a-0.0.1-SNAPSHOT.jar to C:\Users\devel\.m2\repository\a\a\0.0.1-SNAPSHOT\a-0.0.1-SNAPSHOT.jar
[INFO] Installing C:\Users\devel\workspace\a\pom.xml to C:\Users\devel\.m2\repository\a\a\0.0.1-SNAPSHOT\a-0.0.1-SNAPSHOT.pom
[INFO]
[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ a ---
Mai 28, 2017 11:27:06 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
Mai 28, 2017 11:27:06 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.10.Final}
Mai 28, 2017 11:27:06 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Mai 28, 2017 11:27:06 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Mai 28, 2017 11:27:06 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Mai 28, 2017 11:27:06 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [null] at URL [jdbc:hsqldb:mem:.]
Mai 28, 2017 11:27:06 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=sa}
Mai 28, 2017 11:27:06 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
Mai 28, 2017 11:27:06 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Mai 28, 2017 11:27:06 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.HSQLDialect
Mai 28, 2017 11:27:06 AM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
Mai 28, 2017 11:27:06 AM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Mai 28, 2017 11:27:07 AM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000227: Running hbm2ddl schema export
Mai 28, 2017 11:27:07 AM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: alter table Test_AUD drop constraint FK_p3jg7xwj0p2hij6o3otnb60uj
Mai 28, 2017 11:27:07 AM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: user lacks privilege or object not found: PUBLIC.TEST_AUD
Mai 28, 2017 11:27:07 AM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete
1495963627299
1495963627315
null
null
Mai 28, 2017 11:27:07 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH000030: Cleaning up connection pool [jdbc:hsqldb:mem:.]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.438 s
[INFO] Finished at: 2017-05-28T11:27:07+02:00
[INFO] Final Memory: 27M/435M
[INFO] ------------------------------------------------------------------------

为什么版本字段在旧版本中为空?

1 个答案:

答案 0 :(得分:2)

由于org.hibernate.envers.do_not_audit_optimistic_locking_field,这是预期的。

默认情况下,Envers将此属性设置为true,因此实体上任何带注释的@Version属性都不会被审核,也不会包含在审计表模式中,因此将为null从envers历史表中获取审计实体时。

如果您还希望审核@Version带注释的字段,请在休眠配置中将提及的属性更改为false。但是,这样做意味着需要进行架构更新,旧的历史记录将继续报告空值。