Spring Data REST URI与实体ID [英] Spring Data REST URI vs. entity ID

查看:113
本文介绍了Spring Data REST URI与实体ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring Data REST(尤其是Spring HATEOAS)将RESTful ID(即URI)与实体ID分离,在保存新对象时,我很难将它们重新链接起来.在 https://github.com/SpringSource/spring上查看有关这种解耦的有趣讨论. -data-rest/issues/13 .

Spring Data REST (and Spring HATEOAS in particular) decouples RESTful IDs (viz., URIs) from entity IDs, and I'm having trouble linking them back up when saving new objects. See the interesting discussion around this decoupling at https://github.com/SpringSource/spring-data-rest/issues/13.

假设客户端应用希望创建具有关联的TicketCategory资源的新Ticket资源.我想针对远程Spring Data REST端点发布Ticket. Ticket尚无ID,因为它是新的. TicketCategory有一个ID,但根据上面的讨论,在客户端上是一个URI.因此,当我保存Ticket时,Spring Data REST将Ticket传递给Spring Data JPA,这不喜欢它:Spring Data JPA认为没有实体ID的TicketCategory是瞬态的:

Suppose that a client app wants to create a new Ticket resource with an associated TicketCategory resource. I want to post the Ticket against a remote Spring Data REST endpoint. The Ticket doesn't yet have an ID since it's new. The TicketCategory has an ID, but on the client it's a URI per the discussion above. So when I save the Ticket, Spring Data REST passes the Ticket to Spring Data JPA, which doesn't like it: Spring Data JPA thinks that the TicketCategory—having no entity ID—is transient:

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在实体之间建立关系的方案.我不知道这是否是当前可用的唯一方法,但是这种方法似乎需要在初始帖子中保留关联为空,然后在后续帖子中对其进行更新.在上述情况下,这是不希望的,因为票证的类别字段(@NotNull)是必需的.

has a section called "Updating relationships" that describes a scheme using HTTP POST to establish relationships between entities. I don't know if that's the only approach currently available, but it seems that this approach would require leaving the association null on the initial post and then updating it with a subsequent post. In the case above that would be undesirable since the category field is required (@NotNull) for tickets.

推荐答案

您是否看过简单地说,如果导出器找到链接对象而不是关系或托管对象(另一个具有导出的存储库的实体),它将取消引用链接对象.

Simply put, the exporter will de-reference Link objects if it finds them in place of a relationship or managed object (another entity that has an exported Repository).

假设链接的属性称为类别",则可以创建一个新的票证,例如:

Assuming your linked property is called "category", then you could create a new Ticket like:

POST /tickets
Content-Type: application/json

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

这篇关于Spring Data REST URI与实体ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