2

AuditingEntityListener correctly updates columns marked with @LastModifiedDate, @CreatedDate, @CreatedBy and @LastModifiedBy in dev mode (mvn jetty:run) when I use LocalContainerEntityManagerFactoryBean. However, when I activate prod profile and deploy in Wildfly 8, the columns are not updated.

I found on this forum post: "You will need to use one of Spring's EntityManagerFactoryBeans to setup the EntityManagerFactory" Is there any way to use AuditingEntityListener with <jee:jndi-lookup /> EntityManagerFactory?

Here is my applicationContext.xml

<beans profile="dev">
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${database.driver}" />
        <property name="url" value="${database.url}" />
        <property name="username" value="${database.username}" />
        <property name="password" value="${database.password}" />
    </bean>
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="persistenceXmlLocation" value="classpath:/META-INF/local-container-persistence.xml" />
    </bean>
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
</beans>

<beans profile="prod">
    <jee:jndi-lookup id="dataSource" jndi-name="java:jboss/datasources/postgresql-datasource" lookup-on-startup="false" expected-type="javax.sql.DataSource" />
    <jee:jndi-lookup id="entityManagerFactory" jndi-name="java:jboss/entity-manager-factory" lookup-on-startup="false" expected-type="javax.persistence.EntityManagerFactory" />
    <tx:jta-transaction-manager />
</beans>

<jpa:repositories base-package="com.corp.repository" factory-class="com.corp.RespositoryFactoryBean" />
<jpa:auditing auditor-aware-ref="entityAuditorAware" />
<bean name="entityAuditorAware" class="com.corp.EntityAuditorAware" />

orm.xml

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_2_0.xsd"
    version="2.0">
    <persistence-unit-metadata>
        <persistence-unit-defaults>
            <entity-listeners>
                <entity-listener class="org.springframework.data.jpa.domain.support.AuditingEntityListener" />
            </entity-listeners>
        </persistence-unit-defaults>
    </persistence-unit-metadata>
</entity-mappings>
Oliver Drotbohm
  • 80,157
  • 18
  • 225
  • 211
alex
  • 21
  • 1
  • 3

1 Answers1

0

tl;dr

Make sure the EntityManagerFactory you obtain from JNDI is configured to have the AuditingEntityListener applied to the persistence unit you obtain.

Details

In your dev profile, The EntityManagerFactory is created within your application and thus - by definition in the spec - considers the locally available orm.xml.

In you prod example you don't bootstrap an EntityManagerFactory locally but obtain a preconfigured one from your application server. Thus you have to make sure the EntityManagerFactory instance is configured as you expected in the application server in the first place.

Oliver Drotbohm
  • 80,157
  • 18
  • 225
  • 211
  • Oliver, thank you for your time. `AuditingEntityListener` gets initialized but `handler` is null in `touchForCreate` and `touchForUpdate` methods, so the columns are not updated. Debugging `AuditingBeanFactoryPostProcessor` I see `BeanDefinitionUtils.getEntityManagerFactoryBeanNames` returns an empty `Set` since `BeanFactoryUtils.beanNamesForTypeIncludingAncestors` returns and empty `Set` for both `EntityManagerFactory.class` and `AbstractEntityManagerFactoryBean.class` – alex Mar 12 '15 at 18:36