1

I need that hibernate read a sql file before start junit tests, so I did the following configuration 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>

The applicationContext.xml to load spring context:

<?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 tests extends the class:

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

When I run the junit tests, the file load.sql is not been imported and no errors are shown. I'm using Hibernate 4 and Spring 3.0.5.

Leo
  • 93
  • 1
  • 2
  • 9
  • First you didn't specify path to persistence.xml in your application-context.xml. Second, show please your junit test, we need to see how you configured it. – mvb13 Nov 07 '13 at 13:19
  • Hi @mvb13. Thanks for reply. I think that is not necessary to specify path to persistence.xml in my application-context.xml. Junit tests were working before. They stoped to work because some triggers were created to start the sequences and thus make the auto increment. Before running the junit tests I have to create these triggers with load.sql. – Leo Nov 07 '13 at 13:45
  • I asked you for Junit configuration (through annotations), to see how you specify path to persistence.xml. In case if you don't specify it you will run tests without Hibernate. – mvb13 Nov 07 '13 at 14:11
  • I edited my question. How can I specify path to persistence.xml? Can you show me an example? – Leo Nov 07 '13 at 14:33

3 Answers3

3

I solved my problem by putting the load.sql in src/test/resource/ folder. This way isn't necessary to specify in persistence.xml where the sql file is.

Leo
  • 93
  • 1
  • 2
  • 9
0

I think(I usually use it) you should include it in your applicationContext.xml:

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

Also you can specify several configuration files in test @ContextConfiguration:

@ContextConfiguration(locations = { "classpath:/applicationContext1.xml", ... ,"classpath:/applicationContextn.xml" })
mvb13
  • 1,514
  • 3
  • 18
  • 33
  • `org.xml.sax.SAXParseException; lineNumber: 7; columnNumber: 16; cvc-elt.1: Cannot find the declaration of element 'persistence'.` I think it can't parse because persistence.xml is different from applicationContext.xml. – Leo Nov 07 '13 at 15:33
  • Persistence.xml that you exposed me is only a snippet of configuration. It isn't enough. See how it should be written here http://docs.spring.io/spring-roo/reference/html/base-persistence.html – mvb13 Nov 07 '13 at 15:38
  • I edited my question again. I did exactly the same code you sent and I got the same error. – Leo Nov 07 '13 at 15:51
  • From your code I see that you didn't close with – mvb13 Nov 07 '13 at 15:57
  • In persistence.xml I didn't see any mistakes. Show me please your full applicationContext.xml – mvb13 Nov 07 '13 at 16:16
0

Just put a file with name import.sql on src/test/resources directory.

The default value of property javax.persistence.hibernate.hbm2ddl.import_files is import.sql.

Check: http://docs.jboss.org/hibernate/orm/5.3/userguide/html_single/Hibernate_User_Guide.html