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
Now I need to migrate to JPA and I use the org.springframework.orm.jpa.LocalContainerEntityMa nagerFactoryBean class as entity manager factory.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>
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 )
My spring fileCode: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()); } } }
Is there a solution to inject data in the entity listener used by JPA.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>
Thank you for your help
Bert.


Reply With Quote
