Spring Data REST URI与实体ID

栏目: Java · 发布时间: 7年前

内容简介:翻译自:https://stackoverflow.com/questions/12879975/spring-data-rest-uri-vs-entity-id
Spring Data REST(特别是Spring HATEOAS)将RESTful ID(即URI)与实体ID分离,在保存新对象时我无法将它们连接起来.在 https://github.com/SpringSource/spring-data-rest/issues/13

看到有关这种脱钩的有趣讨论.

假设客户端应用程序想要使用关联的TicketCategory资源创建新的Ticket资源.我想针对远程Spring Data REST端点发布Ticket.由于它是新的,故障单还没有ID. TicketCategory有一个ID,但在客户端上,它是上面讨论的URI.因此,当我保存Ticket时,Spring Data REST将Ticket传递给Spring Data JPA,它不喜欢它:Spring Data JPA认为TicketCategory没有实体ID – 是瞬态的:

org.hibernate.TransientPropertyValueException:
    Not-null property references a transient value -
    transient instance must be saved before current operation:
    com.springinpractice.ch13.helpdesk.model.Ticket.category ->
    com.springinpractice.ch13.helpdesk.model.TicketCategory

更新:文档在

https://github.com/SpringSource/spring-data-rest/wiki/JPA-Repository-REST-Exporter

有一个名为“更新关系”的部分,描述了使用HTTP POST建立实体之间关系的方案.我不知道这是否是目前可用的唯一方法,但似乎这种方法需要在初始帖子上保留关联null,然后用后续帖子更新它.在上面的情况下,这是不合需要的,因为票据需要类别字段(@NotNull).

你看过 https://github.com/SpringSource/spring-data-rest/wiki/Embedded-Entity-references-in-complex-object-graphs

吗?

简单地说,如果导出器找到它们代替关系或托管对象(具有导出的存储库的另一个实体),则导出器将取消引用它们.

假设您的链接属性被称为“类别”,那么您可以创建一个新的Ticket,如:

POST /tickets
Content-Type: application/json

{
  "description": "Description of the ticket or issue",
  "category": {
    "rel": "category.Category",
    "href": "http://localhost:8080/categories/1"
  }
}

翻译自:https://stackoverflow.com/questions/12879975/spring-data-rest-uri-vs-entity-id


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

JavaScript & jQuery

JavaScript & jQuery

David Sawyer McFarland / O Reilly / 2011-10-28 / USD 39.99

You don't need programming experience to add interactive and visual effects to your web pages with JavaScript. This Missing Manual shows you how the jQuery library makes JavaScript programming fun, ea......一起来看看 《JavaScript & jQuery》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具