[英]Spring Boot/Hibernate: How to define one-to-one unidirectional relationship
I have People
entity that has name
and parent
where parent
is a People
. 我有具有name
和parent
People
实体,其中parent
是People
。
@Entity
public class People {
@Id
private Long id;
@Column
@NotNull(message = "error.name.type.null")
private String name;
@OneToOne(cascade=CascadeType.ALL,fetch = FetchType.EAGER)
private People parent;
// getters and setters
}
When I store data using PUT
, I specify the id 1
as a path variable and provide the name
as data input and did not pass parent
like: 当我使用PUT
存储数据时,我将id 1
指定为路径变量,并提供name
作为数据输入,并且没有像下面那样传递parent
:
curl -i -X PUT -H "Content-Type:application/json" -d '{"name":"John"}'
Response 响应
http://localhost:8080/people/1
HTTP/1.1 200
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 12 Oct 2016 13:51:52 GMT
When I retrieve what is stored, parent
already have a link to the object itself although I passed null
when making the curl
request. 当我检索存储的内容时,尽管我在发出curl
请求时传递了null
,但parent
已经具有指向对象本身的链接。
What am I doing wrong with the Hibernate One-to-One
relationship? 休眠One-to-One
关系在做什么?
curl -i -X GET http://localhost:8080/people
HTTP/1.1 200
Content-Type: application/hal+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 12 Oct 2016 13:53:10 GMT
{
"_embedded" : {
"people" : [ {
"name" : "John",
"_links" : {
"self" : {
"href" : "http://localhost:8080/people/1"
},
"people" : {
"href" : "http://localhost:8080/people/1"
},
"parent" : {
"href" : "http://localhost:8080/people/1/parent"
}
}
} ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/people"
},
"profile" : {
"href" : "http://localhost:8080/profile/people"
},
"search" : {
"href" : "http://localhost:8080/people/search"
}
}
}
You're missing either @JoinColumn(name="parent_id")
or @OneToOne(mappedBy="id", cascade=CascadeType.ALL,fetch = FetchType.EAGER)
您缺少@JoinColumn(name="parent_id")
或@OneToOne(mappedBy="id", cascade=CascadeType.ALL,fetch = FetchType.EAGER)
From reading your code i think you want @JoinColumn
. 通过阅读您的代码,我认为您需要@JoinColumn
。
See Hibernate self-referencing one-to-one relationship - mapped/owned side 请参见Hibernate自引用一对一关系-映射/拥有的一面
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.