I have People
entity that has name
and parent
where parent
is a 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:
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.
What am I doing wrong with the Hibernate One-to-One
relationship?
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)
From reading your code i think you want @JoinColumn
.
See Hibernate self-referencing one-to-one relationship - mapped/owned side
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.