Spring Data REST and IdClass - not compatible?
Asked Answered
B

1

6

I am trying to use Spring Data REST with a given SQL schema, which uses the JPA @IdClass annotation for associative tables (intersection table, or many-to-many resolution table). These mapping entities do not get serialized properly.

I created a small project, that illustrates the problem. It's a fork of spring-data-examples, and pretty straightforward. It is using eclipselink, but I already tested it with Hibernate and the problem is the same.

https://github.com/otrosien/spring-data-examples/tree/idClassFailureWithSerializable

The setup: 2 entities: Customer and Relationship, 2 repositories: CustomerRepository, RelationshipRepository, both extend CrudRepository

Customer has a generated Id, and firstname, lastname as String. Relationship has the IdClass "RelationshipID" and customer1, customer2 as composite primary key, both having foreign key on Customer. Plus a relation string.

A basic integration test shows the entities work as expected.

Customer dave = customers.save(new Customer("Dave", "Matthews"));
Customer jack = customers.save(new Customer("Jack", "Johnson"));

assertThat(customers.findOne(dave.getId()), is(dave));
assertThat(customers.findOne(jack.getId()), is(jack));

Relationship rel = relationships.save(new Relationship(dave, jack, "likes"));
assertThat(relationships.findOne(rel.pk()), is(rel));

So far so good. Let's try this via REST API now.

POST http://localhost:8080/customers
Content-Type: application/json

{
  "lastname" :"Dave",
  "firstname":"Matthews"
}

POST http://localhost:8080/customers
Content-Type: application/json

{
  "lastname" :"Jack",
  "firstname":"Johnson"
}

POST http://localhost:8080/relationships
Content-Type: application/json

{
  "customer1" : "http://localhost:8080/customers/1",
  "customer2" : "http://localhost:8080/customers/2",
  "relation" : "likes"
}

I always get a 201 Created, which is good. But the representation of the mapping entity looks broken. Instead of proper links they seem to be serialized objects.

GET /relationships

200 OK
{
  "_embedded" : {
    "relationships" : [ {
      "relation" : "likes",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/relationships/Customer%20%5Bid=2,%20firstname=F,%20lastname=L%5D"
        },
        "customer1" : {
          "href" : "http://localhost:8080/relationships/Customer%20%5Bid=2,%20firstname=F,%20lastname=L%5D/customer1"
        },
        "customer2" : {
          "href" : "http://localhost:8080/relationships/Customer%20%5Bid=2,%20firstname=F,%20lastname=L%5D/customer2"
        }
      }
    } ]
  }
}

Question: Has anyone succeeded in using Spring Data REST with mapping entities? Can you spot a mistake in the implementation? Or is it a bug? (I'm using spring boot 1.2.4.RELEASE with starter-data-rest and starter-data-jpa, which should all be the latest releases)

Please, no suggestions to change the schema. I already know I can fix it by inserting a generated @Id into Relationship, but the schema is given as-is.

Brendon answered 25/6, 2015 at 12:45 Comment(0)
A
0

I solve the problem by custom BackendIdConverter.

spring data rest with composite primary key

Hope this will help you.

Anodic answered 25/7, 2017 at 3:7 Comment(1)
With a bit more rep, you will be able to flag duplicate questions like this. Until then, posting links as answers is not really ideal, and they will generally be deleted. Or, if the question is not a duplicate, tailor the answer to this specific question.Buran

© 2022 - 2024 — McMap. All rights reserved.