gpt4 book ai didi

spring - 如何在 Spring 中以编程方式获取 entitymanager?

转载 作者:行者123 更新时间:2023-12-03 18:38:40 26 4
gpt4 key购买 nike

我正在尝试将我们的 DAO maven 模块集成到 spring,我们不想更改 dao 的任何代码。以前,所有的 dao 类都使用以下方式获取实体管理器。

Persistence.getEntityManager()

在 Spring ,我看到很多使用注释注入(inject)实体管理器的示例,但需要更改 dao 代码。有没有办法可以修改下面的类以使其在 Spring 工作?
 public class PersistenceManager
{

/**
* Key that stores the entity manager.
*/
private static final String ENTITY_MANAGER_INSTANCE = "ENTITY_MANAGER_INSTANCE";
/**
* Persistence unit for eMyEd
*/
private static final String PERSISTENCE_UNIT_EMYED = "application_openjpa";
/**
* The static factory used across the JVM
*/
private static final EntityManagerFactory ENTITY_MANAGER_FACTORY = Persistence
.createEntityManagerFactory(PERSISTENCE_UNIT_EMYED);

/**
* Cleanup any entity managers created in the thread context.
*/
public static void close()
{
EntityManager emInstance = getEntityManager();
try
{
if (emInstance.isOpen())
{
emInstance.close();
}
}
catch (Throwable t)
{
// ignore
}
finally
{
ThreadContext.instance.clear();
}
}

/**
* Returns the Entity manager associated with the given thread context. If
* none available, one is created and set on the thread context and the same
* is returned.
*
* @return
*/
public static EntityManager getEntityManager()
{
EntityManager emInstance = (EntityManager) ThreadContext.instance
.get(ENTITY_MANAGER_INSTANCE);
if (emInstance == null)
{
emInstance = createEntityManager();
emInstance.setFlushMode(FlushModeType.COMMIT);
ThreadContext.instance.put(ENTITY_MANAGER_INSTANCE, emInstance);
}
try
{
// try to join the current active transaction
emInstance.joinTransaction();
}
catch (TransactionRequiredException notSupportedEx)
{
// If there was no transaction to join. Ignore
}
catch (Throwable unknownEx)
{
// If there was no transaction to join. Ignore
}
return emInstance;

}

/**
* Create a new entity manager.
*
* @return
*/
private static EntityManager createEntityManager()
{
return ENTITY_MANAGER_FACTORY.createEntityManager();
}
}

Spring 配置
<description>Example configuration to get you started.</description>
<context:component-scan base-package="com.veera" />
<!-- tell Spring that it should act on any @PersistenceContext and @Transactional annotations found in bean classes -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- ******************************************************************** -->
<!-- Setup the transaction manager -->
<!-- ******************************************************************** -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>


<!-- ******************************************************************** -->
<!-- Setup each data source -->
<!-- ******************************************************************** -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>

<!-- ******************************************************************** -->
<!-- Setup each persistence unit -->
<!-- ******************************************************************** -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter">
<property name="databasePlatform"
value="org.apache.openjpa.jdbc.sql.MySQLDictionary" />
</bean>
</property>
<property name="persistenceUnitName" value="application_openjpa" />
<!-- property name="loadTimeWeaver">
<bean
class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property -->
<!-- property name="jpaProperties"> <props> <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
</props> </property -->
</bean>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

持久性.xml
    <persistence-unit name="application_openjpa"
transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>com.veera.jpa.Item</class>
<class>com.veera.jpa.Order</class>
<class>com.zreflect.emyed.entity.user.User</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<!-- property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema" /
<property name="openjpa.InitializeEagerly" value="true"/-->
<property name="openjpa.Log" value="File=stdout, DefaultLevel=INFO, Runtime=INFO, SQL=TRACE"/>
</properties>
</persistence-unit>

</persistence>

最佳答案

作为 EntityManager根据定义,它不是线程安全的,唯一的 - 仍然丑陋的 - 解决方案是持有 EntityManagerFactory静态创建 EntityManager可应要求提供工厂实例。

我什至建议通过这种方法更改 DAO 以使用依赖注入(inject),因为您的 DAO 本质上变得不可单元测试,而无需引导整个 JPA 基础设施。

关于spring - 如何在 Spring 中以编程方式获取 entitymanager?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18668378/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com