2010-06-01 37 views
11

Điều tôi đang cố gắng thực hiện là tiêm thông qua XML hầu như giống như cách được thực hiện thông qua chú thích A @PersistenceContext. Tôi cần điều này vì thực tế tôi có những người quản lý thực thể khác nhau mà tôi cần phải tiêm vào cùng một DAO. Các cơ sở dữ liệu phản chiếu lẫn nhau và tôi muốn có 1 lớp cơ sở và cho các cá thể của lớp cơ sở đó, sau đó tạo nhiều lớp để tôi có thể sử dụng chú thích @PersistenceContext.Tiêm Entitymanager qua XML chứ không phải các chú thích

Đây là ví dụ của tôi. Đây là những gì tôi đang làm bây giờ và nó hoạt động.

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 được định nghĩa dưới đây ...

<!-- 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> 

Những gì tôi muốn làm là KHÔNG tạo lớp ItemDaoImplStore2 hoặc ItemDaoImplStore1. Tôi muốn có những trường hợp như ItemDaoImpl qua xml thay thế. Tôi không biết làm thế nào để tiêm thực thể đúng cách mặc dù. Tôi muốn mô phỏng chú thích này như là một chú thích 'Kho lưu trữ', và tôi cũng muốn có thể chỉ định entityManager nào được chèn vào bởi tên đơn vị tồn tại. Tôi muốn một cái gì đó tương tự như dưới đây bằng cách sử dụng XML để thay thế.

<!-- 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> 

Trả lời

12

Sử dụng SharedEntityManagerBean - nó tạo ra một chia sẻ EntityManager cho EntityManagerFactory theo cùng một cách như @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> 
4

Bạn có thể cung cấp các kiên trì tên đơn vị trong cấu hình xml, sử dụng SharedEntityManagerBean, như dưới đây :

<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> 

tất nhiên, bạn có thể có SharedEntityManagerBean như một bean riêng

Ở đây, tôi đang tiêm entityManager vào BaseDAO như bạn đang làm bằng @PersistenceContext(unitName="...")

Các vấn đề liên quan