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
有人知道可能会导致这种行为吗?
您是否尝试将@Transactional放在主要方法上? – German
我在主要方法上使用@Transactional。进一步调查后,似乎是休眠版本的问题。 – xavipen