How to use JPA / Hibernate in Plugin?

Hi,

I am writing a Plugin that gets some data out of a database in order to show it as code documentation.

The Problem is that I cannot create an EntityManagerFactory:

javax.persistence.PersistenceException: No Persistence provider for EntityManager named myDatabase
     at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:56)
     at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:34)
     at org.myorganization.DatabaseModelDocumentationProvider.initializeDocumentation(DatabaseModelDocumentationProvider.java:31)
        ...


When running the code in unit tests there is no problem. The exception only appears when running the plugin in the target platform.

I have tried to locate the persistence.xml in both /META-INF and /src/META-INF but neither works.

Thanks for help!

1 comment

Hi again,

I managed to solve the problem by setting the contextClassLoader to the ClassLoader of the invoking class. I did this because the contextClassLoader does not know the classes from the libraries.

So the code looks just something like this:

ClassLoader old = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(MyClass.class.getClassLoader);

/* JPA/Hibernate code ... */

Thread.currentThread().setContextClassLoader(old);


Cheers
Yann

0

Please sign in to leave a comment.