spring - Injecting JPA's Entity Manager in Hibernate's EmptyInterceptor -
i using jpa-2.0 hibernate in data access layer.
for audit logging purposes, using hibernate's emptyinterceptor configuring below property in persistence.xml:
<property name="hibernate.ejb.interceptor" value="com.mycom.audit.auditloginterceptor" />
where auditloginterceptor extends hibernate's 'org.hibernate.emptyinterceptor'.
public class auditloginterceptor extends emptyinterceptor { private long userid; public auditloginterceptor() {} @override public boolean onsave(object entity, serializable id, object[] state, string[] propertynames, type[] types) throws callbackexception { // need perform database operations using jpa entity manager return false; } @override public boolean onflushdirty(object entity, serializable id, object[] currentstate, object[] previousstate, string[] propertynames, type[] types) { // other code here return false; } @override public void postflush(iterator iterator) throws callbackexception { system.out.println("i on postflush"); // other code here } }
i using jpa entity manager in data access layer perform database operations. jpa configuration below:
<bean id="entitymanagerfactory" class="org.springframework.orm.jpa.localcontainerentitymanagerfactorybean" p:persistenceunitname="persistenceunit" p:persistencexmllocation="classpath*:persistence.xml" p:datasource-ref="datasource" p:jpavendoradapter-ref="jpaadapter"> <property name="loadtimeweaver"> <bean class="org.springframework.instrument.classloading.instrumentationloadtimeweaver" /> </property> </bean>
my abstractdao :
public class abstractdao<t, id extends serializable> { private final transient class<t> persistentclass; protected transient entitymanager entitymanager; @suppresswarnings("unchecked") public abstractdao() { this.persistentclass = (class<t>) ((parameterizedtype) getclass().getgenericsuperclass()).getactualtypearguments()[0]; } @persistencecontext public final void setentitymanager(final entitymanager entitymgrtoset) { this.entitymanager = entitymgrtoset; } public final class<t> getpersistentclass() { return persistentclass; } public final void persist(final t entity) { entitymanager.persist(entity); } }
i inject jpa entity manager in 'auditloginterceptor' can perform database operations in 'auditloginterceptor' abstract dao.
any idea? should proper solution?
i have got simple way perform database operation using jpa entity manager in 'auditloginterceptor'
i have created below class give application context reference:
@component("applicationcontextprovider") public class applicationcontextprovider implements applicationcontextaware { private static applicationcontext context; public static applicationcontext getapplicationcontext() { return context; } @override public void setapplicationcontext(applicationcontext ctx) { context = ctx; } }
created data access class:
@repository("myauditdao") public class myauditdao<t, id extends serializable> { private final transient class<t> persistentclass; protected transient entitymanager entitymanager; @suppresswarnings("unchecked") public mydao() { this.persistentclass = (class<t>) ((parameterizedtype) getclass().getgenericsuperclass()).getactualtypearguments()[0]; } @persistencecontext public final void setentitymanager(final entitymanager entitymgrtoset) { this.entitymanager = entitymgrtoset; } public final class<t> getpersistentclass() { return persistentclass; } public final t findbyid(final id theid) { return entitymanager.find(persistentclass, theid); } public final void persist(final t entity) { entitymanager.persist(entity); } public final void merge(final t entity) { entitymanager.merge(entity); } }
and used 'applicationcontextprovider' in 'auditloginterceptor' reference of 'myauditdao' having jpa entity manager property injected during dao initialization. of 'myauditdao' can perform database operations.
public class auditloginterceptor extends emptyinterceptor { @override public void postflush(iterator iterator) throws callbackexception { // here can myauditdao reference , can perform persiste/merge options myauditdao myauditdao = (myauditdao ) applicationcontextprovider.getapplicationcontext().getbean("myauditdao"); // myauditdao.persist(myentity); } }
Comments
Post a Comment