All @Version fields are null when used hibernate-envers?












1















There is application spring+jpa+envers(hibernate)
envers needs to save history of entities in special table.



I use 4.0.1.Final hibernate



<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
<version>4.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.0.1.Final</version>
</dependency>


But, when I look in table, all values in Version field are null



My entity is



import lombok.*;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.envers.Audited;

import javax.persistence.*;

@Entity
@Audited
@Table(name = "User", uniqueConstraints = {
@UniqueConstraint(columnNames = { "prKey"})})
@NoArgsConstructor(access = AccessLevel.PUBLIC)
@AllArgsConstructor
@Getter
@Setter
public class User {
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(name = "PR_KEY", unique = true)
private String prKey;

@Column(name = "name", length = 100, unique = false)
private String name;

@Version
private int version;

public User(String name){
this.name = name;
}
}


And when I get entities using audit:



 public List<User> getHistory(String id) {
AuditReader auditReader = AuditReaderFactory.get(entityManagerFactory.createEntityManager());

List<Number> auditVersions = auditReader.getRevisions(User.class, id);
List<User> users = auditVersions.stream().map(item -> auditReader.find(User.class, id, item.intValue())).collect(Collectors.toList());

return extractRiskMetrics(riskMetricRecords);
}


So, my persistence - config is



@Configuration
@EnableTransactionManagement
@EnableJpaAuditing
@EnableJpaRepositories(basePackages = {"persistence"})
@ComponentScan(basePackages = {"persistence", "model"})
public class PersistenceConfig {
private static final String PACKAGE_WITH_JPA_ENTITIES = "persistence";
private final Logger log = Logger.getLogger(getClass());

@Bean
@Resource(type = DataSource.class, lookup = "jdbc/MyDatasource", name = "jdbc/MyDatasource")
public DataSource dataSource() {
final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
dsLookup.setResourceRef(true);
DataSource dataSource = dsLookup.getDataSource("java:comp/env/jdbc/MyDatasource");
return dataSource;
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();
entityManager.setDataSource(dataSource());
entityManager.setPackagesToScan(PACKAGE_WITH_JPA_ENTITIES);
entityManager.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
entityManager.setJpaProperties(getHibernateProperties());
log.info("Entity Manager configured.");
return entityManager;
}

@Bean
public JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory);
return transactionManager;
}

//Set properties hibernate
private Properties getHibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", "org.hibernate.dialect.OracleDialect");
properties.put("hibernate.show_sql", "true");
properties.put("hibernate.hbm2ddl.auto", "none");
properties.put("org.hibernate.envers.do_not_audit_optimistic_locking_field", false);

properties.put("verifyServerCertificate", false);
properties.put("useSSL", false);
properties.put("requireSSL", false);
properties.put("useLegacyDatetimeCode", false);
properties.put("useUnicode", "yes");
properties.put("characterEncoding", "UTF-8");
properties.put("serverTimezone", "UTC");
properties.put("useJDBCCompliantTimezoneShift", true);
return properties;
}
}


Updates:



org.hibernate.envers.do_not_audit_optimistic_locking_field set to false, but version fields still null.










share|improve this question

























  • At which table do you look? user or user_AUD ?

    – Ralph
    Jan 18 at 17:44











  • @Ralph I look in user_AUD table, because in just user table version is okey(incremented)

    – Vladislav Osipenkov
    yesterday
















1















There is application spring+jpa+envers(hibernate)
envers needs to save history of entities in special table.



I use 4.0.1.Final hibernate



<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
<version>4.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.0.1.Final</version>
</dependency>


But, when I look in table, all values in Version field are null



My entity is



import lombok.*;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.envers.Audited;

import javax.persistence.*;

@Entity
@Audited
@Table(name = "User", uniqueConstraints = {
@UniqueConstraint(columnNames = { "prKey"})})
@NoArgsConstructor(access = AccessLevel.PUBLIC)
@AllArgsConstructor
@Getter
@Setter
public class User {
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(name = "PR_KEY", unique = true)
private String prKey;

@Column(name = "name", length = 100, unique = false)
private String name;

@Version
private int version;

public User(String name){
this.name = name;
}
}


And when I get entities using audit:



 public List<User> getHistory(String id) {
AuditReader auditReader = AuditReaderFactory.get(entityManagerFactory.createEntityManager());

List<Number> auditVersions = auditReader.getRevisions(User.class, id);
List<User> users = auditVersions.stream().map(item -> auditReader.find(User.class, id, item.intValue())).collect(Collectors.toList());

return extractRiskMetrics(riskMetricRecords);
}


