sql - JPA + 云 SQL : How to design the persistence. xml

标签 sql xml google-cloud-sql

我读了Cloud-SQL JPA Tutorial .

当我单击“连接”时,我与云和本地 mysql 数据库的连接就会正常工作。 但我的 EntityManagerFactory 无法读取 persistence.xml。

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

在教程中,听起来 persistence.xml 文件是自动生成的,我只需添加我的类。那是对的吗?如果是,我需要做什么才能触发自动生成?

不然会是什么样子?

最佳答案

我让它工作了。这些步骤帮助我修复了它,以便我可以连接到本地 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 
    }
}

关于sql - JPA + 云 SQL : How to design the persistence. xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11953079/

相关文章:

php - 使用 PHP MySQLi 从 Google Cloud App Engine 连接到 Cloud SQL 不起作用

相同限制的 MySQL SELECT 查询越来越慢

sql - PostgreSQL 查询约束中的允许值列表?

mysql - 如果左侧存在某些内容,则返回左侧表和右侧表中的所有结果

java - 使用 JAXB 将 XML 解析为 java 变量(bean)

postgresql - 谷歌云 PostgreSQL : Utilization remains at 100%

php - 在 Google App Engine 中连接到 Google Cloud SQL

php - 在 INNER JOIN MySQL 中合并冗余行

xml - 如何使用 Powershell 枚举 XML 元素的属性

python - 关闭所有打开的 xml 标签