Google App Engine Entity Manager Configuration with Spring 3.0
-
26-10-2019 - |
Pergunta
I'm trying to write a Spring configuration for some basic Google App Engine services. In particular for the EntityManagerFactory and Entity Manager.
To create an EntityManagerFactory from Java code:
EntityManagerFactory emfInstance = javax.persistence.Persistence
.createEntityManagerFactory("transactions-optional");
Which I would translate as:
<bean id="entityManagerFactory" class="javax.persistence.Persistence"
factory-method="createEntityManagerFactory" scope="singleton">
<constructor-arg value="transactions-optional"/>
</bean>
And every time I need an EntityManager
EntityManager em = emfInstance.get().createEntityManager();
But I don't know how to tell to apply the factory method to the previously created factory.
<bean id="entityManagerProto"
factory-method="createEntityManager" scope="prototype"/>
Anyone knows? Thanks.
Solução
You can use existing bean as a factory as follows:
<bean id="entityManagerProto"
factory-bean = "entityManagerFactory"
factory-method="createEntityManager" scope="prototype"/>
Also, I'm not sure whether it would work on GAE, but perhaps it would be better to use LocalEntityManagerFactoryBean
instead, and get EntityManager
injected with @PersistenceContext
.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow