Search code examples
javajakarta-eejpaeclipselinkderby

Is it possible to use EclipseLink (with persistence.xml) with Embedded Derby


So here is what i am trying to achieve:
A Java App with JPA implementation usinf Embedded Derby. Once this is working , i am then planning to have Spark (with Jetty Server) and Angular js.

These are the tools/frameworks i am using
Eclipse IDE (Luna)
Maven Build system
JPA Implementation (EclipseLink)
Embedded Derby for database
Spark with Embedded Jetty server.
Angular JS

I have the persistence.xml defined with the Derby Embedded Driver and the db properties. In the Main class i am using the EntityManger to get the EM. I also create new Instance of the EmbeddedDerby to start the Engine.

But when i run the App, it says "No Persistence provider Persistence for Entity Manager"named testSparkJettyDerby"

Here's my persistence.xml

<?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="testSparkJettyDerby" transaction-type="RESOURCE_LOCAL">
    <class>model.MyUser</class>
    <properties>
        <property name="javax.persistence.jdbc.url" value="jdbc:derby:sample;create=true"/>
        <property name="javax.persistence.jdbc.user" value="user"/>
        <property name="javax.persistence.jdbc.password" value="xxxx"/>
        <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
    </properties>       
</persistence-unit>

Code snippet in my Main Class

    createConnection();

    // TODO Auto-generated method stub
    factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
    EntityManager em = factory.createEntityManager();


private static void createConnection()
{
    try
    {
        Class.forName("org.eclipse.persistence.jpa.PersistenceProvider");
        Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
        //Get a connection
        conn = DriverManager.getConnection(dbURL);
        if(conn != null) {
          System.out.println("Connected...");
        }
    }
    catch (Exception except)
    {
        except.printStackTrace();
    }
}

My derby jar files are in the classpath.

I am not sure if this is possible, but i could not find any tutorial or answers who have done something like this.

Any help is appreciated.

Thanks in advance..


Solution

  • Make sure that your persistence.xml file is in classes(or whatever your build dir is)/META-INF.