2

so i'm trying to use EclipseLink ORM with a Bukkit plugin.

I know there is already a ORM built-in to Craftbukkit (Ebean) but this one doesn't match my needs, it's too limited for entities Inheritance. That's why I turn to EclipseLink.

The problem is I can't initialize an EntityManagerFactory inside my Bukkit plugin : It seems that JPA can't find the META-INF/persistence.xml inside my plugin JAR. I guess this is a classpath issue, and JPA try to find the persistence file using the Craftbukkit classpath instead of my plugin classpath but I have no idea how to solve this.

So here is my plugin main class :

public class MyPlugin extends JavaPlugin{
     @Override
     public void onEnable(){
         EntityManagerFactory factory = Persistence.createEntityManagerFactory("persistunit");
         EntityManager em = factory.createEntityManager();
         // ...
     }

     // ...
}

And my persistence.xml file (which is located in the META-INF folder of my plugin JAR) :

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="persistunit" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://......"/>
      <property name="javax.persistence.jdbc.user" value="......."/>
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
      <property name="javax.persistence.jdbc.password" value="........."/>
    </properties>
  </persistence-unit>
</persistence>

And that's the exception thrown when running :

Error occured while enabling MyPlugin...
javax.persistence.PersistenceException: No Persistence provider for EntityManager named persistunit
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:89) ~[craftbukkit.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:60) ~[craftbukkit.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]

Thanks in advance for your help !

Tlokuus
  • 197
  • 2
  • 8
  • The errors states it can't find a provider for the persistence unit - how are you adding the EclipseLink.jar to the classpath? – Chris Oct 27 '14 at 13:13
  • In the Class-Path section of my MANIFEST.MF. I tried to call some methods of EclipseLink and it works, so it's correctly added. – Tlokuus Oct 27 '14 at 14:48
  • But, I solved my problem with another way : Using Hibernate instead of EclipseLink because Hibernate allows to choose specific location for his config file and it's works fine ! :) Thanks you anyway. – Tlokuus Oct 27 '14 at 14:51
  • EclipseLink does as well: https://www.eclipse.org/eclipselink/api/2.0/org/eclipse/persistence/config/PersistenceUnitProperties.html#ECLIPSELINK_PERSISTENCE_XML_DEFAULT Glad it is working for you though. – Chris Oct 27 '14 at 19:34
  • Is it possible to poste some code, so others can solve the problem, too? – Baalthasarr Jun 07 '15 at 17:38
  • I had the same issue when working with different plugins. One of them being a dedicated "persistence"-plugin, containing all the entities, DTOs and DB-stuff. However, the error disappeard when the eclipselink library (eclipselink.jar) and the "persistence.xml" file were put into the same plugin. Separating them leads to the mentioned "No Persistence provider found" error. – Michael Marton Aug 04 '16 at 21:17

0 Answers0