Java 使用 EclipseLink

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3046797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:58:15  来源:igfitidea点击:

Using EclipseLink

javajpaeclipselink

提问by Ross Peoples

I am still new to Java and Eclipse and I'm trying to get my application to connect to a database. I think I want to use EclipseLink, but all of the documentation on the matter assumes you already know everything there is to know about everything.

我还是 Java 和 Eclipse 的新手,我正在尝试让我的应用程序连接到数据库。我想我想使用 EclipseLink,但是所有关于这个问题的文档都假设您已经知道一切。

I keep getting linked back to this tutorial: http://www.vogella.de/articles/JavaPersistenceAPI/article.html

我一直链接回本教程:http: //www.vogella.de/articles/JavaPersistenceAPI/article.html

But it's basically useless because it doesn't tell you HOW to do anything. For the Installation section, it tells you to download EclipseLink and gives you a link to the download page, but doesn't tell you what to do with it after you download. The download page doesn't either. I used the "Install new software" option in Eclipse to install EclipseLink into Eclipse, but it gave me like 4 different options, none of which are explained anywhere. It gave me options JPA, MOXy, SDO, etc, but I don't know which one I need. I just installed them all. Everything on the web assumes you are already a Java guru and things that are second nature to Java devs are never explained, so it's very frustrating for someone trying to learn.

但它基本上没用,因为它没有告诉你如何做任何事情。对于安装部分,它告诉您下载 EclipseLink 并为您提供下载页面的链接,但没有告诉您下载后如何处理它。下载页面也没有。我使用 Eclipse 中的“安装新软件”选项将 EclipseLink 安装到 Eclipse 中,但它给了我 4 个不同的选项,其中没有一个在任何地方进行解释。它为我提供了 JPA、MOXy、SDO 等选项,但我不知道我需要哪一个。我刚刚安装了它们。网络上的所有内容都假设您已经是 Java 大师,而 Java 开发人员的第二天性却从未被解释过,因此对于尝试学习的人来说,这是非常令人沮丧的。

So how do I install and USE EclipseLink in my project and what do I need to do to connect it to a Microsoft SQL server? Again, I am new to all of this so I have no clue what to do. Thanks for the help.

那么如何在我的项目中安装和使用 EclipseLink,我需要做什么才能将它连接到 Microsoft SQL 服务器?同样,我对这一切都不熟悉,所以我不知道该怎么做。谢谢您的帮助。

采纳答案by Ross

I don't think you need to just learn how to use EclipseLink. EclipseLink is a reference implementation for JPA. There are a number of tutorials on the web that you can read up about regarding JPA. I have written a series of articlesfor beginners for building web applications and one of the sections deals with JPA (and EclipseLink). If you are interested take a look and I welcome discussion on it to improve it as well as my own understanding.

我认为您不需要只学习如何使用 EclipseLink。EclipseLink 是 JPA 的参考实现。网络上有许多关于 JPA 的教程,您可以阅读这些教程。我为初学者编写了一系列构建 Web 应用程序的文章,其中一个部分涉及 JPA(和 EclipseLink)。如果您有兴趣,请看一看,我欢迎讨论以改进它以及我自己的理解。

EDIT:Here's a more direct answer to your question. I hope it helps

编辑:这是对您问题的更直接的回答。我希望它有帮助

There are a number of different download approaches to take for EclipeLink. This is one way:

EclipeLink 有多种不同的下载方法。这是一种方式:

Download EclipseLink Installer

下载EclipseLink 安装程序

Extract the eclipselink.jar from the download jlib directory and include it in your projects classpath.

从下载 jlib 目录中提取 eclipselink.jar 并将其包含在您的项目类路径中。

Download the ejb3-persistence.jarand include it in your projects classpath.

下载ejb3-persistence.jar并将其包含在您的项目类路径中。

I haven't got SQL server myself but you would need to include the sqljdbc jar in your classpath. Don't know what version you are using or if it even matters but you can try this linkand download the 1033\sqljdbc_3.0.1301.101_enu.tar.gz file. Extract it and copy the sqljdbc.jar into your projects classpath.

我自己没有 SQL 服务器,但您需要在类路径中包含 sqljdbc jar。不知道您使用的是什么版本,或者它是否重要,但您可以尝试此链接并下载 1033\sqljdbc_3.0.1301.101_enu.tar.gz 文件。提取它并将 sqljdbc.jar 复制到您的项目类路径中。

Here's a simple standalone example:

这是一个简单的独立示例:

persistence.xml (saved in your META-INF directory in your src folder)

persistence.xml(保存在 src 文件夹中的 META-INF 目录中)

<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="escribs-pu" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
            <!-- haven't tested with SQL server so hope the below is correct -->
            <property name="eclipselink.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
            <property name="eclipselink.jdbc.url" value="jdbc:sqlserver://localhost;databaseName=MyDB" />
            <property name="eclipselink.jdbc.user" value="myusername" />
            <property name="eclipselink.jdbc.password" value="mypassword" />
        </properties>
    </persistence-unit>
