Announcement Announcement Module
Collapse
No announcement yet.
Inject data in entity listener,LocalSessionFactoryBean vs LocalContainerEntityManager Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Inject data in entity listener,LocalSessionFactoryBean vs LocalContainerEntityManager

    Hello,

    I try to inject the name and the applicationName in an EntityListener to use them during PrePersist and PreUpdate.
    It was possible when I used the LocalSessionFactoryBean class
    Code:
    <bean id="sessionFactoryHib" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
     ...
    <property name="eventListeners">
      <map>
        <entry>
          <key><value>pre-insert</value></key>
          <ref bean="signHandler"/>
        </entry>
        <entry>
          <key><value>pre-update</value></key>
          <ref bean="signHandler"/>
       </entry>
      </map>
    </property>
    
    <bean id="signHandler" class="com....SignEntityListener">
      <property name="currentUser" value="aUser"/>
      <property name="appliName" value="theApp"/>
    </bean>
    Now I need to migrate to JPA and I use the org.springframework.orm.jpa.LocalContainerEntityMa nagerFactoryBean class as entity manager factory.

    I created an event listener class with the tags PrePersist and PreUpdate and I declared this listener with the tag EntityListeners.
    The listener is called but the autoDataSignature object in it is allways null.

    My listener ( tag means at )
    Code:
    tag Repository
    public class SignatureEntityListener {
    
    	private static final long serialVersionUID = 1L;
    
    	private static final Log LOG = LogFactory.getLog(SignatureEntityListener.class);
    
    	tag Autowired
    	private AutoDataSignature autoDataSignature;
    
    	tag PrePersist
    	public void onPreInsert(Object entity) {
    		Date currentDate = new Date();
    		if (entity instanceof ISignedEntity) {
    			LOG.info("onPreInsert : " + autoDataSignature);
    			ISignedEntity signedEntity = (ISignedEntity) entity;
    			signedEntity.getSignature().setCreated(currentDate);
    			signedEntity.getSignature().setUpdated(currentDate);
    			signedEntity.getSignature().setCreationApplication(autoDataSignature.getAppli());
    			signedEntity.getSignature().setUpdateApplication(autoDataSignature.getAppli());
    	   		signedEntity.getSignature().setCreator(autoDataSignature.getName());
    			signedEntity.getSignature().setUpdater(autoDataSignature.getName());
    		}
    	}
    
    	tag PreUpdate
    	public void onPreUpdate(Object entity) {
    		Date currentDate = new Date();
    
    		if (entity instanceof ISignature) {
    			LOG.info("onPreUpdate");
    			ISignedEntity signedEntity = (ISignedEntity) entity;
    			signedEntity.getSignature().setUpdated(currentDate);
    			signedEntity.getSignature().setUpdater(autoDataSignature.getName());
    			signedEntity.getSignature().setUpdateApplication(autoDataSignature.getAppli());
    		}
    	}
    }
    My spring file

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans ... >
         
    	<context:annotation-config /> 
        <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
        <context:component-scan base-package="ch.unicible" /> 
      
        <!-- Simple beans reused -->
    	<bean id="autoDataSignature" class="ch.unicible.clipre.mapper.bo.AutoDataSignature" scope="singleton">
            <property name="name" value="xze"/>
            <property name="appli" value="CRM"/>
        </bean>
    
    	<bean id="isisMapperFactory" class="com.ibm.bcc.crm.contact.mo.IsisMapperFactory" scope="singleton"/>
    	
    	<bean id="crmInfra"  class="com.ibm.bcc.crm.stub.CrmInfrastructureStub" scope="singleton"/>
    	
    	<!-- Persistence definition -->
    	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    
            <property name="dataSource" ref="dataSource" />
    
            <property name="jpaVendorAdapter">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                    <property name="generateDdl" value="true"/>
                    <property name="showSql" value="true" />
                    <property name="database" value="DERBY" />
                </bean>
            </property>
            <property name="jpaProperties">
                <props>
                    <prop key="hibernate.hbm2ddl.auto">update</prop>
                    <prop key="hibernate.default_schema">APP</prop>
                    <prop key="hibernate.ejb.autodetection">class</prop>
                    <prop key="hibernate.hbm2ddl.auto">update</prop>
                </props>
            </property>
        </bean>
    
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver" />
            <property name="url" value="jdbc:derby://localhost:1527/isisDB" />
            <property name="username" value="user1" />
            <property name="password" value="secret4me" />
        </bean>
    	
    	
    	<!-- Transactions -->
    	<tx:annotation-driven /> 
    	
    	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory" />
        </bean>
    	
    	<bean id="transactionInterceptor"  class="org.springframework.transaction.interceptor.TransactionInterceptor">
    	  <property name="transactionManager" ref="transactionManager" />
    	  <property name="transactionAttributeSource">
    	       <bean class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource" />
    	  </property>
    	</bean>
    
       <!-- Abstract Mapper-->
       <bean id="abstractMapper" class="ch.unicible.clipre.mapper.AbstractMapper" abstract="true">
           <property name="entityManagerFactory" ref="entityManagerFactory" />
           <property name="factory" ref="isisMapperFactory"/>
           <property name="autoDataSignature" ref="autoDataSignature" />
        </bean>
        
        <bean id="abstractMapperFactory"  class="org.springframework.aop.framework.ProxyFactoryBean"  abstract="true">
          <property name="proxyInterfaces">
            <value>ch.unicible.clipre.mapper.IMapper</value>
          </property>
          <property name="interceptorNames">
            <list>
              <idref local="transactionInterceptor" />
            </list>
          </property>
        </bean>
    
    	<!-- Client Mapper-->
    	<bean id="persistenceManagerTargetClient"  class="com.ibm.bcc.crm.contact.mo.ClientMapper" parent="abstractMapper"/>
    	  
    	<bean id="clientMapper"  class="org.springframework.aop.framework.ProxyFactoryBean" parent="abstractMapperFactory" >
    	  <property name="interceptorNames">
    	    <list>
    	      <idref local="persistenceManagerTargetClient" />
    	    </list>
    	  </property>
    	</bean>
    
        <!-- Contacts Mapper-->
    	<bean id="persistenceManagerTargetContact"  class="com.ibm.bcc.crm.contact.mo.ContactMapper" parent="abstractMapper">
           <property name="infra" ref="crmInfra"/>
        </bean>
    
        <bean id="contactMapper"  class="org.springframework.aop.framework.ProxyFactoryBean"  parent="abstractMapperFactory">
          <property name="interceptorNames">
            <list>
              <idref local="persistenceManagerTargetContact" />
            </list>
          </property>
        </bean>
        
        <!-- Propositions Mapper-->
        <bean id="persistenceManagerTargetProposition"  class="com.ibm.bcc.crm.contact.mo.PropositionMapper" parent="abstractMapper">
           <property name="infra" ref="crmInfra"/>
        </bean>
    
        <bean id="propositionMapper"  class="org.springframework.aop.framework.ProxyFactoryBean"  parent="abstractMapperFactory">
          <property name="interceptorNames">
            <list>
              <idref local="persistenceManagerTargetProposition" />
            </list>
          </property>
        </bean>
        
    </beans>
    Is there a solution to inject data in the entity listener used by JPA.
    Thank you for your help
    Bert.

  • #2
    I found a solution : I use spring-aspects to inject the bean outside of the spring bean factory.
    see the thread : forum.springframework.org/showthread.php?t=10175

    Comment


    • #3
      As stated in the other thread (you have the same issue here) fix your configuration and don't hack away...

      Your bean is a repository NOT a @Configurable so nothing is going to get injected.

      Comment


      • #4
        Inject data in entity listener,LocalSessionFactoryBean vs LocalContainerEntityManager

        How did you solve it ?

        Comment


        • #5
          The Listener called by the JPA Entity is not in the BeanFactory, it's managed by the JPA implementation Hibernate in my case. Even if I try to declare it in the BeanFactory, the instance called is not the BeanFactory one but another created by the JPA entity manager.
          So the spring configuration of the Listener is useless. That's why I try with aspects but I'm not completly satisfied because it's more complicated than the old pure hibernate implementation coupled with spring and the callback methods are called after the entity session is closed.

          Comment

          Working...
          X