gpt4 book ai didi

hibernate - Spring + hibernate + JPA : How to reload EntityManagerFactory at runtime

转载 作者:行者123 更新时间:2023-12-01 06:42:53 26 4
gpt4 key购买 nike

过去几个小时我一直在寻找这个,也许你们中的一些人可以帮助我。

我尝试实现 重新加载我的地​​图信息在 Spring 运行时在 EntityManagerFactory(或 SessionFactory)中

EntityManagerFactory 定义如下:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="persistence.xml" />
<property name="persistenceUnitName" value="JPAService" />
<property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence"/>
<property name="dataSource" ref="dataSource" />
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.Hibernate.JpaDialect" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">none</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>

在我的 persistence.xml 中,我只定义了映射文件所在的 jar
<?xml version="1.0" encoding="UTF-8"?>
<persistence
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="JPAService" transaction-type="RESOURCE_LOCAL">
<jar-file>WEB-INF/lib/mapping.jar</jar-file>
</persistence-unit>
</persistence>

我的 hibernate 映射文件会经常更改,我的应用程序正在使用这些文件来生成部分 UI。因此,我不想每次更改 hibernate 映射时都重新启动服务器。

我想到的一件事是用一个新的替换 EntityManagerFacotries/SessionFactory

Hibernate configuration on runtime
Dynamic Configure an EJB

但我不知道副作用

另一种方法是在运行时以编程方式更改(添加/删除)EntityManagerFactory/SessionFactory 映射:

JPA: adding entities to EntityManagerFactory programmatically
Programmatically loading Entity classes with JPA 2.0?

一个非常复杂的场景,没有找到解决方案

Dynamic ORM entity class generation - NOT SOLVED

另一个主题提到动态JPA

How can I merge / extend persistence units from different JARs?
JPA 2.0: Adding entity classes to PersistenceUnit *from different jar* automatically

我已经尝试像这样从 Spring 更新整个应用程序上下文
@RequestMapping(value = { "/path" })
public ModelMap refresh(Model model, Locale locale) throws IOException,
TemplateException, ExtJSException {

((ConfigurableApplicationContext)ApplicationContextProvider.getApplicationContext()).refresh();

return getMessage("Context was refreshed!!");
}

但似乎这个项目不再受支持......

最佳答案

除了重新加载整个上下文之外,我没有看到任何其他解决方案。

所有其他做法都可能导致内存泄漏(例如在连接上)或各种 ClassNotFound 和其他事情
当您尝试单独重新配置实体管理器时,只需想象在 tx 期间会发生什么

我能想到的一种解决方案是“隔离”数据层的整个 bean 定义并替换 bean 本身,从而避免完整的应用程序上下文重新加载。

您可以通过尝试创建新的 ApplicationContext、添加数据层 bean 并替换现有的或使用一堆接口(interface)(对于 entitymanager、ds 等)来完成或尝试
并替换他们的实现@运行时(但需要大量的工作..)

你只需要确保你正在重新加载 EntityManager + TX + DS (也许还有其他我忘记的东西)

高温高压

关于hibernate - Spring + hibernate + JPA : How to reload EntityManagerFactory at runtime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8671776/

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