</persistence>

Entity class:

实体类:

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="person")
public class Person {
    private Long id;
    private String name;

    @Id
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}   

SimpleTest

简单测试

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

public class SimpleTest {
    public static void main(String[] args) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("escribs-pu");

        Person person = new Person();
        person.setId(1L);
        person.setName("Clark");

        EntityManager em = null;
        EntityTransaction tx = null;
        try {
            em = emf.createEntityManager();
            tx = em.getTransaction();
            tx.begin();
            em.persist(person);
            tx.commit();

            System.out.println("Person id: " + person.getId());
        } catch (RuntimeException e) {
            tx.rollback();
            throw e;
        } finally {
            if (em != null && em.isOpen()) {
                em.close();
            }
        }
    }
}

回答by bdoughan

In addtion to being the JPA reference implementation, EclipseLinkalso offers:

除了作为 JPA 参考实现之外,EclipseLink还提供:

EclipseLink MOXy

EclipseLink MOXy

EclipseLink is a component for mapping Java objects (included JPA entities) to/from XML. It conforms to the JAXB 2.X (JSR-222) specifications with useful extensions such as: XPath based mapping, externalized metadata.

EclipseLink 是用于将 Java 对象(包括 JPA 实体)映射到 XML 或从 XML 映射的组件。它符合 JAXB 2.X (JSR-222) 规范和有用的扩展,例如:基于 XPath 的映射、外部化元数据。

EclipseLink SDO

EclipseLink SDO

SDO stands for Service Data Objects (JSR-235, EclipseLink is the reference implementation). Data is represented as a DataObject with generic get/set methods (such as set("first-name", "Jane")). The metadata is normally provided as an XML schema, and there are several helpers for acting on the data including conversion to/from XML.

SDO 代表服务数据对象(JSR-235,EclipseLink 是参考实现)。数据表示为具有通用 get/set 方法(例如 set("first-name", "Jane"))的 DataObject。元数据通常作为 XML 模式提供,并且有几个帮助程序用于处理数据,包括与 XML 的转换。

EclipseLink DBWS

EclipseLink DBWS

Is used to create a Web Service as a means of accessing your relational data.

用于创建 Web 服务作为访问关系数据的一种方式。

回答by user3263506

"So how do I install and USE EclipseLink in my project

“那么我如何在我的项目中安装和使用 EclipseLink

Since none of the previous answers really answer the question "How do I add the jpa impl. libraries to eclipse", here's the answer (took me an hour to figure it out myself!):

由于之前的答案都没有真正回答“如何将 jpa impl. 库添加到 eclipse”这个问题,这里是答案(我自己花了一个小时才弄明白!):

  • download a JPA implementation, for example eclipselink-2.5.1.v20130918-f2b9fc5.zip and extract the implementation libs. For example: eclipselink.jar jpa/*.jar to a directory

  • In eclipse open window -> preferences. In preferences window open build path -> user libraries

  • In user libraries click on new, type "name jpa" => ok

  • click on jpa and click "add external jars ... and select all extracted impl libraries

  • 下载 JPA 实现,例如 eclipselink-2.5.1.v20130918-f2b9fc5.zip 并提取实现库。例如:eclipselink.jar jpa/*.jar 到一个目录

  • 在 Eclipse 打开窗口 -> 首选项。在首选项窗口中打开构建路径 -> 用户库

  • 在用户库中点击新建,输入“name jpa”=> ok

  • 单击 jpa 并单击“添加外部 jars ... 并选择所有提取的 impl 库

After that you can complete vogella's tutorial.

之后,您可以完成 vogella 的教程。

To the admins: You might want to flag the previous comments as invalid/offtopic. Especially you don't put the jars to the project path, as you are about to create a NEWproject. However, the question is too broad, too.

致管理员:您可能希望将之前的评论标记为无效/离题。特别是您不要将 jars 放在项目路径中,因为您将要创建一个项目。然而,这个问题也太宽泛了。

回答by Kyrylo Bulat

No one mentioned a way to install EclipseLink libraries with the help of Maven. If you have a Maven project and manage your dependencies with Maven you can simply add following dependency and EclipseLink libraries are going to be able for your use in your project:

没有人提到在 Maven 的帮助下安装 EclipseLink 库的方法。如果您有一个 Maven 项目并使用 Maven 管理您的依赖项,您只需添加以下依赖项,EclipseLink 库将能够在您的项目中使用:

<!-- https://mvnrepository.com/artifact/org.eclipse.persistence/eclipselink -->
<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>eclipselink</artifactId>
    <version>2.7.6</version>
</dependency>

You can find other versions of EclipseLink here. After you add and download Maven dependency, you can set up persistence.xmlas mentioned in the top-rated answer under this question and start working with the database.

您可以在此处找到 EclipseLink 的其他版本。添加并下载 Maven 依赖项后,您可以persistence.xml按照此问题下评分最高的答案中所述进行设置并开始使用数据库。