Spring 数据 REST 和 IdClass - 不兼容?

Spring Data REST and IdClass - not compatible?

我正在尝试将 Spring 数据 REST 与给定的 SQL 模式一起使用,该模式使用 JPA @IdClass 注释进行关联 tables(交叉点 table,或多对多解析 table)。这些映射实体未正确序列化。

我创建了一个小项目来说明问题。它是 spring-data-examples 的分支,非常简单。它使用的是eclipselink,但我已经用Hibernate测试过它,问题是一样的。

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

设置: 2 个实体:Customer 和 Relationship,2 个存储库:CustomerRepository、RelationshipRepository,均扩展 CrudRepository

客户有一个生成的 ID,名字,姓氏作为字符串。 关系有 IdClass "RelationshipID" 和 customer1、customer2 作为复合主键,两者都在 Customer 上有外键。加上一个关系字符串。

基本集成测试显示实体按预期工作。

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));

到目前为止一切顺利。让我们现在通过 REST API 试试这个。

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"
}

我总是得到 201 Created,这很好。但是映射实体的表示看起来很破损。它们似乎不是正确的链接,而是序列化的对象。

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"
        }
      }
    } ]
  }
}

问题:有没有人成功地将 Spring Data REST 与映射实体一起使用?你能发现实施中的错误吗?或者这是一个错误? (我正在使用 spring boot 1.2.4.RELEASE with starter-data-rest 和 starter-data-jpa,它们应该都是最新版本)

拜托,没有更改架构的建议。我已经知道我可以通过将生成的 @Id 插入到关系中来修复它,但是模式是按原样给出的。

我通过自定义BackendIdConverter解决了这个问题。

希望对您有所帮助。