0

I have a problem that i'm stuck with and i would like some help.

I got a web-app and Hibernate. I want to run my maven test from a different database schema, so i've wrote two different persistence.xml files changing only this line :

<property name="hibernate.default_schema" value="myschema"/>

into this one :

<property name="hibernate.default_schema" value="myschematest"/>

I've placed it in the test folder so the new persistence.xml will be loaded when i run my Maven Test. Everything goes well except for this thing:

Considering that my persistence.xml is in the src/test forlder i don't know how to point my classes that are in the /src/main/... ?

src
--- main
------ java
--------- beans
------------ Application.java
------------ Derogation.java
------ resources
--------- META-INF
------------ persistence.xml
--- test
------ resources
--------- META-INF
------------ persistence.xml

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="calamar" transaction-type="RESOURCE_LOCAL"> 
        <provider>org.hibernate.ejb.HibernatePersistence</provider>        
        <class>com.calamar.beans.Application</class>
        <class>com.calamar.beans.Derogation</class>
        [...]

Exception

testModifyApplication(test.TestApplicationService)  Time elapsed: 0.06 sec  <<< ERROR!
java.lang.IllegalArgumentException: Unknown entity: beans.Application

The line causing the exception

entityManager.persist(application); 

Can someone help me?

Antoine Grenard
  • 1,712
  • 3
  • 21
  • 41
  • Can you post the exception trace you are getting after what you have achieved till now. – mhasan Sep 20 '16 at 10:56
  • @mhasan I added it to the first post :) – Antoine Grenard Sep 20 '16 at 11:11
  • 1
    Maven expects a directory "resources" not "ressources". – Puce Sep 20 '16 at 11:12
  • @Puce that might be an answer – HRgiger Sep 20 '16 at 11:20
  • @Puce sorry it was a mistake while writing the first post my folder name is resources and not ressources (i've edited the post) – Antoine Grenard Sep 20 '16 at 11:30
  • There is no need to point to `src/main/java`. Both `src/main/java` (compiled) and `src/test/resources` (copied) will be on the test classpath. `beans.Application` - do you have this entity in persistence.xml? It seems your entities are in `beans.entities` package. – ike3 Sep 20 '16 at 12:16
  • i think that by `entities` he means all the entity classes that are located there. – Apostolos Sep 20 '16 at 12:19
  • @ike3 then why did i get an "unknown entity" when i load the persistence.xml from /src/test/ressources (Maven test) and no error when i load it from /src/main/ressources ? – Antoine Grenard Sep 20 '16 at 12:19
  • i guess you don't use spring, right? in order to have the `package-scan` enabled (just a thought) – Apostolos Sep 20 '16 at 12:21
  • @Apostolos i don't but i could use it if it can resolve my problem. How could i resolve my problem with it ? – Antoine Grenard Sep 20 '16 at 12:27
  • it's a whole new configuration. not that easy :) – Apostolos Sep 20 '16 at 12:42
  • can you post your test program? – Apostolos Sep 20 '16 at 12:49
  • @Apostolos I've edited the post to add the persistence.xml & the line causing the error. But as i said it is because the entity manager can't find my classes. If i move all my files to src/test/java there is no error anymore but i don't want to move the files everytime i run my test :/ – Antoine Grenard Sep 21 '16 at 06:19
  • `Unknown entity: beans.Application` but the package is `com.calamar.beans.Application`? – Apostolos Sep 21 '16 at 11:32
  • Hi @Apostolos, i've found a similar post on stackoverflow and found my answers. I've put my src/test/resources/persistance.xml in src/main/resources as persistance-test.xml and when i run my test i just replace persitance.xml by my persistance-test.xml and everything goes well. It is really messy but it work. http://stackoverflow.com/questions/385532/how-to-configure-jpa-for-testing-in-maven – Antoine Grenard Sep 22 '16 at 07:32
  • great! glad that you found a solution, even such one :) – Apostolos Sep 22 '16 at 07:39

0 Answers0