2

我阅读了Cloud-SQL JPA 教程

当我单击“连接”时,我与云和本地 mysql 数据库的连接确实有效。但是我的EntityManagerFactory 无法读取persistence.xml。

当我想连接到本地 mysql 数据库时,我不清楚我的 persistence.xml 应该是什么样子。

在本教程中,它听起来像persistence.xml-File 是自动生成的,我只需要添加我的类。那是对的吗?如果是,我该怎么做才能触发自动生成?

否则,它会是什么样子?

4

1 回答 1

3

我让它工作。这些步骤帮助我修复它,以便我可以连接到我的本地 mysql 数据库:

你可以在下面看到我的 persistence.xml。

注释:

  • 不要实验性地将 com.mysql.Driver 设置为驱动程序,它不会起作用。

  • 设置Eclipselink的MySQL属性

希望这可以帮助某人;)

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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_2_0.xsd">
    <persistence-unit name="transactions-optional" transaction-type="RESOURCE_LOCAL">
        <class>com.MyClass</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://myapp:instance1/test"/>
            <property name="javax.persistence.jdbc.user" value="root"/>
            <property name="javax.persistence.jdbc.password" value=""/>
            <property name="eclipselink.target-database" value="MySQL"/>
            <property name="eclipselink.platform.class.name" value="org.eclipse.persistence.platform.database.MySQLPlatform" />
        </properties>
    </persistence-unit>
</persistence>

我的 EntityManagerFactory 看起来像这样:

package de.compareyourrace.system.server;

/*
 * Copyright 2010 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

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

/**
 * Factory for creating EntityManager.
 */
public final class EMF {
    private static final EntityManagerFactory emfInstance =
        Persistence.createEntityManagerFactory("transactions-optional");

    public static EntityManagerFactory get() {
        return emfInstance;
    }

    private EMF() {
      // nothing 
    }
}
于 2012-08-14T16:00:29.717 回答