8

I am using eclipse. I am trying to debug a failure to find the persistence unit that is indeed located in the directory structure of my eclipse project.

Where does EntityManagerFactory javax.persistence.Persistence.createEntityManagerFactory(String persistenceUnitName, Map properties) look for the persistence unit?

When I ask where it looks, I mean all the possible resources that could get searched, including eclipse configurations, properties in persistence.xml if the file is found, resources searched by included JARs, etc. To explore how this works, I am executing a test program by right clicking on a Main.java class located in the root (src/main/java) of an eclipse project.

The answer might help me figure out how to configure the eclipse project so that EntityManagerFactory javax.persistence.Persistence.createEntityManagerFactory(String persistenceUnitName, Map properties) can find the persistence unit.

CodeMed
  • 9,527
  • 70
  • 212
  • 364
  • In one of linked [questions](http://stackoverflow.com/q/16427271/1391249), (most probably) the cause of the stated exception was that I misconfigured `databasePlatform` as `org.hibernate.dialect.HSQLDialect` in the given XML file in that question though I was using MySQL 5.x. It should have been `org.hibernate.dialect.MySQLDialect` or version-restricted/version-specific - `org.hibernate.dialect.MySQL5Dialect` (I do not remember precisely till date what exactly was wrong other than this, since I work mostly with EJBs nowadays). – Tiny Oct 14 '14 at 23:11

1 Answers1

6

It searches your persistence.xml, located inside your META-INF dir. This folder has to be at the same level as your root package.

In a maven project, the folder would be in /src/main/resources/META-INF/persistence.xml

mendieta
  • 3,470
  • 2
  • 20
  • 23
  • Ok, your META-INF folder has to be inside src/main/resources – mendieta Oct 14 '14 at 19:57
  • How are you executing your example? If you are running from a maven test package, then maybe you have to declare the persistence in src/test/resources.. But to be honest, I'm not so sure if it is needed. – mendieta Oct 14 '14 at 20:19
  • +1 for your help so far, even though I am looking for a deeper answer that can help people who google search this to really understand what is going on inside the inner plumbing. – CodeMed Oct 14 '14 at 20:26
  • hyperjaxb creates a persistence.xml and places it in your target folder.. maybe this is interfering with the one placed in the resources folder.. maybe you can try removing the persistence from the output dir, and run your example again – mendieta Oct 14 '14 at 20:39
  • My issue was created by moving around things that were created by a combination of hyperjaxb and the pom.xml file that built the hyperjaxb project. I resolved my issue by making changes to the project build before it got into eclipse. I am going to mark yours as the answer to thank you for the time you put into this. – CodeMed Oct 15 '14 at 01:27