Injecting the application TransactionManager into a JPA EntityListener
- by nodje
I want to use the JPA EntityListener to support spring security ACLs.
On @PostPersist events, I create a permission corresponding to the persisted entity.
I need this operation to participate to the current Transaction.
For this to happen I need to have a reference to the application TransactionManager in the EntityListener.
The problem is, Spring can't manage the EntityListener as it is created automatically when EntityManagerFactory is instantiated.
And in a classic Spring app, the EntityManagerFactory is itself created during the TransactioManager instantiation.
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
So I have no way to inject the TransactionManager with the constructor, as it is not yet instantiated.
Making the EntityManager a @Component create another instance of the EntityManager.
Implementing InitiliazingBean and using afterPropertySet() doesn't work as it's not a Spring managed bean.
Any idea would be helpful as I'm stuck and out of ideas.