Spring + Hibernate + JPA: how to reload EntityManagerFactory at runtime

I have been looking for this for the past few hours, maybe some of you can help me.

I am trying to reload matching information in EntityManagerFactory (or SessionFactory) at runtime in spring

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 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 hibernation mapping files will change very often, and my application uses these files to create part of the user interface. Therefore, I do not want to restart the server every time I change my sleep mappings.

One thing I was thinking about is replacing EntityManagerFacotries / SessionFactory with a new one, e.g.

Hibernate Runtime Configuration
Dynamic EJB Tuning

but I do not know the side effects

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

JPA: adding objects to EntityManagerFactory programmatically
Programmatically load Entity classes with JPA 2.0?

A very complex scenario in which no solution was found

Dynamically creating an ORM entity class - NOT SOLVED

Another lead mentions dynamic JPA

How can I combine / extend continuity 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 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 looks like this project is no longer supported ...

+4
source share
1 answer

I see no other solution than reloading the whole context.

All other methods can lead to a memory leak (for example, to connections) or to all ClassNotFound classes and other things just an image of what happens during tx when you try to reconfigure only the entitymanager

One solution that I can think of is to "isolate" the entire bean of the data layer definition and replace the beans themselves, thereby avoiding a complete reload of the application.

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

you just need to make sure that you restart EntityManager + TX + DS (and maybe other things that I forgot)

NTN

0
source

Source: https://habr.com/ru/post/1388498/


All Articles