Loading...
середу, 12 березня 2014 р.

JPA for testing in Maven: persistence.xml in test/resources

For Maven testing with JPA we need to have persistence.xml file located in the in src/test/resources and that is not good sometimes, because we will have two persistence.xml files (one in src/main/resources and one in src/test/resources), configured to different DBs (I guess, you would like to use embedded-DB for testing purposes).
In this case you may get error like this: java.lang.IllegalArgumentException: Unknown entity: com.your.package.SomeEntity
There are two solutions:
1. Use maven-antrun-plugin to copy appropriate persistence.xml to the appropriate location (suitable for maven, but not for running tests under IDE).
2. List all managed entities inside the test persistence.xml like the following:
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
    <persistence-unit name="hibernatePersistenceUnit" transaction-type="RESOURCE_LOCAL">
        <class>com.your.package.SomeEntity</class>
        <class>com.your.package.AnotherEntity</class>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
            <property name = "hibernate.show_sql" value = "true" />
        </properties>
    </persistence-unit>
</persistence>

0 коментарі:

 
TOP