Domanda

Ho bisogno che Hibernate abbia letto un file SQL prima di iniziare i test Junit, quindi ho fatto la seguente configurazione in 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 per caricare il contesto a molla:

<?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>

I test Junit estendono la classe:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/applicationContext.xml" })
@TestExecutionListeners(inheritListeners = false, listeners = {
    TransactionalTestExecutionListener.class, DependencyInjectionTestExecutionListener.class })
@TransactionConfiguration(defaultRollback = true)
@Transactional
public abstract class UnitTestConfiguration {}

Quando eseguo i test JUnit, il file load.sql non è stato importato e non vengono mostrati errori. Sto usando Hibernate 4 e Spring 3.0.5.

È stato utile?

Soluzione

Ho risolto il mio problema mettendo il load.sql in src/test/resource/ cartella. In questo modo non è necessario per specificare persistence.xml dove si trova il file SQL.

Altri suggerimenti

Penso che (di solito lo uso) dovresti includerlo in ApplicationContext.xml:

<import resource="classpath:/path_to_persistence/persistence.xml" />

Inoltre è possibile specificare diversi file di configurazione nel test @ContextConfiguration:

@ContextConfiguration(locations = { "classpath:/applicationContext1.xml", ... ,"classpath:/applicationContextn.xml" })

Basta mettere un file con il nome import.sql Su src/test/resources directory.

Il valore predefinito della proprietà javax.persistence.hibernate.hbm2ddl.import_files èimport.sql.

Dai un'occhiata: http://docs.jboss.org/hibernate/orm/5.3/userguide/html_single/hibernate_user_guide.html

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top