日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

EclipseLink JPA-RS简介

發(fā)布時(shí)間:2023/12/3 编程问答 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 EclipseLink JPA-RS简介 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在之前的系列文章中,我介紹了如何創(chuàng)建一個(gè)將JPA用于持久層的JAX-RS服務(wù)。 EclipseLink包含一個(gè)名為JPA-RS的組件,該組件可用于輕松自動(dòng)地將持久性單元公開為RESTful服務(wù)(支持XML和JSON消息)。 MOXy為JPA-RS提供XML和JSON綁定,并且雙向映射之類的東西會(huì)自動(dòng)為您映射。 在另一篇文章中,我將介紹如何使用MOXy定制本示例中顯示的消息。

我將使用在以下帖子中創(chuàng)建的JPA模型:

  • 第1部分–數(shù)據(jù)庫(kù)
  • 第2部分–將數(shù)據(jù)庫(kù)映射到JPA實(shí)體
    • 注解

包裝/部署

使用JPA-RS是一個(gè)簡(jiǎn)單的包裝問(wèn)題。 我們將創(chuàng)建一個(gè)WAR,其中包含JAR中的JPA模型,JPA-RS JAR和用于初始化JPA模型的簡(jiǎn)單會(huì)話bean。 對(duì)于此示例,我使用的是包含EclipseLink 2.5的GlassFish 4.0的升級(jí)版本。

  • http://dlc.sun.com.edgesuite.net/glassfish/4.0/promoted/

客戶JPARS.war

  • 網(wǎng)絡(luò)信息
  • META-INF
    • 清單文件

持久性WeaverBean

JPA-RS要求已初始化JPA實(shí)體。 我們將創(chuàng)建一個(gè)簡(jiǎn)單的會(huì)話bean來(lái)完成此任務(wù)。

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包含我們?cè)谝韵挛恼轮卸x的JPA模型:

  • 第2部分–將數(shù)據(jù)庫(kù)映射到JPA實(shí)體
    • 注解

org.eclipse.persistence.jpars_2.5.0.qualifier.jar
這是來(lái)自EclipseLink安裝的JPA-RS JAR:

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

(服務(wù)元數(shù)據(jù))

一旦部署了WAR,我們的服務(wù)就會(huì)啟動(dòng)。 我們可以執(zhí)行GET來(lái)查看我們服務(wù)的元數(shù)據(jù)。

GET(應(yīng)用程序/ xml或應(yīng)用程序/ json)

用于獲取服務(wù)元數(shù)據(jù)的URI具有以下格式:

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

以下是我們示例的URI:

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

響應(yīng)

以下是我們服務(wù)的元數(shù)據(jù)。 除了持久性單元名稱之外,我們還看到JPA模型中每個(gè)實(shí)體的元數(shù)據(jù)鏈接。 接下來(lái),我們將仔細(xì)研究Customer實(shí)體。

{"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"}}] }

(實(shí)體元數(shù)據(jù))

如果我們點(diǎn)擊其中一個(gè)實(shí)體的鏈接,那么我們將獲得以下信息:

  • 實(shí)體屬性。
  • 我們可以在實(shí)體上執(zhí)行的CRUD操作。
  • 我們可以在實(shí)體上執(zhí)行的命名查詢。

GET(應(yīng)用程序/ xml或應(yīng)用程序/ json)

用于獲取實(shí)體元數(shù)據(jù)的URI具有以下格式:

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

以下是獲取Customer實(shí)體的元數(shù)據(jù)的URI:

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

響應(yīng)

以下是客戶實(shí)體的元數(shù)據(jù)。 我們將仔細(xì)研究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"}] }

實(shí)體存在

我們將使用POST操作創(chuàng)建Customer實(shí)體的新實(shí)例。

POST(應(yīng)用程序/ xml或應(yīng)用程序/ json)

用于創(chuàng)建實(shí)體的URI具有以下格式:

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

以下是用于創(chuàng)建Customer實(shí)例的URI:

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

請(qǐng)求

以下是我們將發(fā)布到上述URI中的客戶數(shù)據(jù)的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"}] }

執(zhí)行查詢

JPA-RS會(huì)為我們?cè)贘PA模型中定義的每個(gè)命名查詢自動(dòng)創(chuàng)建URI:

GET(應(yīng)用程序/ xml或應(yīng)用程序/ json)

執(zhí)行命名查詢的URI具有以下格式:

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

下面,我們將調(diào)用名為findCustomersByCity的查詢,以查找來(lái)自Any Town的所有客戶。

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

響應(yīng)

以下是調(diào)用命名查詢的結(jié)果。 我們可以看到實(shí)體之間的關(guān)系表示為鏈接。 您在鏈接上執(zhí)行GET以獲取引用的數(shù)據(jù)。

[{"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

總結(jié)

以上是生活随笔為你收集整理的EclipseLink JPA-RS简介的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。