мне нужно, что спящий режим чтения файла SQL перед началом тестов JUnit, так что я сделал следующую конфигурацию в persistence.xml:hibernate.hbm2ddl.import_files в 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>
В 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 и Spring 3.0.5.
Сначала вы не указали путь к persistence.xml в свой application-context.xml. Во-вторых, покажите, пожалуйста, ваш тест junit, нам нужно посмотреть, как вы его настроили. – mvb13
Привет @ mvb13. Спасибо за ответ. Я думаю, что нет необходимости указывать путь к persistence.xml в моем приложении-context.xml. Ранние тесты Junit. Они перестали работать, потому что были созданы триггеры для запуска последовательностей и, таким образом, создания автоматического приращения. Перед запуском тестов junit я должен создать эти триггеры с помощью load.sql. – Leo
Я попросил вас настроить Junit (через аннотации), чтобы узнать, как указать путь к persistence.xml. Если вы не укажете его, вы будете запускать тесты без спящего режима. – mvb13