Results 1 to 2 of 2

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

  1. #1

    Default 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. #2
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,793

    Default

    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.
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •