Reputation: 2612
I'm using Android Studio and I've created an app engine backend with the tools provided by Android Studio. I'm trying to use JPA/Persistence in my backend module for my android application. When I try my API to store some data I get the Exception
java.lang.ExceptionInInitializerError
at be.stece.esocomm.backendApp.EsoCommEndpoint.getEntityManager(EsoCommEndpoint.java:73)
....
Caused by: javax.persistence.PersistenceException: No Persistence provider for EntityManager named esoCommBackendPersistence
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
So the problem lies somewhere with my setup or project structure. Since it can't find my persistence provider. But I can't figure out where the problem lies.
First of all in my build.gradle file of this backend module I have the dependecies:
compile 'javax.servlet:servlet-api:2.5'
compile group: 'org.hibernate.javax.persistence', name: 'hibernate-jpa-2.0-api', version: '1.0.0.Final'
compile 'org.hibernate:hibernate-entitymanager:4.1.7.Final'
Then in MyModule/src/META_INF/persistence.xml I have declared everything:
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="esoCommBackendPersistence">
<provider>org.datanucleus.api.jpa.PersistenceProviderImpl</provider>
<properties>
<property name="datanucleus.NontransactionalRead" value="true" />
<property name="datanucleus.NontransactionalWrite" value="true" />
<property name="datanucleus.ConnectionURL" value="appengine" />
<property name="datanucleus.appengine.datastoreEnableXGTransactions"
value="true" />
<property name="datanucleus.appengine.autoCreateDatastoreTxns"
value="true" />
</properties>
<class>be.stece.esocomm.backendApp.NewsBean</class>
</persistence-unit>
</persistence>
I'm not 100% sure if the persistence.xml is in the right directory. I've tried several options, like moving the META_INF folder to the /src/main folder instead of just /src. But no difference.
To create my EntityManager I use the correct Persistence name
public class MyEntityManagerFactory {
private static final String PERSISTENCE_NAME = "esoCommBackendPersistence";
private static final EntityManagerFactory emfInstance =
Persistence.createEntityManagerFactory(PERSISTENCE_NAME);
private MyEntityManagerFactory() {}
public static EntityManagerFactory get() {
return emfInstance;
}
}
private static EntityManager getEntityManager() {
return MyEntityManagerFactory.get().createEntityManager(); => The error is triggered on this line
}
Upvotes: 1
Views: 916
Reputation: 2612
Ok, so I got it to work.
I removed all hibernate and use the appengine persistence as per suggestion of Neil Stockton.
dependencies {
appengineSdk 'com.google.appengine:appengine-java-sdk:1.8.9'
compile 'com.google.appengine:appengine-endpoints:1.8.9'
compile 'com.google.appengine:appengine-endpoints-deps:1.8.9'
compile 'javax.servlet:servlet-api:2.5'
// Persistence
compile 'org.ow2.asm:asm:4.0'
compile 'org.datanucleus:datanucleus-api-jpa:3.1.3'
compile 'org.datanucleus:datanucleus-api-jdo:3.1.3'
compile 'com.google.appengine.orm:datanucleus-appengine:2.1.2'
compile 'org.datanucleus:datanucleus-core:3.1.3'
compile 'org.apache.geronimo.specs:geronimo-jpa_2.0_spec:1.0'
compile 'javax.jdo:jdo-api:3.0.1'
compile 'javax.transaction:jta:1.1'
}
Also now I followed the following guides to create the endpoints: https://developers.google.com/appengine/docs/java/datastore/entities#Java_Kinds_and_identifiers
Upvotes: 1