JPA鐨勬噿鍔犺浇

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JPA鐨勬噿鍔犺浇相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/copy' title='copy'>copy   鐩殑   util   涓昏〃   post   缁撴潫   澶氬涓€   hibernate   inf   

 

   鎳掑姞杞絃AZY鍜屽疄鏃跺姞杞紼AGER鐨勬蹇?鍦ㄥ悇绉嶅紑鍙戣瑷€涓兘鏈夊箍娉涘簲鐢ㄣ€傚叾鐩殑鏄疄鐜板叧鑱旀暟鎹殑閫夋嫨鎬у姞杞斤紝鎳掑姞杞芥槸鍦ㄥ睘鎬ц寮曠敤鏃讹紝鎵嶇敓鎴愭煡璇㈣鍙ワ紝鎶藉彇鐩稿叧鑱旀暟鎹€傝€屽疄鏃跺姞杞藉垯鏄墽琛屽畬涓绘煡璇㈠悗,涓嶇鏄惁琚紩鐢?绔嬮┈鎵ц鍚庣画鐨勫叧鑱旀暟鎹煡璇€傜ぞ鍖洪噷鏈変汉璁や负鎳掑姞杞借繖绉嶅姛鑳芥瘮杈冮浮鑲嬶紝杩欑浜嬩粊鑰呰浠侊紝鏅鸿€呰鏅哄暒锛屼釜浜鸿寰椾緷鑷繁涓氬姟鍦烘櫙鑰屽畾銆?/p>

   椤哄甫璇翠竴鍙?浣跨敤鎳掑姞杞芥潵璋冪敤鍏宠仈鏁版嵁锛屽繀椤昏淇濊瘉涓绘煡璇ession(鏁版嵁搴撹繛鎺ヤ細璇?鐨勭敓鍛藉懆鏈熸病鏈夌粨鏉燂紝鍚﹀垯锛屼綘鏄棤娉曟娊鍙栧埌鏁版嵁鐨勩€傚湪Spring Data JPA涓浣曡嚜宸辨帶鍒秙ession鐨勭敓鍛藉懆鏈燂紝杩欎釜鍔熻兘鎴戣繕娌℃湁鐮旂┒锛屼綔涓轰笅涓€娆$殑鐮旂┒璇鹃銆?/p>

   鍦ㄤ粖澶╄В鏋愭噿鍔犺浇涓庡疄鏃跺姞杞界殑瀹炰緥鏃讹紝闇€瑕佸€熷姪瀹炰綋鍏崇郴鏄犲皠鍔熻兘锛岄厤缃疄浣撻棿鐨勪竴瀵瑰(OneToMany)鍜屽瀵逛竴(ManyToOne)鍏崇郴銆傚綋鐒讹紝杩樻湁OneToOne銆丮anyToMany鍏崇郴锛岃繖涓鏈夊叴瓒eぇ瀹跺氨鑷繁鐮旂┒涓嬩簡銆?/p>

   涓€銆佷负瀹炰綋绫籔rocessBlock鍜孨ode娣诲姞涓€瀵瑰鐨勫叧鑱斿叧绯汇€傚湪鎴戣繖涓ず渚嬩腑ProcessBlock鏄祦绋嬪畾涔夌被锛屼竴涓祦绋嬪畾涔夊搴斿涓祦绋嬭妭鐐癸紝鍗砃ode琛ㄣ€備笅闈㈢粰鍑轰唬鐮併€傘€€銆€

銆€銆€ 1銆丳rocessBlock瀹炰綋瀹氫箟

鎶€鏈浘鐗? src=
@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;
    }
……
鎶€鏈浘鐗? src=

