Sto usando JPA-2.0 con Hibernate nel mio livello di accesso ai dati.Iniezione di Entity Manager di JPA in EmptyInterceptor di Hibernate
Ai fini della registrazione di controllo, io sto usando EmptyInterceptor di Hibernate configurando sotto proprietà in persistence.xml:
<property name="hibernate.ejb.interceptor"
value="com.mycom.audit.AuditLogInterceptor" />
Dove AuditLogInterceptor estende ibernazione del '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 to 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 am on postFlush");
// other code here
}
}
Sto utilizzando gestore di entità JPA nel livello di accesso ai dati per eseguire operazioni di database. configurazione JPA è come qui di seguito:
<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>
mio 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);
}
}
Vorrei iniettare JPA entity manager in 'AuditLogInterceptor' in modo che possa eseguire operazioni di database in 'AuditLogInterceptor' come il mio DAO astratto.
Qualche idea? Quale dovrebbe essere la soluzione corretta?
E la sicurezza del filo? –
Sto provando a farlo in JPA2 per ottenere un riferimento al mio auditLogRepository in quanto l'annotazione @Resource non ha iniettato nulla e mi ha lasciato con un NPE. – Stephane