2017-05-28 68 views
1

有這樣的POM:爲什麼審計返回null版

<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

這是由於org.hibernate.envers.do_not_audit_optimistic_locking_field而引起的。

Envers默認情況下,因此任何註解@Version性能上的實體將不會被審覈,將不包括在審覈表模式,設置此歡迎使用屬性來true,當你從獲取的審計機構因此將是無效的envers歷史表。

如果您還想對@Version帶註釋的字段進行審覈,請在您的休眠配置中將上述屬性更改爲false。但是,這樣做確實意味着模式更新是必要的,您的舊歷史記錄將繼續報告空值。

+0

看起來你是Envers的授權人。這個決定(使其默認爲真)不是一個賭博決定。爲什麼低風險的信息會丟失? –

+1

這很可能是通過這種方式與早期版本向後兼容,並且之前的維護人員從未決定改變這種行爲,而是由實現者自行決定。 – Naros