2010-11-29 57 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

相關問題