GAE - 使用Google Cloud SQL的Google端点

时间:2015-01-05 19:21:58

标签: hibernate google-app-engine google-cloud-endpoints google-cloud-sql

我尝试将de Endpoints与Cloud SQL和Hibernate集成。 但是,每次都有错误。 按照我的测试:

1 - 源文件夹中的persistence.xml:      http://imgur.com/hKjf8Cs   给我错误:      http://imgur.com/QJe8rvq

2 - resources文件夹中的persistence.xml给出了错误:

  

javax.persistence.PersistenceException:没有名为gegAdminPU的EntityManager的持久性提供程序       在javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69)       在br.com.gegsystems.gegadmin.utils.HibernateUtils。(HibernateUtils.java:47)       at java.lang.Class.forName0(Native Method)       at java.lang.Class.forName(Class.java:188)       在com.google.appengine.tools.development.agent.runtime.RuntimeHelper.checkRestricted(RuntimeHelper.java:70)       在com.google.appengine.tools.development.agent.runtime.Runtime.checkRestricted(Runtime.java:64)       在br.com.gegsystems.gegadmin.endpoints.ClienteEndpoint。(ClienteEndpoint.java:20)       at java.lang.Class.forName0(Native Method)       at java.lang.Class.forName(Class.java:266)       在com.google.api.server.spi.ServletInitializationParameters.getClassForName(ServletInitializationParameters.java:82)       在com.google.api.server.spi.ServletInitializationParameters.fromServletConfig(ServletInitializationParameters.java:51)       在com.google.api.server.spi.SystemServiceServlet.init(SystemServiceServlet.java:102)       在org.mortbay.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:440)       在org.mortbay.jetty.servlet.ServletHolder.getServlet(ServletHolder.java:339)       在org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)       在org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1166)       在com.google.appengine.api.socket.dev.DevSocketFilter.doFilter(DevSocketFilter.java:74)       在org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157)       在com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:127)       在org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157)       在com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34)       在org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157)       在com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:63)       在org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157)       在com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)       在org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157)       在com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:125)       在org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157)       在com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectRequest(DevAppServerModulesFilter.java:366)       在com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectModuleRequest(DevAppServerModulesFilter.java:349)       在com.google.appengine.tools.development.DevAppServerModulesFilter.doFilter(DevAppServerModulesFilter.java:116)       在org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157)       在org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)       在org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)       在org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)       在org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)       在org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)       在com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:98)       在org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)       在com.google.appengine.tools.development.JettyContainerService $ ApiProxyHandler.handle(JettyContainerService.java:491)       在org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)       在org.mortbay.jetty.Server.handle(Server.java:326)       在org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)       在org.mortbay.jetty.HttpConnection $ RequestHandler.content(HttpConnection.java:938)       在org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:755)       在org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)       在org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)       在org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)       在org.mortbay.thread.QueuedThreadPool $ PoolThread.run(QueuedThreadPool.java:582)

遵循端点代码:

@Api(name="clientes", version="v1")
public class ClienteEndpoint {

    private static EntityManagerFactory entityManagerFactory;

    static {
        entityManagerFactory = HibernateUtils.getEntityManagerFactory();
    }

    @ApiMethod(name="listar", path="clientes")
    public List<String> listar(){
        List<String> resultado = new ArrayList<>();
        EntityManager em = entityManagerFactory.createEntityManager();

        em.getTransaction().begin();

        Query query = em.createQuery("SELECT 'teste' FROM dual;");
        resultado.add((String) query.getSingleResult());

        return resultado;
    }
}

HibernateUtils.java:

import com.google.appengine.api.rdbms.AppEngineDriver;
import com.google.appengine.api.utils.SystemProperty;

public class HibernateUtils {

    private static final EntityManagerFactory entityManagerFactory;

    public static EntityManagerFactory getEntityManagerFactory() {
        return entityManagerFactory;
    }

    static {
        try {
            DriverManager.registerDriver(new AppEngineDriver());
        } catch (SQLException e) {
            throw new ExceptionInInitializerError(e);
        }

        Map<String, String> properties = new HashMap<>();
        if (SystemProperty.environment.value() ==
                SystemProperty.Environment.Value.Production) {
            properties.put("javax.persistence.jdbc.driver",
                    "com.mysql.jdbc.GoogleDriver");
            properties.put("javax.persistence.jdbc.url", System.getProperty(
                    "jdbc:google:mysql://**********:*******/gegadmin?user=root")
            );
            properties.put("javax.persistence.jdbc.password",
                    System.getProperty("**********"));
        } else {
            properties.put("hibernate.connection.driver_class",
                    "com.mysql.jdbc.Driver");
            properties.put("hibernate.connection.url", System.getProperty(
                    "jdbc:mysql://localhost:3306/gegadmin?user=root")
            );
            properties.put("hibernate.connection.password",
                    System.getProperty("**********"));
        }

        entityManagerFactory = Persistence
                .createEntityManagerFactory("gegAdminPU", properties);
    }
}

1 个答案:

答案 0 :(得分:0)

我已经将持久性框架从Hibernate更改为EclipseLink并且正常工作。 我认为这是我的Eclipse配置Hibernate和JPA的一个问题。