eclipselink_EclipseLink JPA-RS简介

eclipselink

在以前的系列文章中,我介绍了如何创建一个将JPA用于持久层的JAX-RS服务。 EclipseLink包含一个名为JPA-RS的组件,该组件可用于轻松自动地将持久性单元公开为RESTful服务(支持XML和JSON消息)。 MOXy为JPA-RS提供XML和JSON绑定,并且双向映射之类的东西会自动为您映射。 在另一篇文章中,我将介绍如何使用MOXy定制本示例中显示的消息。

我将使用在以下帖子中创建的JPA模型:

包装/部署

使用JPA-RS是一个简单的包装问题。 我们将创建一个WAR,其中包含JAR中的JPA模型,JPA-RS JAR和用于初始化JPA模型的简单会话bean。 对于此示例,我使用的是包含EclipseLink 2.5的GlassFish 4.0的升级版本。

客户JPARS.war
  • 网络信息
  • META-INF
    • 清单文件
持久性WeaverBean

JPA-RS要求已初始化JPA实体。 我们将创建一个简单的会话bean来完成此任务。

package org.example.ejb;

import javax.ejb.*;
import javax.persistence.*;

@Startup
@Singleton
@LocalBean
public class PersistenceWeaverBean {

    @SuppressWarnings("unused")
    @PersistenceUnit(unitName = "CustomerService")
    private EntityManagerFactory emf;

}
客户JPA.jar

该JAR包含我们在以下文章中定义的JPA模型:

  • 第2部分–将数据库映射到JPA实体

org.eclipse.persistence.jpars_2.5.0.qualifier.jar
这是来自EclipseLink安装的JPA-RS JAR:

<ECLIPSELINK_HOME>/jlib/jpa/org.eclipse.persistence.jpars_2.5.0.qualifier.jar

(服务元数据)

一旦部署了WAR,我们的服务就会启动。 我们可以执行GET来查看我们服务的元数据。

GET(应用程序/ xml或应用程序/ json)

用于获取服务元数据的URI具有以下格式:

http://{Server}/{Application}/persistence/v1.0/{PersistenceUnit}/metadata

以下是我们示例的URI:

http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/metadata
响应

以下是我们服务的元数据。 除了持久性单元名称之外,我们还看到JPA模型中每个实体的元数据链接。 接下来,我们将仔细研究Customer实体。

{
    "persistenceUnitName": "CustomerService",
    "types": [
        {
            "_link": {
                "href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/metadata/entity/Address",
                "method": "application/json",
                "rel": "Address"
            }
        },
        {
            "_link": {
                "href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/metadata/entity/PhoneNumber",
                "method": "application/json",
                "rel": "PhoneNumber"
            }
        },
        {
            "_link": {
                "href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/metadata/entity/Customer",
                "method": "application/json",
                "rel": "Customer"
            }
        }
    ]
}

(实体元数据)

如果我们点击其中一个实体的链接,那么我们将获得以下信息:

  • 实体属性。
  • 我们可以在实体上执行的CRUD操作。
  • 我们可以在实体上执行的命名查询。
GET(应用程序/ xml或应用程序/ json)

用于获取实体元数据的URI具有以下格式:

http://{Server}/{Application}/persistence/v1.0/{PersistenceUnit/metadata/entity/{Entity}

以下是获取Customer实体的元数据的URI:

http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/metadata/entity/Customer
响应

以下是客户实体的元数据。 我们将仔细研究POST操作(第37-39行)和命名查询(第49-58行)。

{
    "name": "Customer",
    "attributes": [
        {
            "name": "id",
            "type": "long"
        },
        {
            "name": "firstName",
            "type": "String"
        },
        {
            "name": "lastName",
            "type": "String"
        },
        {
            "name": "address",
            "type": "Address"
        },
        {
            "name": "phoneNumbers",
            "type": "Set<PhoneNumber>"
        }
    ],
    "linkTemplates": [
        {
            "method": "get",
            "href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/entity/Customer/{primaryKey}",
            "rel": "find"
        },
        {
            "method": "put",
            "href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/entity/Customer",
            "rel": "persist"
        },
        {
            "method": "post",
            "href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/entity/Customer",
            "rel": "update"
        },
        {
            "method": "delete",
            "href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/entity/Customer/{primaryKey}",
            "rel": "delete"
        }
    ],
    "queries": [
        {
            "queryName": "findCustomersByCity",
            "returnTypes": [
                "Customer"
            ],
            "linkTemplate": {
                "method": "get",
                "href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/query/findCustomersByCity;city={city}",
                "rel": "execute"
            },
            "jpql": "SELECT c FROM Customer c WHERE c.address.city = :city"
        }
    ]
}

持久化实体

我们将使用POST操作创建Customer实体的新实例。

POST(应用程序/ xml或应用程序/ json)

用于创建实体的URI具有以下格式:

http://{Server}/{Application}/persistence/v1.0/{PersistenceUnit}/entity/{Entity}

以下是用于创建Customer实例的URI:

http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/entity/Customer
请求

以下是我们将发布到上述URI中的客户数据的JSON表示形式:

{
    "id" : 1,
    "firstName" : "Jane",
    "lastName" : "Doe",
    "address" : {
        "id" : 1,
        "street" : "1 A Street",
        "city" : "Any Town"
    },
    "phoneNumbers" : [{
        "id" : 2,
        "type" : "work",
        "num" : "555-1111"
    }, {
        "id" : 3,
        "type" : "home",
        "num" : "555-2222"
    }]
}

执行查询

JPA-RS会为我们在JPA模型中定义的每个命名查询自动创建URI:

GET(应用程序/ xml或应用程序/ json)

执行命名查询的URI具有以下格式:

http://{Server}/{Application}/persistence/v1.0/{PersistenceUnit}/query/{NamedQuery;Parameters}

下面,我们将调用名为findCustomersByCity的查询,以查找来自Any Town的所有客户。

http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/query/findCustomersByCity;city=Any%20Town
响应

以下是调用命名查询的结果。 我们可以看到实体之间的关系表示为链接。 您在链接上执行GET以获取引用的数据。

[
    {
        "firstName": "Jane",
        "id": 1,
        "lastName": "Doe",
        "_relationships": [
            {
                "_link": {
                    "href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/entity/Customer/1/address",
                    "rel": "address"
                }
            },
            {
                "_link": {
                    "href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/entity/Customer/1/phoneNumbers",
                    "rel": "phoneNumbers"
                }
            }
        ],
        "address": {
            "_link": {
                "href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/entity/Address/1",
                "method": "GET",
                "rel": "self"
            }
        },
        "phoneNumbers": [
            {
                "_link": {
                    "href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/entity/PhoneNumber/3",
                    "method": "GET",
                    "rel": "self"
                }
            },
            {
                "_link": {
                    "href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/entity/PhoneNumber/2",
                    "method": "GET",
                    "rel": "self"
                }
            }
        ]
    }
]

参考:Java XML和JSON绑定博客上,我们的JCG合作伙伴 Blaise Doughan 介绍了EclipseLink JPA-RS

翻译自: https://www.javacodegeeks.com/2013/04/introducing-eclipselink-jpa-rs.html

eclipselink

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值