简体   繁体   English

在同一应用程序中使用Google Data Store和Google App Engine

[英]Use Google Data Store and Google App Engine in same application

I have an application where i was storing all my data in Google Cloud SQL . 我有一个应用程序,我将所有数据存储在Google Cloud SQL中。 But now i think that some the tables can be moved to Google datastore . 但是现在我认为某些表可以移至Google数据存储区。 So my question is how can use both Data Store and Cloud SQL using same application . 所以我的问题是如何在同一应用程序中同时使用数据存储和Cloud SQL。 I tried checking both the option for datastore and cloud SQL but its only persisting in Cloud SQL . 我尝试同时检查数据存储和Cloud SQL的选项,但它仅在Cloud SQL中保留。

This is my persistence.xml file . 这是我的persistence.xml文件。

<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
        http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
    version="1.0">

    <persistence-unit name="transactions-optional1">
        <provider>org.datanucleus.api.jpa.PersistenceProviderImpl</provider>
        <class>com.sample.poc.Component</class>
        <properties>
            <property name="datanucleus.NontransactionalRead" value="true"/>
            <property name="datanucleus.NontransactionalWrite" value="true"/>
            <property name="datanucleus.ConnectionURL" value="appengine"/>
            <property name="datanucleus.singletonEMFForName" value="true"/>
        </properties>

    </persistence-unit>


    <persistence-unit name="transactions-optional2"
        transaction-type="RESOURCE_LOCAL">
        <provider> org.datanucleus.jpa.PersistenceProviderImpl</provider>
        <class>com.sample.poc.User</class>
        <class>com.sample.poc.Role</class>

        <properties>
          <property name="javax.persistence.jdbc.driver"
                value="com.google.appengine.api.rdbms.AppEngineDriver" />
            <property name="javax.persistence.jdbc.url"
                value="jdbc:google:rdbms://span-test-app:testinstance/pocDB" />
            <property name="javax.persistence.jdbc.user" value="" />
            <property name="javax.persistence.jdbc.password" value="" />
        </properties>

    </persistence-unit>

</persistence>

Here is my two EMF beans: 这是我的两个EMF bean:

For Datastore: 对于数据存储区:

package com.sample.poc;


import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public final class EMFDS {
    private static final EntityManagerFactory emfInstance = Persistence
            .createEntityManagerFactory("transactions-optional1");

    private EMFDS() {
    }

    public static EntityManagerFactory get() {
        return emfInstance;
    }
}

Here is EMF bean for cloud sql: 这是用于云sql的EMF bean:

package com.sample.poc;


import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public final class EMFRDBMS {
    private static final EntityManagerFactory emfInstance = Persistence
            .createEntityManagerFactory("transactions-optional");

    private EMFRDBMS() {
    }

    public static EntityManagerFactory get() {
        return emfInstance;
    }
}

Here's the servlet for inserting component in datastore: 这是用于在数据存储区中插入组件的servlet:

package com.sample.poc;

import java.io.IOException;

import javax.persistence.EntityManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ComponentInsert extends HttpServlet {

    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String CompId = request.getParameter("CompId");  
    String CompName= request.getParameter("CompName");  
    response.setContentType("text/html");  
    EntityManager e = EMFDS.get().createEntityManager();
    try {
        e.getTransaction().begin();
        Component ob = new Component();
        ob.setCompId(Integer.parseInt(CompId));
        ob.setCompName(CompName);
        e.persist(ob);
        e.getTransaction().commit();
    } catch (Exception ex) {
        e.getTransaction().rollback();
        ex.printStackTrace();
    }
}

}

I just want user,role bean to be persisted in cloud sql and component in datastore. 我只希望角色豆保留在云sql和数据存储区中的组件中。

When i try to insert a component in datastore its throws this exception: 当我尝试在数据存储区中插入组件时,将引发此异常:

Caused by: javax.persistence.PersistenceException: No persistence providers available for "transactions-optional1" after trying the following discovered implementations: org.datanucleus.api.jpa.PersistenceProviderImpl
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:180)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:70)
    at com.sample.poc.EMFDS.<clinit>(EMFDS.java:9)
    ... 40 more
12 Aug, 2013 9:48:24 AM com.google.apphosting.utils.jetty.JettyLogger warn
WARNING: Nested in java.lang.ExceptionInInitializerError:
javax.persistence.PersistenceException: No persistence providers available for "transactions-optional1" after trying the following discovered implementations: org.datanucleus.api.jpa.PersistenceProviderImpl
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:180)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:70)

Am I missing some thing . 我错过了什么吗? Please suggest . 请提出建议。

You have no "provider" specified for one persistence-unit - which JPA provider are you going to use for cloud-sql? 您没有为一个持久性单元指定“提供者”-您要为cloud-sql使用哪个JPA提供者? So specify it. 因此,请指定它。

You have an invalid "provider" for the other persistence-unit. 您有另一个持久性单元无效的“提供者”。 GAE JPA v2 is "org.datanucleus.api.jpa.PersistenceProviderImpl" as the message says, yet you seem to want to use some (very old, unsupported) GAE JPA 1 variant. 消息说,GAE JPA v2是“ org.datanucleus.api.jpa.PersistenceProviderImpl”,但您似乎想使用某些(非常老旧,不受支持的)GAE JPA 1变体。 Make sure you have the right one in the CLASSPATH (and not the other one) 确保您在CLASSPATH中拥有正确的一个(而不是另一个)

Which "JPA API" jar is in the CLASSPATH? 哪个“ JPA API”罐子在CLASSPATH中? (this is NOT the JPA implementation). (这不是JPA实现)。 If you are using GAE JPA v2 then you need JPA API v2 (geronimo-jpa_2.0_spec) and NOT JPA API v1 (geronimo-jpa_3.0_spec). 如果您使用的是GAE JPA v2,则需要JPA API v2(geronimo-jpa_2.0_spec)和NOT JPA API v1(geronimo-jpa_3.0_spec)。 If one of the JPA providers being used is JPA2 then the other has to be too. 如果使用的一个JPA提供程序是JPA2,那么另一个也必须使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM