Injecting Entitymanager via XML and not annnotations

后端 未结 2 1088
萌比男神i
萌比男神i 2021-02-20 03:44

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 differen

相关标签:
2条回答
  • 2021-02-20 04:12

    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>
    
    0 讨论(0)
  • 2021-02-20 04:12

    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="...")

    0 讨论(0)
提交回复
热议问题