銆€銆€瀹氫箟Set<Node>灞炴€?nodeSet,涓哄叾娣诲姞娉ㄨВ@OneToMany锛屾剰鎬濆氨鏄竴涓狿rocessBlock瀹炰綋瀵瑰簲澶氫釜Node鑺傜偣銆傛敞瑙e睘鎬?mappedBy,鏄寚鍏宠仈鍏崇郴鍙互浠嶯ode绫讳腑鐨刾rocessblock灞炴€т笂鑾峰彇锛沜ascade灞炴€ц缃垚CascadeType.ALL,鏄涓讳粠琛ㄥ叏闈㈠缓绔嬬骇鑱斿叧绯伙紱fetch灞炴€ц缃垚FetchType.EAGER锛屾槸鎸囧姞杞借鍒欐槸鍗虫椂鍔犺浇銆傛煡鐪嬫簮鐮佷細鍙戠幇榛樿鎯呭喌涓嬶紝OneToMany娉ㄨВ鐨刦etch灞炴€ц缃殑鏄疐etchType.LAZY銆?/p>

銆€銆€2銆丯ode瀹炰綋瀹氫箟

鎶€鏈浘鐗? src=
@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;
    }   
……
鎶€鏈浘鐗? src=

銆€銆€ 瀹氫箟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>

鎶€鏈浘鐗? src=
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();
鎶€鏈浘鐗? src=

 

銆€銆€鎶€鏈浘鐗? src=

鎶€鏈浘鐗? src=
-- 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=?
鎶€鏈浘鐗? src=

銆€銆€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>();

銆€銆€銆€鎶€鏈浘鐗? src=

-- 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);
鎶€鏈浘鐗? src=
-- 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=?
鎶€鏈浘鐗? src=

 

銆€銆€鑷虫锛屾湰娆℃祴璇曠殑涓昏鍐呭灏辫瀹屼簡锛岃皟璇曠粏鑺傛瘮杈冪箒鏉傝繖閲屼笉鍐嶇粏琛ㄣ€傚湪鏈€鍚庢湁涓枒闂笌澶у浜ゆ祦涓嬶紝鍦∕anyToOne杩欎釜娉ㄨВ涓湁涓€涓猳ptional鐨勫睘鎬э紝婧愮爜閲屾爣娉ㄨ灞炴€ч粯璁よ缃垚true锛屽嵆涓庝富琛ㄩ棿鐨勫叧鑱斿彲浠ヤ负null锛岃缃垚false鏃讹紝鍒欏叧鑱斿叧绯讳笉鑳戒负绌恒€傛垜鐚滄兂鐫€璁剧疆姝ゅ叧绯诲悗锛岀敓鎴愮殑sql璇彞鍙兘浼氭湁鎵€涓嶅悓锛屾垨鑰呬細寮曡捣绯荤粺杩愯鏃舵姤閿欎粈涔堢殑銆備絾鏄祴璇曞彂鐜帮紝骞舵病鏈夊奖鍝嶏紝sql璇彞閮芥槸left outer join鍏宠仈锛岃缃垚false鏃讹紝绌哄叧绯讳篃涓嶄細閫犳垚鎶ラ敊锛屼技涔庢病鏈変綔鐢紝璋冭瘯婧愮爜鏆傛椂杩樻病鏈夊彂鐜板叾璋冪敤閫昏緫銆傛湁閬撳弸鐭ラ亾杩欎釜鍔熻兘鍚楋紵鍙互甯垜瑙g瓟涓€涓嬶紝璋㈣阿锛?/p>

以上是关于JPA鐨勬噿鍔犺浇的主要内容,如果未能解决你的问题,请参考以下文章

Springboot鑷姩鍔犺浇宸ュ叿-devtools鐨勭悊瑙d笌浣跨敤

娣卞叆鎺㈢┒JVM涔嬬被鍔犺浇涓庡弻浜插娲炬満鍒?/h1>

濡備綍鎻愬崌浣庣璁惧鐨刉eb鎬ц兘锛熻瘯璇曡嚜閫傚簲鍔犺浇妯″紡

鎵嬫捀Spring妗嗘灦锛岃璁′笌瀹炵幇璧勬簮鍔犺浇鍣紝浠嶴pring.xml瑙f瀽鍜屾敞鍐孊ean瀵硅薄

Echars璁茶В鍒嗘瀽

ServletContext瀵硅薄

(c)2006-2024 SYSTEM All Rights Reserved IT常识