鎳掑姞杞絃AZY鍜屽疄鏃跺姞杞紼AGER鐨勬蹇?鍦ㄥ悇绉嶅紑鍙戣瑷€涓兘鏈夊箍娉涘簲鐢ㄣ€傚叾鐩殑鏄疄鐜板叧鑱旀暟鎹殑閫夋嫨鎬у姞杞斤紝鎳掑姞杞芥槸鍦ㄥ睘鎬ц寮曠敤鏃讹紝鎵嶇敓鎴愭煡璇㈣鍙ワ紝鎶藉彇鐩稿叧鑱旀暟鎹€傝€屽疄鏃跺姞杞藉垯鏄墽琛屽畬涓绘煡璇㈠悗,涓嶇鏄惁琚紩鐢?绔嬮┈鎵ц鍚庣画鐨勫叧鑱旀暟鎹煡璇€傜ぞ鍖洪噷鏈変汉璁や负鎳掑姞杞借繖绉嶅姛鑳芥瘮杈冮浮鑲嬶紝杩欑浜嬩粊鑰呰浠侊紝鏅鸿€呰鏅哄暒锛屼釜浜鸿寰椾緷鑷繁涓氬姟鍦烘櫙鑰屽畾銆?/p>
椤哄甫璇翠竴鍙?浣跨敤鎳掑姞杞芥潵璋冪敤鍏宠仈鏁版嵁锛屽繀椤昏淇濊瘉涓绘煡璇ession(鏁版嵁搴撹繛鎺ヤ細璇?鐨勭敓鍛藉懆鏈熸病鏈夌粨鏉燂紝鍚﹀垯锛屼綘鏄棤娉曟娊鍙栧埌鏁版嵁鐨勩€傚湪Spring Data JPA涓浣曡嚜宸辨帶鍒秙ession鐨勭敓鍛藉懆鏈燂紝杩欎釜鍔熻兘鎴戣繕娌℃湁鐮旂┒锛屼綔涓轰笅涓€娆$殑鐮旂┒璇鹃銆?/p>
鍦ㄤ粖澶╄В鏋愭噿鍔犺浇涓庡疄鏃跺姞杞界殑瀹炰緥鏃讹紝闇€瑕佸€熷姪瀹炰綋鍏崇郴鏄犲皠鍔熻兘锛岄厤缃疄浣撻棿鐨勪竴瀵瑰(OneToMany)鍜屽瀵逛竴(ManyToOne)鍏崇郴銆傚綋鐒讹紝杩樻湁OneToOne銆丮anyToMany鍏崇郴锛岃繖涓鏈夊叴瓒eぇ瀹跺氨鑷繁鐮旂┒涓嬩簡銆?/p>
涓€銆佷负瀹炰綋绫籔rocessBlock鍜孨ode娣诲姞涓€瀵瑰鐨勫叧鑱斿叧绯汇€傚湪鎴戣繖涓ず渚嬩腑ProcessBlock鏄祦绋嬪畾涔夌被锛屼竴涓祦绋嬪畾涔夊搴斿涓祦绋嬭妭鐐癸紝鍗砃ode琛ㄣ€備笅闈㈢粰鍑轰唬鐮併€傘€€銆€
銆€銆€ 1銆丳rocessBlock瀹炰綋瀹氫箟
@Entity(name = "nbpm_processblock") @NamedQuery(name = "ProcessBlock.findByName", query = "select p from nbpm_processblock p where p.name=?1") public class ProcessBlock implements Serializable { private static final long serialVersionUID = 1L; @Id @Column(name = "id") long id; @Column(name = "name") String name; @Column(name = "description") String description; @OneToMany(mappedBy="processblock",cascade=CascadeType.ALL,fetch = FetchType.EAGER) Set<Node> nodeSet = new HashSet<Node>(); public long getId() { return id; }
……
銆€銆€瀹氫箟Set<Node>灞炴€?nodeSet,涓哄叾娣诲姞娉ㄨВ@OneToMany锛屾剰鎬濆氨鏄竴涓狿rocessBlock瀹炰綋瀵瑰簲澶氫釜Node鑺傜偣銆傛敞瑙e睘鎬?mappedBy,鏄寚鍏宠仈鍏崇郴鍙互浠嶯ode绫讳腑鐨刾rocessblock灞炴€т笂鑾峰彇锛沜ascade灞炴€ц缃垚CascadeType.ALL,鏄涓讳粠琛ㄥ叏闈㈠缓绔嬬骇鑱斿叧绯伙紱fetch灞炴€ц缃垚FetchType.EAGER锛屾槸鎸囧姞杞借鍒欐槸鍗虫椂鍔犺浇銆傛煡鐪嬫簮鐮佷細鍙戠幇榛樿鎯呭喌涓嬶紝OneToMany娉ㄨВ鐨刦etch灞炴€ц缃殑鏄疐etchType.LAZY銆?/p>
銆€銆€2銆丯ode瀹炰綋瀹氫箟
@Entity(name = "nbpm_node") public class Node implements Serializable { private static final long serialVersionUID = 1L; @Id @Column(name = "id") long id; @Column(name = "name") String name; @Column(name = "description") String description; @Column(name = "subclass") String subclass; /** * 鍏宠仈鍏崇郴蹇呴』瀛樺湪 */ @ManyToOne(optional = true) @JoinColumn(name="processblock") ProcessBlock processblock; public long getId() { return id; }
……
銆€銆€ 瀹氫箟ProcessBlock灞炴€?processblock,涓哄叾娣诲姞娉ㄨВ@ManyToOne锛屾剰鎬濆氨鏄竴涓狽ode瀹炰綋瀵瑰簲涓€涓狿rocessBlock娴佺▼瀹氫箟銆傛坊鍔犳敞瑙JoinColumn,灞炴€ame璁剧疆鎴?processblock"锛屾剰鎬濇槸nbpm_node琛ㄤ腑鐨刾rocessblock瀛楁鏄閿紝涓嶱rocessBlock鐨勪富閿缓璁叧鑱斿叧绯汇€傞渶瑕佽鏄庝竴涓?nbsp;ManyToOne娉ㄨВ鐨刦etch灞炴€?榛樿鏄疐etchType.EAGER锛屾煡璇㈡椂浼氱珛椹娊鍙栧搴旂殑涓昏〃鏁版嵁銆傝繖鍧楃殑鑰冭檻杩樻槸寰堢粏鑷寸殑锛屽ぇ鑷村氨鏄叧鑱旀暟鎹槸闆嗗悎鏃跺氨鎳掑姞杞斤紝鏄崟鏉℃暟鎹椂灏卞疄鏃跺姞杞姐€?/p>
銆€浜屻€佺紪鍐欐煡璇㈢ず渚嬪苟灞曠ず缁撴灉銆傛煡璇娇鐢ㄨ嚜瀹氫箟鐨凧paUtil宸ュ叿锛屽伐鍏峰疄鐜拌鍙傝€?a href="http://www.cnblogs.com/MrSi/p/8074460.html" target="_blank">銆婅嚜瀹氫箟JpaUtil,蹇€熷畬鎴怘ql鎵ц閫昏緫(涓€)銆?/a>
銆€銆€1銆佹煡璇rocessBlock,涓诲共娴佺▼鏁版嵁锛屽洜涓鸿缃殑nodeSet灞炴€ф槸瀹炴椂鍔犺浇鍔犺浇锛屾煡璇㈠畬鎴愶紝鏂偣鏌ョ湅纭疄鎷垮埌浜嗘祦绋嬪搴旂殑鎵€鏈夎妭鐐逛俊鎭€?/strong>
Map<String, Object> params = new HashMap<>(); params.put("name", "涓诲共娴佺▼"); List<ProcessBlock> list = ApplicationContextUtil.instance.getJpaUtil().list( "select u from simm.spring.entity.ProcessBlock u where u.name=:name", params, ProcessBlock.class); Set<Node> nodes = list.get(0).getNodeSet();
銆€銆€
-- Hibernate妗嗘灦鐢熸垚鐨勬煡璇㈣鍙?----------------- Hibernate: select processblo0_.id as id1_2_, processblo0_.description as descript2_2_, processblo0_.name as name3_2_ from nbpm_processblock processblo0_ where processblo0_.name=? Hibernate: select nodeset0_.processblock as processb5_1_0_, nodeset0_.id as id1_1_0_, nodeset0_.id as id1_1_1_, nodeset0_.description as descript2_1_1_, nodeset0_.name as name3_1_1_, nodeset0_.processblock as processb5_1_1_, nodeset0_.subclass as subclass4_1_1_ from nbpm_node nodeset0_ where nodeset0_.processblock=? Hibernate: select nodeset0_.processblock as processb5_1_0_, nodeset0_.id as id1_1_0_, nodeset0_.id as id1_1_1_, nodeset0_.description as descript2_1_1_, nodeset0_.name as name3_1_1_, nodeset0_.processblock as processb5_1_1_, nodeset0_.subclass as subclass4_1_1_ from nbpm_node nodeset0_ where nodeset0_.processblock=? Hibernate: select nodeset0_.processblock as processb5_1_0_, nodeset0_.id as id1_1_0_, nodeset0_.id as id1_1_1_, nodeset0_.description as descript2_1_1_, nodeset0_.name as name3_1_1_, nodeset0_.processblock as processb5_1_1_, nodeset0_.subclass as subclass4_1_1_ from nbpm_node nodeset0_ where nodeset0_.processblock=? Hibernate: select nodeset0_.processblock as processb5_1_0_, nodeset0_.id as id1_1_0_, nodeset0_.id as id1_1_1_, nodeset0_.description as descript2_1_1_, nodeset0_.name as name3_1_1_, nodeset0_.processblock as processb5_1_1_, nodeset0_.subclass as subclass4_1_1_ from nbpm_node nodeset0_ where nodeset0_.processblock=?
銆€銆€2銆佸皢ProcessBlock绫荤殑灞炴€odeSet璁剧疆鎴愭噿鍔犺浇LAZY锛屾煡璇㈠悗锛屽彂鐜板搴旂殑鑺傜偣鍒楄〃淇℃伅鑾峰彇涓嶅埌浜嗐€傝繖閲屾噿鍔犺浇鐨勬暟鎹鍙栦笉鍒版槸鍥犱负涓绘煡璇㈢殑session宸茬粡缁撴潫銆?/strong>
@OneToMany(mappedBy="processblock",cascade=CascadeType.ALL,fetch = FetchType.LAZY) //@JoinColumn(name="processblock_id") Set<Node> nodeSet = new HashSet<Node>();
銆€銆€銆€
-- Hibernate妗嗘灦鐢熸垚鐨勬煡璇㈣鍙?----------------- Hibernate: select processblo0_.id as id1_2_, processblo0_.description as descript2_2_, processblo0_.name as name3_2_ from nbpm_processblock processblo0_ where processblo0_.name=?
銆€銆€3銆丠ql鐩存帴鏌ヨ鍏宠仈灞炴€?/strong>
List<Node> nodeList = ApplicationContextUtil.instance.getJpaUtil().list( "select u.nodeSet from simm.spring.entity.ProcessBlock u where u.id=1", null, Node.class);
-- Hibernate妗嗘灦鐢熸垚鐨勬煡璇㈣鍙?----------------- Hibernate: select nodeset1_.id as id1_1_, nodeset1_.description as descript2_1_, nodeset1_.name as name3_1_, nodeset1_.processblock as processb5_1_, nodeset1_.subclass as subclass4_1_ nbpm_processblock processblo0_ inner join nbpm_node nodeset1_ on processblo0_.id=nodeset1_.processblock where processblo0_.id=1 Hibernate: select processblo0_.id as id1_2_0_, processblo0_.description as descript2_2_0_, processblo0_.name as name3_2_0_ from nbpm_processblock processblo0_ where processblo0_.id=?
銆€銆€鑷虫锛屾湰娆℃祴璇曠殑涓昏鍐呭灏辫瀹屼簡锛岃皟璇曠粏鑺傛瘮杈冪箒鏉傝繖閲屼笉鍐嶇粏琛ㄣ€傚湪鏈€鍚庢湁涓枒闂笌澶у浜ゆ祦涓嬶紝鍦∕anyToOne杩欎釜娉ㄨВ涓湁涓€涓猳ptional鐨勫睘鎬э紝婧愮爜閲屾爣娉ㄨ灞炴€ч粯璁よ缃垚true锛屽嵆涓庝富琛ㄩ棿鐨勫叧鑱斿彲浠ヤ负null锛岃缃垚false鏃讹紝鍒欏叧鑱斿叧绯讳笉鑳戒负绌恒€傛垜鐚滄兂鐫€璁剧疆姝ゅ叧绯诲悗锛岀敓鎴愮殑sql璇彞鍙兘浼氭湁鎵€涓嶅悓锛屾垨鑰呬細寮曡捣绯荤粺杩愯鏃舵姤閿欎粈涔堢殑銆備絾鏄祴璇曞彂鐜帮紝骞舵病鏈夊奖鍝嶏紝sql璇彞閮芥槸left outer join鍏宠仈锛岃缃垚false鏃讹紝绌哄叧绯讳篃涓嶄細閫犳垚鎶ラ敊锛屼技涔庢病鏈変綔鐢紝璋冭瘯婧愮爜鏆傛椂杩樻病鏈夊彂鐜板叾璋冪敤閫昏緫銆傛湁閬撳弸鐭ラ亾杩欎釜鍔熻兘鍚楋紵鍙互甯垜瑙g瓟涓€涓嬶紝璋㈣阿锛?/p>