Announcement Announcement Module
Collapse
No announcement yet.
Can't get AuditingEntityListener to fire Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Can't get AuditingEntityListener to fire

    Hi,
    I am playing around with Spring Data JPA and thought I would give the AuditingEntityListener a try. I have followed instructions from here http://static.springsource.org/sprin.../#jpa.auditing and created my implementation of AuditorAware interface but I can't seem to get the listener to fire. (note I have not implemented the Auditable anywhere yet but I simply wanted to get my AuditorAware implementation to be invoked).

    My persistence.xml looks like:
    Code:
    <persistence-unit name="spring-jpa" transaction-type="RESOURCE_LOCAL">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <mapping-file>META-INF/orm.xml</mapping-file>
            <properties>
                <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
                <!-- value="create" to build a new database on each run; value="update" to modify an existing database; value="create-drop" means the same as "create" but also drops tables when Hibernate closes; value="validate" makes no changes to the database -->
                <property name="hibernate.hbm2ddl.auto" value="validate"/>
                <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
                <property name="hibernate.connection.charSet" value="UTF-8"/>
                <!-- Uncomment the following two properties for JBoss only -->
                <!-- property name="hibernate.validator.apply_to_ddl" value="false" /-->
                <!-- property name="hibernate.validator.autoregister_listeners" value="false" / -->
            </properties>
        </persistence-unit>
    My orm looks like:
    Code:
    <?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 http://java.sun.com/xml/ns/persistence/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>
    and I have registered the following in application context.
    Code:
    <!--  Scan repositories for insert of Crud operations -->    
        <jpa:repositories base-package="com.xxx.yyy.publisher.admin.dao.jpa" />
        <!-- Set up Auditing Bean -->
        <jpa:auditing auditor-aware-ref="auditorAwareBean" />
    	
    	<bean id="auditorAwareBean" class="com.xxx.yyy.publisher.admin.dao.jpa.LoggedInAware" />
    But I can't get my LoggedInAware (implements AuditorAware) to be invoked.

    Am I missing something? Any ideas more than welcome.

    Cheers
    Andreas
    Last edited by andreas; Feb 16th, 2011, 10:20 AM.

  • #2
    Working for me

    Used EntityListener annotation as follows:
    @EntityListeners({ org.springframework.data.jpa.domain.support.Auditi ngEntityListener.class })

    Comment


    • #3
      You will need to use one of Spring's EntityManagerFactoryBeans to setup the EntityManagerFactory. This is due to the fact that <jpa:auditing /> registers a special AOP component that automatically injects the AuditorAware implementation into the AuditingEntityListener instances when they are created. If you don't use that EntityManagerFactoryBean infrastructure the AuditingEntityListener instance will already be created before we can bootstrap the injection mechanism.

      Comment


      • #4
        Thanks for that.

        Another question, I want to set AuditingEntityListener.modifyOnCreation=false
        How can I do that?

        Comment

        Working...
        X