Question

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.

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

UPDATE: The documentation at

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

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.

Was it helpful?

Solution

Have you looked at 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"
  }
}

OTHER TIPS

Apprently in the newer versions of Spring Data Rest, this should do it:

POST /tickets
Content-Type: application/json

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

Going by Oliver Gierke's comment at Spring Data Rest 2.0.0.RELEASE Breaks Code Working Previously With RC1

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top