Announcement Announcement Module
Collapse
No announcement yet.
Spring 3.x JPA2 and drop-create tables : howto? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring 3.x JPA2 and drop-create tables : howto?

    What can be used in a spring bean xml configuration file to specify that JPA2 should create the needed tables when it runs? (Like the hibernate "drop-create" or "create") This is running in a JUnit test with memory resident DB (hsqldb AND H2 both are tested separately) Thus the desire is for JPA to create the tables when the db datasource is connected.

    An additional desire is to do this:
    1. purely in spring configs (see below)
    2. preferably with JPA2 only configuration that is neutral to the JPA provider (toplink, hibernate, openjpa etc...) The goal here being no, or minimal, changes to move from one JPA provider to another.

    The goal is to keep persistence.xml almost empty and use Spring for all the configuration so that PropertyPlaceHolder's can be used for dynamic configuration and avoiding the need to edit the persistence.xml (or springbeans.xml) inside deployed JAR files. Or worse yet.. A WAR with a JAR with a persistence.xml that would need changing to move from one DB or JPA provider to another.

  • #2
    FYI: persistence.xml - JPA 2.0

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="2.0" 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_2_0.xsd">
      <persistence-unit name="testPU" transaction-type="RESOURCE_LOCAL">
            <shared-cache-mode>ALL</shared-cache-mode>
            <validation-mode>CALLBACK</validation-mode>
            <properties>
        </properties>
        
        </persistence-unit>
    </persistence>

    Comment


    • #3
      FYI: spring beans.xml : spring 3.x

      Code:
      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.0.xsd  http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
      
       <!-- holding properties for database connectivity /-->
          <context:property-placeholder location="classpath:jdbc.properties"/>
         <!-- enabling annotation driven configuration /-->
          <context:annotation-config/>
          <context:component-scan base-package="com.hanaden.demo"/>
          <tx:annotation-driven transaction-manager="transactionManager"/>
      
          <bean
             class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
             
      <!--       class="org.springframework.jdbc.datasource.DriverManagerDataSource">-->
      <!--    class="com.mchange.v2.c3p0.ComboPooledDataSource">-->
          <bean id="dataSource"
             class="org.springframework.jdbc.datasource.DriverManagerDataSource">
              <property name="driverClassName" value="${jdbc.driverClassName}" />
              <property name ="url" value="${jdbc.url}" />
              
          </bean>
      
          <bean id="transactionManager"
              class="org.springframework.orm.jpa.JpaTransactionManager">
              <property name="dataSource" ref="dataSource" />
              <property name="entityManagerFactory" ref="entityManagerFactory" />
              <property name="jpaDialect">
      <!--    http://forum.springsource.org/showthread.php?t=32205 -->
      <!--            <bean class="org.springframework.orm.jpa.vendor.OpenJpaDialect"/>-->
                  <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
              </property>
      <!--    http://forum.springsource.org/showthread.php?t=32205 -->
      
          </bean>
      
          
          <bean id="entityManagerFactory"
             class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
              <property name="persistenceUnitName" value="testPU"/>
              <property name="dataSource" ref="dataSource"/>
              <property name="loadTimeWeaver">
                  <bean
                   class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
              </property>
              <property name="jpaVendorAdapter" >
                  <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                      <property name="databasePlatform" value="org.hibernate.dialect.H2Dialect" />
                      <property name="showSql" value="true" />
                      <property name="generateDdl" value="true" />
      <!--                <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />                -->
                  </bean>
              </property>
              <property name="jpaProperties">
                  <props>
      <!--                <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
                      <prop key="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</prop>
                      <prop key="hibernate.connection.password"></prop>
                      <prop key="hibernate.connection.url">jdbc:hsqldb:file:target/hsqldb/data</prop>
                      <prop key="hibernate.connection.username">sa</prop>-->
                      <prop key="hibernate.hbm2ddl.auto">create</prop>
                      <prop key="hbm2ddl.auto">create</prop>
                  </props>
      
              </property>
          </bean>       
                           
      </beans>

      Comment

      Working...
      X