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

查看:38
本文介绍了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.

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

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.

推荐答案

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

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

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天全站免登陆