So, my persistence - config is



@Configuration
@EnableTransactionManagement
@EnableJpaAuditing
@EnableJpaRepositories(basePackages = {"persistence"})
@ComponentScan(basePackages = {"persistence", "model"})
public class PersistenceConfig {
private static final String PACKAGE_WITH_JPA_ENTITIES = "persistence";
private final Logger log = Logger.getLogger(getClass());

@Bean
@Resource(type = DataSource.class, lookup = "jdbc/MyDatasource", name = "jdbc/MyDatasource")
public DataSource dataSource() {
final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
dsLookup.setResourceRef(true);
DataSource dataSource = dsLookup.getDataSource("java:comp/env/jdbc/MyDatasource");
return dataSource;
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();
entityManager.setDataSource(dataSource());
entityManager.setPackagesToScan(PACKAGE_WITH_JPA_ENTITIES);
entityManager.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
entityManager.setJpaProperties(getHibernateProperties());
log.info("Entity Manager configured.");
return entityManager;
}

@Bean
public JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory);
return transactionManager;
}

//Set properties hibernate
private Properties getHibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", "org.hibernate.dialect.OracleDialect");
properties.put("hibernate.show_sql", "true");
properties.put("hibernate.hbm2ddl.auto", "none");
properties.put("org.hibernate.envers.do_not_audit_optimistic_locking_field", false);

properties.put("verifyServerCertificate", false);
properties.put("useSSL", false);
properties.put("requireSSL", false);
properties.put("useLegacyDatetimeCode", false);
properties.put("useUnicode", "yes");
properties.put("characterEncoding", "UTF-8");
properties.put("serverTimezone", "UTC");
properties.put("useJDBCCompliantTimezoneShift", true);
return properties;
}
}


Updates:



org.hibernate.envers.do_not_audit_optimistic_locking_field set to false, but version fields still null.










share|improve this question

























  • At which table do you look? user or user_AUD ?

    – Ralph
    Jan 18 at 17:44











  • @Ralph I look in user_AUD table, because in just user table version is okey(incremented)

    – Vladislav Osipenkov
    yesterday














1












1








1








There is application spring+jpa+envers(hibernate)
envers needs to save history of entities in special table.



I use 4.0.1.Final hibernate



<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
<version>4.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.0.1.Final</version>
</dependency>


But, when I look in table, all values in Version field are null



My entity is



import lombok.*;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.envers.Audited;

import javax.persistence.*;

@Entity
@Audited
@Table(name = "User", uniqueConstraints = {
@UniqueConstraint(columnNames = { "prKey"})})
@NoArgsConstructor(access = AccessLevel.PUBLIC)
@AllArgsConstructor
@Getter
@Setter
public class User {
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(name = "PR_KEY", unique = true)
private String prKey;

@Column(name = "name", length = 100, unique = false)
private String name;

@Version
private int version;

public User(String name){
this.name = name;
}
}


And when I get entities using audit:



 public List<User> getHistory(String id) {
AuditReader auditReader = AuditReaderFactory.get(entityManagerFactory.createEntityManager());

List<Number> auditVersions = auditReader.getRevisions(User.class, id);
List<User> users = auditVersions.stream().map(item -> auditReader.find(User.class, id, item.intValue())).collect(Collectors.toList());

return extractRiskMetrics(riskMetricRecords);
}


So, my persistence - config is



@Configuration
@EnableTransactionManagement
@EnableJpaAuditing
@EnableJpaRepositories(basePackages = {"persistence"})
@ComponentScan(basePackages = {"persistence", "model"})
public class PersistenceConfig {
private static final String PACKAGE_WITH_JPA_ENTITIES = "persistence";
private final Logger log = Logger.getLogger(getClass());

@Bean
@Resource(type = DataSource.class, lookup = "jdbc/MyDatasource", name = "jdbc/MyDatasource")
public DataSource dataSource() {
final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
dsLookup.setResourceRef(true);
DataSource dataSource = dsLookup.getDataSource("java:comp/env/jdbc/MyDatasource");
return dataSource;
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();
entityManager.setDataSource(dataSource());
entityManager.setPackagesToScan(PACKAGE_WITH_JPA_ENTITIES);
entityManager.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
entityManager.setJpaProperties(getHibernateProperties());
log.info("Entity Manager configured.");
return entityManager;
}

@Bean
public JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory);
return transactionManager;
}

