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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

EclipseLink JPA-RS简介

發布時間:2023/12/3 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 EclipseLink JPA-RS简介 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

我將使用在以下帖子中創建的JPA模型:

  • 第1部分–數據庫
  • 第2部分–將數據庫映射到JPA實體
    • 注解

包裝/部署

使用JPA-RS是一個簡單的包裝問題。 我們將創建一個WAR,其中包含JAR中的JPA模型,JPA-RS JAR和用于初始化JPA模型的簡單會話bean。 對于此示例,我使用的是包含EclipseLink 2.5的GlassFish 4.0的升級版本。

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

客戶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 JPA-RS简介的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。