Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JPA脚本生成重复语句_Java_Spring_Hibernate_Jpa - Fatal编程技术网

Java JPA脚本生成重复语句

Java JPA脚本生成重复语句,java,spring,hibernate,jpa,Java,Spring,Hibernate,Jpa,我正在一个项目中使用JPA(hibernate实现)和spring。运行自动测试时,通过spring配置persistence.xml,以生成数据库的创建和删除脚本 只有一个实体名为Book。这应该在创建脚本中创建一行以创建表Book,在拖放脚本中创建一行以拖放表Book 问题是,每次运行测试时,脚本都不会重新生成,而是会向脚本中添加一行新行。因此,如果我运行测试3次,脚本如下所示: create table Book (title varchar(255) not null, primary

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

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

有人知道是什么导致了这种行为吗

经过进一步调查,这个问题似乎与hibernate有关


我使用的是hibernate的5.2.10.Final版本。但是,当我切换回hibernate的4.3.6.Final版本时,一切都正常。

您是否尝试将@Transactional置于主方法之上?我正在使用@Transactional置于主方法之上。经过进一步调查,hibernate版本似乎存在问题。
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