2017-06-14 127 views
1

我在項目中使用JPA(hibernate實現)和spring。在運行自動測試時,persistance.xml通過spring進行配置,以生成數據庫的創建和刪除腳本。JPA腳本生成複製腳本

只有一個實體叫做Book。這應該在創建腳本中創建一行以創建表Book,並在下拉腳本中的一行刪除表Book

問題是,我每次運行測試腳本都不會重新生成,而是將新行添加到腳本中。所以,如果我運行測試3次劇本的樣子:

create table Book (title varchar(255) not null, primary key (title)) 
create table Book (title varchar(255) not null, primary key (title)) 
create table Book (title varchar(255) not null, primary key (title)) 

這些都是持久性配置值

<!-- C3p0 datasource with connection pool configuration --> 
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> 
    <property name="driverClass" value="${jdbc.driverClassName}" /> 
    <property name="jdbcUrl" value="${jdbc.url}" /> 
    <property name="user" value="${jdbc.username}" /> 
    <property name="password" value="${jdbc.password}" /> 
    <property name="maxPoolSize" value="20" /> 
    <property name="minPoolSize" value="5" /> 
    <property name="maxStatements" value="50" /> 
</bean> 

    <bean id="EntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="jpaVendorAdapter" ref="JpaAdapter" /> 
    <property name="persistenceUnitName" value="com.test.domain.PU" /> 
    <property name="jpaPropertyMap"> 
     <map> 
      <entry key="javax.persistence.schema-generation.database.action" value="${jpa.database.action:none}"/> 
      <entry key="javax.persistence.schema-generation.scripts.action" value="${jpa.scripts.action:none}"/> 
      <entry key="javax.persistence.schema-generation.scripts.drop-target" value="${jpa.scripts.drop-target:target/generated-resources/schemagen/db/drop.ddl}"/> 
      <entry key="javax.persistence.schema-generation.scripts.create-target" value="${jpa.scripts.create-target:target/generated-resources/schemagen/db/create.ddl}"/> 
      <!-- If present, a ddl script is loaded to init the db. Used only during development. For production a specific script will be provided. 
       The script is activated on create, drop-and-create, and create-and-drop options for database schema creation -->      
      <entry key="javax.persistence.sql-load-script-source" value="${jpa.sql-load-script-source:sql/init_db.ddl}"/> 
     </map> 
    </property> 
</bean> 

而且在測試過程中使用的屬性文件是:

jdbc.driverClassName=org.hsqldb.jdbc.JDBCDriver 
jdbc.url=jdbc:hsqldb:mem:testdb 
jdbc.username=sa 
jdbc.password= 

jdbc.showSql=true 
jpa.database.action=drop-and-create 
jpa.scripts.action=drop-and-create 

有人知道可能會導致這種行爲嗎?

+0

您是否嘗試將@Transactional放在主要方法上? – German

+0

我在主要方法上使用@Transactional。進一步調查後,似乎是休眠版本的問題。 – xavipen

回答

2

經過一番進一步的調查,似乎這個問題與冬眠相關。

我正在使用版本5.2.10.Hibernate的最終版本。但是,當我切換回版本4.3.6.Hibernate一切正常工作正常。