I am trying to find out a way to auto detect my entities while runing JUnit tests. It seems tha this is only possible when the generated entity classes are located in the same archive that my persistence.xml.
I have a different persistenceTESTE.xml and applicationContextTESTE.xml files for my JUnit tests located in src/test/resources/META-INF and I tried several different configurations in both files but nothing works and I aways get a "java.lang.IllegalArgumentException: Unknown entity: ...".
src/test/resources/META-INF/applicationContext-TESTE.xml:
<context:component-scan base-package="model.*" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="META-INF/persistenceTESTE.xml" />
<!-- As defined in /src/test/resources/META-INF/persistenceTESTE.xml -->
<property name="persistenceUnitName" value="mfspuTESTE" />
<property name="packagesToScan">
<array>
<value>model.finance</value>
</array>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven />
src/test/resources/META-INF/persistenceTESTE.xml:
<persistence-unit name="mfspuTESTE" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<!-- <class>model.finance.Sale</class> -->
<properties>
<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbc.JDBCDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:hsql://localhost/mfsdbUTeste" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
<property name="connection.pool_size" value="2"/>
<property name="show_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
</properties>
</persistence-unit>
Important! It works fine if I just change the location of persistenceTESTE.xml from src/test/resources/META-INF/ to src/main/resources/META-INF/
The thing that most intrigues me is that the classes can be seen from test classes but the entities can not be auto detected when I change the location on persistence.xml to test/resources/META-INF.