我正在尝试使用Spring Data REST与给定的SQL模式,该模式使用JPA @IdClass注释来处理关联表(交叉表或多对多解析表)。但这些映射实体无法正确序列化。
我创建了一个小项目来说明问题。它是spring-data-examples的一个分支,非常简单。它使用eclipselink,但我已经测试过Hibernate,问题是一样的。 https://github.com/otrosien/spring-data-examples/tree/idClassFailureWithSerializable 设置如下: 2个实体:客户和关系,2个存储库:CustomerRepository、RelationshipRepository,都扩展了CrudRepository
客户有一个生成的Id,以及名字和姓氏作为字符串。 关系具有“RelationshipID”作为IdClass,customer1和customer2作为复合主键,两者都在客户上具有外键。此外还有一个关系字符串。
基本的集成测试显示实体按预期工作。
问题:有人成功地使用Spring Data REST映射实体吗?您能否在实现中发现错误?还是一个错误?(我正在使用spring boot 1.2.4.RELEASE与starter-data-rest和starter-data-jpa,应该都是最新版本)
请不要建议更改模式。我已经知道可以通过在关系中插入生成的@Id来修复它,但模式是按原样给出的。
我创建了一个小项目来说明问题。它是spring-data-examples的一个分支,非常简单。它使用eclipselink,但我已经测试过Hibernate,问题是一样的。 https://github.com/otrosien/spring-data-examples/tree/idClassFailureWithSerializable 设置如下: 2个实体:客户和关系,2个存储库:CustomerRepository、RelationshipRepository,都扩展了CrudRepository
客户有一个生成的Id,以及名字和姓氏作为字符串。 关系具有“RelationshipID”作为IdClass,customer1和customer2作为复合主键,两者都在客户上具有外键。此外还有一个关系字符串。
基本的集成测试显示实体按预期工作。
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与starter-data-rest和starter-data-jpa,应该都是最新版本)
请不要建议更改模式。我已经知道可以通过在关系中插入生成的@Id来修复它,但模式是按原样给出的。