3

I'm trying to use jpa in a desktop application. I keep getting the error No Persistence provider for EntityManager named <unit> and I suspect that my persistence.xml cannot be located.

I've placed it in the <project>/src/META-INF/persistence.xml

I have the feeling that jpa cannot find this file, but I do not known how to troubleshoot.

How can I specify where the persistence.xml file is stored and how can I check if it is actually read?

Stacktrace:

javax.persistence.PersistenceException: No Persistence provider for EntityManager named myunit
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
    at myapplication.MyEntity.testAddEntity(HistoryAction.java:85)
Roalt
  • 8,330
  • 7
  • 41
  • 53
  • 1
    Which JPA persistence provider do you use? What's the content of the persistence.xml file and what's the stack trace of the exception? – JB Nizet Nov 01 '11 at 08:56
  • @JB Nizet: The stacktrace only says the above message. But even if I put illegal characters in my .xml file, I do not get an (other) error message. So I really want to know whether the application finds and reads my persistence.xml file or not. – Roalt Nov 01 '11 at 09:36
  • The complete stack trace would help diagnosing this. But if you don't want to be helped, you're free not to answer. Are you sure the META-INF folder with the persistence.xml is in the classpath? – JB Nizet Nov 01 '11 at 09:45
  • @JB Nizet: I sure want to be helped, but I wanted to state a general question so it may be useful for others as well, except for the specific problem: my code does not work, please fix it. I added the stacktrace, but I tried a lot of standardized persistence.xml files and none of them worked (even some that worked in other projects). – Roalt Nov 01 '11 at 09:54
  • 1
    I think the problem is rather that your application doesn't find the jpa implementation (hibernate, eclipselink) than the file persistence.xml. Put the term _No Persistence provider for EntityManager_ into the SO search box. There are a lot of similar questions. – Matt Handy Nov 01 '11 at 11:02
  • 1
    Additional to those above, "/src/META-INF/persistence.xml" has no bearing on what is in the CLASSPATH. Your invocation of your app defines what is in the CLASSPATH, and "/META-INF/persistence.xml" is the only place it will look for it at runtime. As per the JPA spec! – DataNucleus Nov 01 '11 at 11:51
  • @Matt Handy: Thank you, it did not find the hibernate3.jar (quite essential!). It was very hard to find out what is wrong with the limited feedback from this stack trace. – Roalt Nov 01 '11 at 13:58
  • You are welcome. So I will add an answer in competition to yours ;-) – Matt Handy Nov 01 '11 at 19:01

2 Answers2

3

I think the problem is rather that your application doesn't find the jpa implementation (hibernate, eclipselink) than the file persistence.xml.

See this related question and answers.

Community
  • 1
  • 1
Matt Handy
  • 29,855
  • 2
  • 89
  • 112
1

You get this error if the JPA cannot find a correct jpa implementation (e.g. hibernate, eclipselink).

Make sure you include the right .jar's to your build path that contain the JPA implementation you are using.

(Answer given in a comment by Matt Handy)

Roalt
  • 8,330
  • 7
  • 41
  • 53