Injecting the application TransactionManager into a JPA EntityListener
Posted
by
nodje
on Stack Overflow
See other posts from Stack Overflow
or by nodje
Published on 2012-08-21T17:03:58Z
Indexed on
2012/09/20
15:38 UTC
Read the original article
Hit count: 200
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.
© Stack Overflow or respective owner