Announcement Announcement Module
Collapse
No announcement yet.
Spring + Hibernate + JPA: How to reload EntityManagerFactory at runtime Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring + Hibernate + JPA: How to reload EntityManagerFactory at runtime

    Hi guys,

    I am using Hibernate 3.5.6 + Spring 3.0.5 (and JPA)

    I am currently searching for a way to reload my hibernate mapping information at runtime.

    All hibernate classes + annotations are in a seperate .jar file which I use in my persistence.xml (JPA) like this

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <persistence
    xmlns="http://java.sun.com/x/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>
    To access the DB I use Springs dependency injection to inject a JPA entity Manager.

    I define my bean like this

    Code:
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
       <property name="persistenceXmlLocation" value="/WEB-INF/config.incb/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.unodc.incbszdb.hibernate.HibernateExtendedJpaDialect" />
       </property>
       <property name="jpaProperties">
         <props>
           <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialectWithGroupConcat</prop>
           <prop key="hibernate.hbm2ddl.auto">none</prop>
           <prop key="hibernate.show_sql">true</prop>
         </props>
       </property>      
    </bean>
    and then I inject the entity manager like this

    Code:
    @PersistenceContext
    private EntityManager entityManager;
    Now every time I add a new property to my mappings I have to restart my server. Obviously a situation I can not really live with.

    I would rather just refresh my mapping information and keep the server up and running.

    Can somebody maybe give me a hint how I can change the mapping without restarting the server...

    Any help is more than welcome.

    PS:
    More information can also be found in a Stackoverflow thread I started.

    There are also some links to similar questions from Hibernate/Spring and Stackoverflow forum, but they didn't provide a proper solution.

    http://stackoverflow.com/questions/8...ory-at-runtime

  • #2
    Hello

    Now every time I add a new property to my mappings I have to restart my server. Obviously a situation I can not really live with.
    I dont know in a 100% if OSGi fit here. But is an option

    Can somebody maybe give me a hint how I can change the mapping without restarting the server...
    Even if OSGi is the solution, consider how the transactions are applied in real time for the "new" DAO method control since you have new fields. I consider that a huge concern. You could get corruption in data.

    Comment

    Working...
    X