Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I configure a JPA EntityListener via a Spring XML configuration file?

I have two webapps, that are built together and respectively provide a client and admin access to a database.

I'd like to register two JPA EntityListeners to a given class, one in each app. To do that, I'm trying to find a way to register the listeners via the Spring XML configuration file that configure each app's JPA context... and just cannot find any way.

Has anybody already done something similar?

Here is a part of the XML configuration file:

<bean id="tempEntityManagerFactory"
  class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
  p:dataSource-ref="tempDataSource" p:persistenceUnitName="tempJpa" >

  <property name="jpaVendorAdapter">
    <bean
      class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
      p:database="ORACLE" 
      p:databasePlatform="org.hibernate.dialect.Oracle9iDialect"
      p:showSql="false"   />
  </property>

  <property name="jpaProperties">
    <props>
      <prop key="hibernate.hbm2ddl.auto">validate</prop>
    </props>
  </property>

</bean>

I thought that there was a way to register a listener around these elements, especially JPAProperties, since it seems to be the place to set JPA configuration elements...

The JPA spec speaks about entity-listeners XML elements, but I can't find a way to inject them in the available Spring elements...

I'm fairly new to Spring, so I may well have misunderstood something... Thanks for your help!

like image 234
Sébastien Avatar asked Oct 30 '09 10:10

Sébastien


1 Answers

I guess what you're looking for is something like this (never mind the actual listeners in this example):

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

    <!-- other configuration cut out -->

    <property name="jpaProperties">
        <props> 

            <!-- for hibernate envers -->
            <prop key="hibernate.ejb.event.post-insert">
                org.hibernate.ejb.event.EJB3PostInsertEventListener,org.hibernate.envers.event.AuditEventListener
            </prop>
            <prop key="hibernate.ejb.event.post-update">
                org.hibernate.ejb.event.EJB3PostUpdateEventListener,org.hibernate.envers.event.AuditEventListener
            </prop>
            <prop key="hibernate.ejb.event.post-delete">
                org.hibernate.ejb.event.EJB3PostDeleteEventListener,org.hibernate.envers.event.AuditEventListener
            </prop>
            <prop key="hibernate.ejb.event.pre-collection-update">org.hibernate.envers.event.AuditEventListener
            </prop>
            <prop key="hibernate.ejb.event.pre-collection-remove">org.hibernate.envers.event.AuditEventListener
            </prop>
            <prop key="hibernate.ejb.event.post-collection-recreate">org.hibernate.envers.event.AuditEventListener
            </prop>
        </props>
    </property>
</bean>
like image 193
Bart Avatar answered Sep 21 '22 13:09

Bart