gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 05:35:48 25 4
gpt4 key购买 nike

我读了 Cloud-SQL JPA Tutorial .

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

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

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

否则,它会是什么样子?

最佳答案

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

  • Project -> Properties -> JPA我必须将连接设置为 MyApp.MySQL.DevInstance
  • Project -> Properties -> Google -> App Engine您需要激活使用“MySQL 实例”
  • 将 mysql .jar 放到 appengine-java-sdk-x.x.x/lib/impl.(参见 Java Google App Engine and Google Cloud SQL running on local dev server)

  • 你可以在下面看到我的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/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com