过去几个小时我一直在寻找这个,也许你们中的一些人可以帮助我。
我尝试在春季运行时在 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>
我的休眠映射文件会经常更改,我的应用程序正在使用这些文件来生成部分 UI。因此,我不想每次更改休眠映射时都重新启动服务器。
我想到的一件事是用一个新的替换 EntityManagerFacotries/SessionFactory
但我不知道副作用
另一种方法是在运行时以编程方式更改(添加/删除)EntityManagerFactory/SessionFactory 映射:
JPA:以编程方式将实体添加到 EntityManagerFactory 以编程
方式使用 JPA 2.0 加载实体类?
一个非常复杂的场景,没有找到解决方案
另一个主题提到动态JPA
如何合并/扩展来自不同 JAR 的持久性单元?
JPA 2.0:自动将实体类*从不同的 jar* 添加到 PersistenceUnit
我已经尝试像这样从春天更新整个应用程序上下文
@RequestMapping(value = { "/path" })
public ModelMap refresh(Model model, Locale locale) throws IOException,
TemplateException, ExtJSException {
((ConfigurableApplicationContext)ApplicationContextProvider.getApplicationContext()).refresh();
return getMessage("Context was refreshed!!");
}
但似乎这个项目不再受支持......