JPA脚本生成重复语句

时间:2017-06-14 17:20:21

标签: java spring hibernate jpa

我在项目中使用带有spring的JPA(hibernate实现)。运行自动化测试时,persistance.xml通过spring配置,以生成数据库的创建和删除脚本。

只有一个名为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

有人知道造成这种行为的原因是什么吗?

1 个答案:

答案 0 :(得分:2)

经过进一步调查后,似乎问题与休眠有关。

我正在使用版本5.2.10.Final of hibernate。但是,当我切换回版本4.3.6.HinalnateFinal一切正常。