0

I have a persistence.xml at src/main/resources/META-INF/ and another at src/test/... which differ only in the value of the database:

  <properties>
      <property name="hibernate.archive.autodetection" value="class, hbm"/>          
      <property name="hibernate.connection.url" value="jdbc:mysql://localhost/lanchecker-prod" />

and:

  <properties>
      <property name="hibernate.archive.autodetection" value="class, hbm"/>          
      <property name="hibernate.connection.url" value="jdbc:mysql://localhost/lanchecker-test" />

However I'm also using class autodetection which works fine for the main resource but fails when I use the test resource.

Is there any way to get this to work or am I obliged to use explicit class naming?

EDIT:

The linked suggestion doesn't actually work it it requires the project is built and tested as a .jar. In my case I'm attempting to do the testing under Eclipse, which can resolve the src/test/resources/META-INF/ location.

I tried adding:

<jar-file>${PROJECT_LOC}/src/main/java/biz/ianw/lanchecker/</jar-file>

but that resulted in:

Exception in thread "main" java.lang.ExceptionInInitializerError
...
Caused by: javax.persistence.PersistenceException: Unable to build entity manager factory
... 
Caused by: java.lang.IllegalArgumentException: Unable to visit JAR file:${PROJECT_LOC}/src/main/java/biz/ianw/lanchecker/. Cause: Illegal character in opaque part at index 6: file:${PROJECT_LOC}/src/main/java/biz/ianw/lanchecker/

And then:

<jar-file>C:\Users\Ian\git\LANchecker\src\main\java\biz\ianw\lanchecker\</jar-file>

which passed entity manager factory startup, but still failed later with:

Exception in thread "main" java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: RouteCabinFares is not mapped [select rcf from RouteCabinFares rcf]

(RouteCabinFares is one of the classes I'm attempting to autolocate).

Maybe this technique can only be used to autolocate in jar files?

Ian
  • 1,507
  • 3
  • 21
  • 36
  • Does this help? [No autodetection of JPA Entities in maven-verify](http://stackoverflow.com/questions/4885836/no-autodetection-of-jpa-entities-in-maven-verify) – K.Nicholas Dec 28 '15 at 19:03
  • That looks to be it, thanks. Didn't find that, my bad. Should I delete this question? – Ian Dec 28 '15 at 22:05
  • Glad that helped. Don't worry about the question, I flagged it as duplicate. The admins will decide whether to delete it or not. – K.Nicholas Dec 29 '15 at 14:54
  • Hi @Nicholas. Turns out this doesn't work after all. I've explained why above. – Ian Jan 07 '16 at 07:17
  • Well, sorry, I'm not sure. I had just done a quick search to get the above link. I put arquillian to use testing a web project and it was able to build a war file for testing. Maybe something like that will help, otherwise, I dunno. – K.Nicholas Jan 08 '16 at 02:02

1 Answers1

0

As far as I can tell what I'm trying to do here is not possible.

The best work around I've come up with is to remove the properties you wish to vary from the persistence.xml, in this case:

<property name="hibernate.connection.url" value="jdbc:mysql://localhost/lanchecker-test" />   

and specify them at runtime using code similar to:

Properties properties = new Properties();
properties.setProperty("hibernate.connection.url", "jdbc:mysql://localhost/lanchecker-test");  
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("lanchecker", properties);

The actual property values can then be injected at runtime via Spring. This involves separating standard properties into persistence.xml and varying properties into spring.xml and is, to my mind, extremely hokey.

I get the impression that Spring Boot might offer a simpler solution but at the expense at another framework so, for the moment, the above is what I'm going for.

Ian
  • 1,507
  • 3
  • 21
  • 36