【发布时间】:2017-06-20 01:30:16
【问题描述】:
具有以下实体和存储库。我无法设法将 id 放在我的关系上。提前感谢您的帮助
我的 build.gradle 中的相关工件(使用 Spring Boot 版本 1.5.4.RELEASE)
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-data-rest')
实体
商店
@Entity
@Table(name = "store")
class Store {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
long id
String description
@ManyToOne(optional=false, fetch = FetchType.EAGER)
Province province
}
省
@Entity
@Table(name = "province")
class Province {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
long id
@NotNull
String name
}
存储库
商店
@RepositoryRestResource(collectionResourceRel = "stores", path = "stores")
interface StoreRepository extends PagingAndSortingRepository<Store, Long> {
}
省
@RepositoryRestResource(collectionResourceRel = "provinces", path = "provinces")
interface ProvinceRepository extends PagingAndSortingRepository<Province, Long> {
}
预期结果 我期待这个结果,请注意省的链接
{
"stores": [
{
"id": 1,
"description": "desc1",
"_links": {
"self": {
"href": "http://localhost:8080/stores/1"
},
"store": {
"href": "http://localhost:8080/stores/1"
},
"province": {
"href": "http://localhost:8080/stores/1/province/1" ==> Expecting the url with provinceID since its many to one
}
}
}
]
//Simplified for simplicity sake
}
实际结果 href 中没有省 ID
{
"stores": [
{
"id": 1,
"description": "desc1",
"_links": {
"self": {
"href": "http://localhost:8080/stores/1"
},
"store": {
"href": "http://localhost:8080/stores/1"
},
"province": {
"href": "http://localhost:8080/stores/1/province" ==> //NO ID!
}
}
}
]
//Simplified for simplicity sake
}
基本上我期待
这个
"province": {
"href": "http://localhost:8080/stores/1/province/1" ==> Expecting the url with provinceID since its many to one
}
而不是这个
"province": {
"href": "http://localhost:8080/stores/1/province" //NO province ID
}
编辑 6 月 21 日 11:54
由于尝试执行时出错,我已将 Store 上的 FetchType.LAZY 更改为 EAGER
"http://localhost:8080/stores/1/province/1"
得到
"org.springframework.http.converter.HttpMessageNotWritableException",
"message": "Could not write JSON: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer
【问题讨论】:
-
问题可能是@ManyToOne,因为链接存储在Province 表而不是Store 表中。但老实说:我以前从未见过这个问题。
标签: spring spring-data spring-data-rest