Announcement Announcement Module
Collapse
No announcement yet.
Spring Data JPA Auditing stops working when I upgrade from 1.4.3 to 1.6.0 Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Data JPA Auditing stops working when I upgrade from 1.4.3 to 1.6.0

    Auditing worked fine for me in version 1.4.3. Now I've tried upgrading to 1.6.0 and auditing simply stops working. (doesnt work with 1.5.X either)

    The only difference in configuring auditing between 1.4.3. vs 1.6.0 that I could spot was the @EnableJpaAuditing annotation. I've added that to one of my Configuration classes and even specified 2auditorAwareRef" with the correct AuditorAware bean, just to make sure.

    I've debugged into AuditingEntityListener, and setAuditingHandler() is called twice during my JUnit test (creating and saving one jpa entity with auditing-annotated fields), but when touchForCreate() is called during JpaRepository.flush(), it is called on a different (new?) instance of AuditingEntityListener, which has no handler (AuditingEntityListener.handler = null), so my entities never receive auditing information during flush().

    Any idea whats going wrong? What is the difference between 1.4.3 and the new 1.6 way that I did not spot?

    I'd like to refrain from posting my configuration due to legal reasons unless I can't get help otherwise.

  • #2
    After long hours fiddling around with this, I found two things:

    1. I was lacking this line in my Config's EntityManagerFactory definition. It would be sensible to add that to the documentation of Spring Data JPA. As soon as I have a persistence.xml next to the orm.xml, auditing stops working apparently because it doesn't load orm.xml anymore (the example project from spring.io confirms this in a comment).

    Code:
    bean.setMappingResources("META-INF/orm.xml");
    2. After that, my EntityManagerFactory definition looked like this:

    Code:
        @Bean
        FactoryBean<EntityManagerFactory> entityManagerFactory() throws NamingException {
    
            HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
            vendorAdapter.setDatabase(Database.ORACLE);
    
            LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
            bean.setPackagesToScan(RsKonstanten.EPLRS_MODEL_PACKAGE);
            bean.setMappingResources("META-INF/orm.xml");
            bean.setJpaVendorAdapter(vendorAdapter);
            bean.setDataSource(dataSourceRs());
            return bean;
        }
    It started working again when I changed the definition such:

    Code:
        @Bean
        LocalContainerEntityManagerFactoryBean entityManagerFactory() throws NamingException {
    
            HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
            vendorAdapter.setDatabase(Database.ORACLE);
    
            LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
            bean.setPackagesToScan(RsKonstanten.EPLRS_MODEL_PACKAGE);
            bean.setMappingResources("META-INF/orm.xml");
            bean.setJpaVendorAdapter(vendorAdapter);
            bean.setDataSource(dataSourceRs());
            return bean;
        }
    Can you spot the difference?

    It's the return type of the method. Once I changed it to the plain LocalContainerEntityManagerFactoryBean, it started working. I have no clue why I initially had FactoryBean<EntityManagerFactory> as return type, nor why auditing would work one way but not the other.

    Somebody more well-versed in Spring Configuration magic would have to answer that.

    PS: The example projects at https://github.com/spring-projects/s...a-jpa-examples use an milestone version of Spring Data. It would be good to update it to a regular, newer version of Spring Data, like 1.6.0.RELEASE.

    Comment

    Working...
    X