有實(shí)體類user 和Info ,info對(duì)應(yīng)的表中有user的主鍵作為外鍵,需求是查詢info可以得到對(duì)應(yīng)的user,查詢user可以得到對(duì)應(yīng)的info。package?com.zxy.demo.entity;
import?com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import?com.fasterxml.jackson.annotation.JsonIgnoreType;
import?javax.persistence.*;
@Entity
public?class?User?{
????@Id
????@GeneratedValue
????private?Integer?id;
????private?Integer?age;
????@OneToOne(mappedBy?=?"user",cascade?=?CascadeType.ALL)
????private?Info?userInfo;
????public?Integer?getId()?{
????????return?id;
????}
????public?void?setId(Integer?id)?{
????????this.id?=?id;
????}
????public?Integer?getAge()?{
????????return?age;
????}
????public?void?setAge(Integer?age)?{
????????this.age?=?age;
????}
????public?Info?getUserInfo()?{
????????return?userInfo;
????}
????public?void?setUserInfo(Info?userInfo)?{
????????this.userInfo?=?userInfo;
????}
}package?com.zxy.demo.entity;
import?javax.persistence.*;
@Entity
public?class?Info?{
????@Id
????@GeneratedValue
????private?Integer?id;
????@Column(nullable?=?false)
????private?String?idCard;
????@OneToOne(cascade?=?CascadeType.ALL)
????@JoinColumn(unique?=?true)
????private?User?user;
????public?Integer?getId()?{
????????return?id;
????}
????public?void?setId(Integer?id)?{
????????this.id?=?id;
????}
????public?String?getIdCard()?{
????????return?idCard;
????}
????public?void?setIdCard(String?idCard)?{
????????this.idCard?=?idCard;
????}
????public?User?getUser()?{
????????return?user;
????}
????public?void?setUser(User?user)?{
????????this.user?=?user;
????}
????@Override
????public?String?toString()?{
????????return?"Info{"?+
????????????????"id="?+?id?+
????????????????",?idCard='"?+?idCard?+?'\''?+
????????????????",?user="?+?user?+
????????????????'}';
????}
}當(dāng)調(diào)用jpa的findOne方法時(shí),hibernate會(huì)注入user中的info,然后又一次注入info中的user,這樣很蠢。網(wǎng)上找到控制json解析的注解,這樣做還不如直接單項(xiàng)關(guān)聯(lián),治標(biāo)不治本。求一個(gè)解決方案!
1 回答

特南克斯
TA貢獻(xiàn)14條經(jīng)驗(yàn) 獲得超9個(gè)贊
控制json解析的深度就好了,沒有其他更合適的方法的。
你可以看一下這個(gè)回答:
spring-data-jpa多對(duì)多雙向關(guān)聯(lián),查詢的時(shí)候進(jìn)入死循環(huán)
添加回答
舉報(bào)
0/150
提交
取消