开发者

How to get automatic table creation working in spring / hibernate / jpa?

I can't get automatic table creation working in spring when using hibernate / jpa.

Here are my config files:

<?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="naveroTest">
     <provider>org.hibernate.ejb.HibernatePersistence</provider>
     <class>xxx</class>
        ...


        <properties>
            <property name="hibernate.archive.autodetection" value="class, hbm"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
   <property name="hibernate.connection.url" value="jdbc:hsqldb:file:/tmp/naveroTestDB"/>
            <property name="hibernate.connection.username" value="sa"/>
            <property name="hibernate.connection.password" value=""/> 
            <property name="hibernate.hbm2ddl.auto" value="create"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
        </properties>
    </persistence-unit>
</persistence>

context.xml

   <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns="http://www.springframework.org/schema/beans" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                  开发者_运维知识库          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
                           http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.4.xsd">

     <!-- For auto creation of tables -->
     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
      <property name="url" value="jdbc:hsqldb:file:/tmp/naveroTestDB" />
      <property name="username" value="sa" />
      <property name="password" value="" />
     </bean>

     <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
      <property name="dataSource" ref="dataSource" />
      <property name="loadTimeWeaver">
       <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
      </property>
      <property name="jpaVendorAdapter">
       <bean id="jpaAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="generateDdl" value="true" />
        <property name="databasePlatform" value="org.hibernate.dialect.HSQLDialect" />
        <property name="showSql" value="true" />
       </bean>
      </property>
     </bean>

     <bean id="PictureBean" class="de.navero.server.bl.PictureBean">
      <property name="entityManagerFactory"><ref local="entityManagerFactory" /></property>
     </bean>

    </beans>

Any ideas what may be wrong? Thanks for any help :).


try the following:


<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://.sun.com/xml/ns/persistence
  http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
    <persistence-unit name="naveroTest" transaction-type="RESOURCE_LOCAL" />
</persistence>

<?xml version="1.0" encoding="UTF-8"?>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
    </property>
    <property name="jpaProperties">
        <props>
...
            <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
        </props>
    </property>
    <property name="dataSource" ref="dataSource" />
</bean>

This works for me.


I had exactly the same issue...

Commenting out property

    <!--property name="generateDdl" value="true"--> in the JpaAdapter, 

but setting

<property name="hibernate.hbm2ddl.auto" value="create"/> in persistence.xml 

worked for me. I am using hsqldb.

I noticed in the logs that the mode was beign set to update no matter what I set in persistence.xml.

The value in persistence.xml was actually picked up but never applied. I am using Spring 3.0.6 and Hibernate 4


Can you try changing the generateDdl property to false on HibernateJpaVendorAdapter in your spring config file.

Seems to conflict with the hibernate hibernate.hbm2ddl.auto property

See https://jira.springframework.org/browse/SPR-6836 for more info.


In My hibernate-default.cfg.xml i used

   <property name="hibernate.hbm2ddl.auto">update</property>

and it worked perfectly, so the config file was as follows

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="dialect">
      org.hibernate.dialect.MySQLDialect
    </property>
    <property name="current_session_context_class">thread</property>
    <!-- When an HQL statement declares a true/false, replace with the standard Y/N -->
    <property name="hibernate.query.substitutions">true 'Y', false 'N'</property>
    <!-- A useful property do debugging queries.  Besure sure it is false or commented out when going PROD -->
    <!--        <property name="hibernate.show_sql">true</property>   -->
    <!-- Format the printed out SQL generated by Hibernate -->
    <property name="hibernate.format_sql">false</property>
    <!-- If enabled, Hibernate will collect statistics useful for performance tuning - JMX -->
    <property name="hibernate.generate_statistics">false</property>

    <property name="hibernate.hbm2ddl.auto">update</property>

    <mapping class=....


sadly both solutions didn't work for me :(. "hibernate.hbm2ddl.auto=update" would be also ok, as it should create the tables if they're not present.

It seems that all property settings from the persistence.xml are recognized as username and database provider are set correctly. Sadly the tables aren't created at startup which causes my testcase to fail as the SELECT statement throws an error ...

As you can see, I've set the connection url to use a local file database, which allows me to review the database log after the test run. But the log file is still empty after the test run, even no error is written to it :(.


This worked for me

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
        <property name="url" value="jdbc:hsqldb:mem://productDb" />
        <property name="username" value="sa" />
        <property name="password" value="" />
    </bean>
<property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="generateDdl" value="true" />
                <property name="showSql" value="true" />
            </bean>
        </property>

If I change value to false like this<property name="generateDdl" value="false" /> then I get SqlExceptionHelper:144 - Table not found in statement


Maybe to late, but today I had the same problem when I was writing some tests for a legacy app.

I was using spring 2.5, hibernate3, and HSQL database.

To solve it I changed the database from HSQL to H2 and the datasource from org.springframework.jdbc.datasource.DriverManagerDataSource to org.apache.commons.dbcp.BasicDataSource.

The spring-context.xml looks like:

<?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:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="org.h2.Driver"/>
        <property name="url" value="jdbc:h2:mem:test"/>
        <property name="username" value="sa"/>
        <property name="password" value=""/>
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
        </property>
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="dataSource" ref="dataSource"/>
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

    <tx:annotation-driven/>
...
</beans>

The persistence.xml looks like:

<?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="TestPU" transaction-type="RESOURCE_LOCAL">
        <properties>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
        </properties>
    </persistence-unit>

</persistence>

I hope it helps.


In my case, the tables were not been created because the objects annotated with @Table where not annotated as @Entity

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