亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

spring-boot hibernate 雙向關聯查詢的坑

spring-boot hibernate 雙向關聯查詢的坑

暴躁的代碼 2017-08-07 23:55:18
有實體類user 和Info ,info對應的表中有user的主鍵作為外鍵,需求是查詢info可以得到對應的user,查詢user可以得到對應的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?+ ????????????????'}'; ????} }當調用jpa的findOne方法時,hibernate會注入user中的info,然后又一次注入info中的user,這樣很蠢。網上找到控制json解析的注解,這樣做還不如直接單項關聯,治標不治本。求一個解決方案!
查看完整描述

1 回答

?
特南克斯

TA貢獻14條經驗 獲得超9個贊

控制json解析的深度就好了,沒有其他更合適的方法的。


你可以看一下這個回答:


spring-data-jpa多對多雙向關聯,查詢的時候進入死循環

查看完整回答
反對 回復 2017-11-01
  • 1 回答
  • 0 關注
  • 2825 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號