What I am trying to do is inject through XML almost the same way that is done through A @PersistenceContext annotation. I am in need of this because of the fact I have different entity managers I need to inject into the same DAO. The databases mirror one another and I would rather have 1 base class and for instances of that base class then create multiple classes just so I can use the @PersistenceContext annotation.
Here is my example. This is what I am doing now and it works.
public class ItemDaoImpl {
protected EntityManager entityManager;
public List<Item> getItems() {
Query query = entityManager.createQuery("select i from Item i");
List<Item> s = (List<Item>)query.getResultList();
return s;
}
public void setEntityManger(EntityManager entityManager) {
this.entityManager = entityManager;
}
}
@Repository(value = "itemDaoStore2")
public class ItemDaoImplStore2 extends ItemDaoImpl {
@PersistenceContext(unitName = "persistence_unit_2")
public void setEntityManger(EntityManager entityManager) {
this.entityManager = entityManager;
}
}
@Repository(value = "itemDaoStore1")
public class ItemDaoImplStore1 extends ItemDaoImpl {
@PersistenceContext(unitName = "persistence_unit_1")
public void setEntityManger(EntityManager entityManager) {
this.entityManager = entityManager;
}
}
TransactionManagers, EntityManagers are defined below...
<!-- Registers Spring's standard post-processors for annotation-based configuration like @Repository -->
<context:annotation-config />
<!-- For @Transactional annotations -->
<tx:annotation-driven transaction-manager="transactionManager1" />
<tx:annotation-driven transaction-manager="transactionManager2" />
<!-- This makes Spring perform @PersistenceContext/@PersitenceUnit injection: -->
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<!-- Drives transactions using local JPA APIs -->
<bean id="transactionManager1" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory1" />
</bean>
<bean id="transactionManager2" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory2" />
</bean>
<bean id="entityManagerFactory1" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="persistence_unit_1"/>
...
</bean>
<bean id="entityManagerFactory2" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="persistence_unit_2"/>
...
</bean>
What I want to do is to NOT create classes ItemDaoImplStore2 or ItemDaoImplStore1. I want to have these as instances of ItemDaoImpl via xml instead. I do not know how to inject the entitymanager properly though. I want to simulate annotating this as a 'Repository' annotation, and I also want to be able to specify what entityManager to inject by the persistence unit name. I want something similar to the below using XML instead.
<!-- Somehow annotate this instance as a @Repository annotation -->
<bean id="itemDaoStore1" class="ItemDaoImpl">
<!-- Does not work since it is a LocalContainerEntityManagerFactoryBean-->
<!-- Also I would perfer to do it the same way PersistenceContext works
and only provide the persistence unit name. I would like to be
able to specify persistence_unit_1-->
<property name="entityManager" ref="entityManagerFactory1"/>
</bean>
<!-- Somehow annotate this instance as a @Repository annotation -->
<bean id="itemDaoStore2" class="ItemDaoImpl">
<!-- Does not work since it is a LocalContainerEntityManagerFactoryBean-->
<!-- Also I would perfer to do it the same way PersistenceContext works
and only provide the persistence unit name. I would like to be
able to specify persistence_unit_2-->
<property name="entityManager" ref="entityManagerFactory2"/>
</bean>
You can use the @PersistenceContext annotation to inject an EntityManager in an EJB 3.0 client (such as a stateful or stateless session bean, message-driven bean, or servlet).
EntityManagerFactory vs EntityManagerWhile EntityManagerFactory instances are thread-safe, EntityManager instances are not. The injected JPA EntityManager behave just like an EntityManager fetched from an application server's JNDI environment, as defined by the JPA specification.
A PersistenceContext is essentially a Cache. It also tends to have it's own non-shared database connection. An EntityManager represents a PersistenceContext (and therefore a Cache) An EntityManagerFactory creates an EntityManager (and therefore a PersistenceContext/Cache)
The EntityManager is an API that manages the lifecycle of entity instances. An EntityManager object manages a set of entities that are defined by a persistence unit. Each EntityManager instance is associated with a persistence context.
Use SharedEntityManagerBean
- it creates a shared EntityManager
for EntityManagerFactory
the same way as @PersistenceContext
:
<bean id="itemDaoStore1" class="ItemDaoImpl">
<property name="entityManager">
<bean class = "org.springframework.orm.jpa.support.SharedEntityManagerBean">
<property name = "entityManagerFactory" ref="entityManagerFactory1"/>
</bean>
</property>
</bean>
You can provide the persistence unit name in the xml configuration, using the SharedEntityManagerBean
, like below:
<bean id="testDao" class="com.test.persistence.dao.BaseDAO">
<property name="entityManager">
<bean class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
<property name="persistenceUnitName" value="persistence-test-unit" />
</bean>
</property>
</bean>
of course, you can have the SharedEntityManagerBean
as a separate bean
Here, I m injecting entityManager
into BaseDAO
as you're doing using @PersistenceContext(unitName="...")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With