Spring + Hibernate + JPA:如何在运行时重新加载EntityManagerFactory [英] Spring + Hibernate + JPA: How to reload EntityManagerFactory at runtime

查看:232
本文介绍了Spring + Hibernate + JPA:如何在运行时重新加载EntityManagerFactory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我尝试实现重新载入我的映射信息

我最近几个小时一直在寻找此信息,可能有些人可以帮助我。 / strong>在EntityManagerFactory(或SessionFactory)中的运行时弹簧



EntityManagerFactory的定义如下:

 < bean id =entityManagerFactoryclass =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean> 
< property name =persistenceXmlLocationvalue =persistence.xml/>
< property name =persistenceUnitNamevalue =JPAService/>
< property name =persistenceProviderClassvalue =org.hibernate.ejb.HibernatePersistence/>
< property name =dataSourceref =dataSource/>
< property name =jpaDialect>
< bean class =org.springframework.orm.jpa.vendor.Hibernate.JpaDialect/>
< / property>
< property name =jpaProperties>
<道具>
< prop key =hibernate.dialect> org.hibernate.dialect.MySQL5Dialect< / prop>
< prop key =hibernate.hbm2ddl.auto> none< / prop>
< prop key =hibernate.show_sql> true< / prop>
< /道具>
< / property>
< / bean>

在我的persistence.xml中,我只是定义了映射文件所在的jar

 <?xml version =1.0encoding =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 =JPAServicetransaction-type =RESOURCE_LOCAL>
< jar-file> WEB-INF / lib / mapping.jar< / jar-file>
< / persistence-unit>
< /余辉>



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



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



运行时的Hibernate配置 >
动态配置EJB



但我不知道副作用另一种方法是在运行时以编程方式更改(添加/删除)EntityManagerFactory / SessionFactory映射:
/ p>

JPA:以编程方式向EntityManagerFactory添加实体

使用JPA 2.0以编程方式加载实体类?



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



另一个提及动态的JPA



如何合并/扩展来自不同JAR的持久化单元?



我已经尝试更新来自春季的整个应用程序上下文像这样

  @RequestMapping(value = {/ path}) 
public ModelMap refresh(Model model,Locale locale)throws IOException,
TemplateException,ExtJSException {

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

return getMessage(Context was refreshres !!);
}

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

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



所有其他的做法可能导致内存泄漏(例如连接)或各种ClassNotFound和其他事物
只是想象当你试图单独重新配置entitymanager时在tx期间会发生什么

p>

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



你可以完成或尝试创建一个新的ApplicationContext,添加你的数据层bean并替换现有的或者使用一堆接口(对于entitymanager,ds等)。
并替换它们的实现@运行时(但需要很多工作..)



你只需要确保你是relo添加EntityManager + TX + DS(以及其他我忘记的东西)



HTH


I have been searching for this the last few hours maybe some of you can help me.

I try to achieve a reload of my mapping info in EntityManagerFactory (or SessionFactory) at runtime in spring

The EntityManagerFactory is defined as follows:

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

In my persistence.xml I just define the jar where the mapping files are

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


My hibernate mapping files will change very often and my app is using this files to generate part of the UI. Therefore I don't want to restart the server every time I change my hibernate mappings.

One thing I thought about is replacing EntityManagerFacotries/SessionFactory with a new one like so

Hibernate configuration on runtime
Dynamic Configure an EJB

but I do not know the side effects

Another way is to change (add/remove) the EntityManagerFactory/SessionFactory Mapping programatically at runtime:

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

A very sophisticated scenario where no solution was found

Dynamic ORM entity class generation - NOT SOLVED

Another thead mentiones dynamic-JPA

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

I already tried to update the whole application context from spring like so

@RequestMapping(value = { "/path" })
public ModelMap refresh(Model model, Locale locale) throws IOException,
  TemplateException, ExtJSException {

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

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

But it seems as if this project isn't supported any more...

解决方案

I don't see any other solution besides reloading entire context.

All other practices could lead to memory leaks (on connections for example) or to all sorts of ClassNotFound and other things just image what will happen during a tx when you're trying to reconfigure the entitymanager alone

One solution I can think of is to "isolate" entire bean definition of the data-layer and replace the beans themselves thus avoiding a full app-context reload.

You can accomplish or by trying to create a new ApplicationContext, adding your data layer beans and replacing existing ones or by using a bunch of interfaces (for entitymanager, ds etc..) and replacing their implementation @ runtime (but will require a lot of work..)

you just need to make sure you're reloading the EntityManager + TX + DS (and perhaps other things I've forgot)

HTH

这篇关于Spring + Hibernate + JPA:如何在运行时重新加载EntityManagerFactory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