2010-11-29 58 views
20

当使用hibernate/jpa时,我无法在spring中自动创建表。如何在spring/hibernate/jpa中自动创建表的工作?

这里是我的配置文件:

<?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> 

任何想法可能是错误的? 感谢您的任何帮助:)。

回答

12

尝试以下操作:


<?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> 

这对我的作品。

+1

是'persistence.xml`仍需要? – 2014-11-12 02:00:45

+0

persistence.xml对于最近版本的Hibernate(和Spring来说)永远不需要。一切都可以使用注释来完成。 – kervin 2015-05-05 15:55:08

1

可悲的是这两种解决方案并没有为我工作:(。“hibernate.hbm2ddl.auto =更新”也将是好的,因为它应该创建表,如果他们不存在。

看来,所有来自persistence.xml的属性设置都被认为是用户名和数据库提供者被正确设置的。很遗憾,这些表不是在启动时创建的,这会导致我的测试用例失败,因为SELECT语句会引发错误...

因为你可以看到,我已经将连接url设置为使用本地文件数据库,这允许我在测试运行后查看数据库日志,但是在测试运行后日志文件仍然是空的,即使没有写入错误: (。

2

在我的休眠,default.cfg.xml我用

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

它完美地工作,所以配置文件是如下

<?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=.... 
7

我有完全一样的问题...

注释掉财产

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

但设置

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

为我工作。我正在使用hsqldb。

我注意到在日志中,无论我在 persistence.xml中设置的模式是否设置为更新。

persistence.xml中的值实际上被拾取,但从未应用。 我正在使用Spring 3.0。6和Hibernate 4

-3

在我的情况,并没有被创建的表,因为在那里没有被标注为@Entity

-2

与@Table注释的对象也许晚,但今天我有同样的问题,当我在写一些遗留应用程序的测试。

我正在使用spring 2.5,和HSQL数据库。

为了解决这个问题,我将数据库从HSQL更改为H2,将数据源从org.springframework.jdbc.datasource.DriverManagerDataSource更改为org.apache.commons.dbcp.BasicDataSource

弹簧-context.xml的样子:

<?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> 

persistence.xml中的样子:

<?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> 

我希望它能帮助。

0

这为我工作

<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> 

如果我改变值为false这样<property name="generateDdl" value="false" />然后我得到SqlExceptionHelper:144 - Table not found in statement

相关问题