Search code examples
javajpanetbeanspersistencederby

Sometimes getting PersistenceException: No Persistence provider for EntityManager


There are many questions about this PersistenceException, but I have not seen some, where the specific line of code throws this exception only sometimes (but not randomly:-).

So, do you have any idea, why once my simple app finds the provider ok, but then later it won't?

NetBeans IDE 7.0.1, NetBeans Platform app, using persistence link library and Derby embbed.

I was trying this CRUD creatable capabilities "tutorial" by Geertjan, but i get the PersistenceException when saving new Trip:

at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source)
at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source)
at org.mv.mm.api.TripSearchDAO.createTransactionalEntityManager(TripSearchDAO.java:61)
at org.mv.mm.api.TripSearchDAO.create(TripSearchDAO.java:41)
at org.mv.mm.api.TripQuery$3.create(TripQuery.java:69)
[catch] at org.mv.mm.api.TripType.create(TripType.java:64)

But in "search" method of DAO class, there is this private method createTransactionalEntityManager called too and EntityManager correctly created. Why it might be?


Ok, the problem is similar like many others here (well, I have not solved it, but I will try again tomorrow). It fails every time. I have mislooked the try catch block, so I did not noticed the exception. It occurs every time the Persistence.createEntityManagerFactory("TripPU").createEntityManager() is called.


Solution

  • And the real solution to the real problem is also found: Really do check in those cases the name of your persistence unit in persistence.xml (like TripPU) if it is correct to the calling Persistence.createEntityManagerFactory("TripPU").createEntityManager();

    The PU is not same as the DB entity, so the tutorials might you confuse when you try to do a bit different example (different tables etc.).