//Set properties hibernate
private Properties getHibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", "org.hibernate.dialect.OracleDialect");
properties.put("hibernate.show_sql", "true");
properties.put("hibernate.hbm2ddl.auto", "none");
properties.put("org.hibernate.envers.do_not_audit_optimistic_locking_field", false);

properties.put("verifyServerCertificate", false);
properties.put("useSSL", false);
properties.put("requireSSL", false);
properties.put("useLegacyDatetimeCode", false);
properties.put("useUnicode", "yes");
properties.put("characterEncoding", "UTF-8");
properties.put("serverTimezone", "UTC");
properties.put("useJDBCCompliantTimezoneShift", true);
return properties;
}
}


Updates:



org.hibernate.envers.do_not_audit_optimistic_locking_field set to false, but version fields still null.










share|improve this question
















There is application spring+jpa+envers(hibernate)
envers needs to save history of entities in special table.



I use 4.0.1.Final hibernate



<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
<version>4.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.0.1.Final</version>
</dependency>


But, when I look in table, all values in Version field are null



My entity is



import lombok.*;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.envers.Audited;

import javax.persistence.*;

@Entity
@Audited
@Table(name = "User", uniqueConstraints = {
@UniqueConstraint(columnNames = { "prKey"})})
@NoArgsConstructor(access = AccessLevel.PUBLIC)
@AllArgsConstructor
@Getter
@Setter
public class User {
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(name = "PR_KEY", unique = true)
private String prKey;

@Column(name = "name", length = 100, unique = false)
private String name;

@Version
private int version;

public User(String name){
this.name = name;
}
}


And when I get entities using audit:



 public List<User> getHistory(String id) {
AuditReader auditReader = AuditReaderFactory.get(entityManagerFactory.createEntityManager());

List<Number> auditVersions = auditReader.getRevisions(User.class, id);
List<User> users = auditVersions.stream().map(item -> auditReader.find(User.class, id, item.intValue())).collect(Collectors.toList());

return extractRiskMetrics(riskMetricRecords);
}


So, my persistence - config is



@Configuration
@EnableTransactionManagement
@EnableJpaAuditing
@EnableJpaRepositories(basePackages = {"persistence"})
@ComponentScan(basePackages = {"persistence", "model"})
public class PersistenceConfig {
private static final String PACKAGE_WITH_JPA_ENTITIES = "persistence";
private final Logger log = Logger.getLogger(getClass());

@Bean
@Resource(type = DataSource.class, lookup = "jdbc/MyDatasource", name = "jdbc/MyDatasource")
public DataSource dataSource() {
final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
dsLookup.setResourceRef(true);
DataSource dataSource = dsLookup.getDataSource("java:comp/env/jdbc/MyDatasource");
return dataSource;
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();
entityManager.setDataSource(dataSource());
entityManager.setPackagesToScan(PACKAGE_WITH_JPA_ENTITIES);
entityManager.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
entityManager.setJpaProperties(getHibernateProperties());
log.info("Entity Manager configured.");
return entityManager;
}

@Bean
public JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory);
return transactionManager;
}

//Set properties hibernate
private Properties getHibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", "org.hibernate.dialect.OracleDialect");
properties.put("hibernate.show_sql", "true");
properties.put("hibernate.hbm2ddl.auto", "none");
properties.put("org.hibernate.envers.do_not_audit_optimistic_locking_field", false);

properties.put("verifyServerCertificate", false);
properties.put("useSSL", false);
properties.put("requireSSL", false);
properties.put("useLegacyDatetimeCode", false);
properties.put("useUnicode", "yes");
properties.put("characterEncoding", "UTF-8");
properties.put("serverTimezone", "UTC");
properties.put("useJDBCCompliantTimezoneShift", true);
return properties;
}
}


Updates:



org.hibernate.envers.do_not_audit_optimistic_locking_field set to false, but version fields still null.







java spring hibernate jpa hibernate-envers






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 10 hours ago







Vladislav Osipenkov

















asked Jan 18 at 12:30









Vladislav OsipenkovVladislav Osipenkov

287215




287215













  • At which table do you look? user or user_AUD ?

    – Ralph
    Jan 18 at 17:44











  • @Ralph I look in user_AUD table, because in just user table version is okey(incremented)

    – Vladislav Osipenkov
    yesterday



















  • At which table do you look? user or user_AUD ?

    – Ralph
    Jan 18 at 17:44











  • @Ralph I look in user_AUD table, because in just user table version is okey(incremented)

    – Vladislav Osipenkov
    yesterday

















