Results 1 to 4 of 4

Thread: Can't get AuditingEntityListener to fire

  1. #1
    Join Date
    Aug 2004
    Location
    London, UK
    Posts
    10

    Default 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 at 09:20 AM.

  2. #2
    Join Date
    May 2011
    Posts
    3

    Default Working for me

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

  3. #3
    Join Date
    Apr 2006
    Location
    Dresden, Germany
    Posts
    492

    Default

    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.

  4. #4
    Join Date
    May 2011
    Posts
    3

    Default

    Thanks for that.

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •