我需要hibernate在开始junit测试之前读取一个sql文件,所以我在persistence.xml中做了以下配置:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.username" value="sa" />
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver" />
<property name="hibernate.connection.password" value="" />
<property name="hibernate.connection.url" value="jdbc:hsqldb:hsql://localhost:9001/test" />
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.id.new_generator_mappings" value="true" />
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="hibernate.hbm2ddl.import_files" value="/META-INF/load.sql" />
</properties>
</persistence-unit>
</persistence>加载spring上下文的applicationContext.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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="com.test" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="test" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"></bean>
</beans>Junit测试扩展了类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/applicationContext.xml" })
@TestExecutionListeners(inheritListeners = false, listeners = {
TransactionalTestExecutionListener.class, DependencyInjectionTestExecutionListener.class })
@TransactionConfiguration(defaultRollback = true)
@Transactional
public abstract class UnitTestConfiguration {}当我运行junit测试时,没有导入文件load.sql,也没有显示错误。我使用Hibernate 4和Spring3.0.5。
发布于 2013-11-21 13:02:08
我通过将load.sql放在src/test/resource/文件夹中解决了问题。这种方式不需要在persistence.xml中指定sql文件所在的位置。
发布于 2013-11-07 15:11:48
我认为(我通常使用它)您应该将它包括在您的applicationContext.xml中:
<import resource="classpath:/path_to_persistence/persistence.xml" />还可以在测试@ContextConfiguration中指定几个配置文件。
@ContextConfiguration(locations = { "classpath:/applicationContext1.xml", ... ,"classpath:/applicationContextn.xml" })发布于 2018-08-22 17:53:20
只需将一个名为import.sql的文件放在src/test/resources目录中即可。
属性javax.persistence.hibernate.hbm2ddl.import_files的默认值是import.sql。
检查:Guide.html
https://stackoverflow.com/questions/19835576
复制相似问题