At which table do you look? user or user_AUD ?

– Ralph
Jan 18 at 17:44





At which table do you look? user or user_AUD ?

– Ralph
Jan 18 at 17:44













@Ralph I look in user_AUD table, because in just user table version is okey(incremented)

– Vladislav Osipenkov
yesterday





@Ralph I look in user_AUD table, because in just user table version is okey(incremented)

– Vladislav Osipenkov
yesterday












1 Answer
1






active

oldest

votes


















0














Look at configuration setting org.hibernate.envers.do_not_audit_optimistic_locking_field.



This configuration setting controls whether or not Hibernate Envers will include the @Version annotated field in the audit schema or not. By default the setting is set to true which means that the optimistic locking field won't be audited. By setting this to false, you will audit the column's value.



I do want to caution you about setting this field to false.



If your application performs explicit optimistic-locking increment functionality, this will lead to additional rows being added to the audit history table even if none of the other database columns are changed as part of your business process. This is because once you enable @Version fields to be tracked, Hibernate Envers simply treats them as any other basic attribute on the entity. Therefore, forced optimistic-lock increment will trigger an audit change.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54254090%2fall-version-fields-are-null-when-used-hibernate-envers%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Look at configuration setting org.hibernate.envers.do_not_audit_optimistic_locking_field.



    This configuration setting controls whether or not Hibernate Envers will include the @Version annotated field in the audit schema or not. By default the setting is set to true which means that the optimistic locking field won't be audited. By setting this to false, you will audit the column's value.



    I do want to caution you about setting this field to false.



    If your application performs explicit optimistic-locking increment functionality, this will lead to additional rows being added to the audit history table even if none of the other database columns are changed as part of your business process. This is because once you enable @Version fields to be tracked, Hibernate Envers simply treats them as any other basic attribute on the entity. Therefore, forced optimistic-lock increment will trigger an audit change.






    share|improve this answer




























      0














      Look at configuration setting org.hibernate.envers.do_not_audit_optimistic_locking_field.



      This configuration setting controls whether or not Hibernate Envers will include the @Version annotated field in the audit schema or not. By default the setting is set to true which means that the optimistic locking field won't be audited. By setting this to false, you will audit the column's value.



      I do want to caution you about setting this field to false.



      If your application performs explicit optimistic-locking increment functionality, this will lead to additional rows being added to the audit history table even if none of the other database columns are changed as part of your business process. This is because once you enable @Version fields to be tracked, Hibernate Envers simply treats them as any other basic attribute on the entity. Therefore, forced optimistic-lock increment will trigger an audit change.






      share|improve this answer


























        0












        0








        0







        Look at configuration setting org.hibernate.envers.do_not_audit_optimistic_locking_field.



        This configuration setting controls whether or not Hibernate Envers will include the @Version annotated field in the audit schema or not. By default the setting is set to true which means that the optimistic locking field won't be audited. By setting this to false, you will audit the column's value.



        I do want to caution you about setting this field to false.



        If your application performs explicit optimistic-locking increment functionality, this will lead to additional rows being added to the audit history table even if none of the other database columns are changed as part of your business process. This is because once you enable @Version fields to be tracked, Hibernate Envers simply treats them as any other basic attribute on the entity. Therefore, forced optimistic-lock increment will trigger an audit change.






        share|improve this answer













        Look at configuration setting org.hibernate.envers.do_not_audit_optimistic_locking_field.



        This configuration setting controls whether or not Hibernate Envers will include the @Version annotated field in the audit schema or not. By default the setting is set to true which means that the optimistic locking field won't be audited. By setting this to false, you will audit the column's value.



        I do want to caution you about setting this field to false.



        If your application performs explicit optimistic-locking increment functionality, this will lead to additional rows being added to the audit history table even if none of the other database columns are changed as part of your business process. This is because once you enable @Version fields to be tracked, Hibernate Envers simply treats them as any other basic attribute on the entity. Therefore, forced optimistic-lock increment will trigger an audit change.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 20 hours ago









        NarosNaros

        9,70812049




        9,70812049






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54254090%2fall-version-fields-are-null-when-used-hibernate-envers%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Homophylophilia

            Updating UILabel text programmatically using a function

            Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage