collections.html

来自「是一个中文的Hibernate库文档」· HTML 代码 · 共 521 行 · 第 1/5 页

HTML
521 行
字号
                &#35201;&#24314;&#31435;&#19968;&#20010;&#21452;&#21521;&#30340;&#22810;&#23545;&#22810;&#20851;&#32852;&#65292;&#21482;&#38656;&#35201;&#26144;&#23556;&#20004;&#20010;many-to-many&#20851;&#32852;&#21040;&#21516;&#19968;&#20010;&#25968;&#25454;&#24211;&#34920;&#20013;&#65292;&#24182;&#20877;&#23450;&#20041;&#20854;&#20013;&#30340;&#19968;&#31471;&#20026;<span class="emphasis"><em>inverse</em></span>(&#20351;&#29992;&#21738;&#19968;&#31471;&#35201;&#26681;&#25454;&#20320;&#30340;&#36873;&#25321;&#65292;&#20294;&#23427;&#19981;&#33021;&#26159;&#19968;&#20010;&#32034;&#24341;&#38598;&#21512;)&#12290;            </p><p>		&#36825;&#37324;&#26377;&#19968;&#20010;many-to-many&#30340;&#21452;&#21521;&#20851;&#32852;&#30340;&#20363;&#23376;;&#27599;&#19968;&#20010;category&#37117;&#21487;&#20197;&#26377;&#24456;&#22810;items,&#27599;&#19968;&#20010;items&#21487;&#20197;&#23646;&#20110;&#24456;&#22810;categories&#65306;		</p><pre class="programlisting">&lt;class name="Category"&gt;    &lt;id name="id" column="CATEGORY_ID"/&gt;    ...    &lt;bag name="items" table="CATEGORY_ITEM"&gt;        &lt;key column="CATEGORY_ID"/&gt;        &lt;many-to-many class="Item" column="ITEM_ID"/&gt;    &lt;/bag&gt;&lt;/class&gt;&lt;class name="Item"&gt;    &lt;id name="id" column="CATEGORY_ID"/&gt;    ...    &lt;!-- inverse end --&gt;    &lt;bag name="categories" table="CATEGORY_ITEM" inverse="true"&gt;        &lt;key column="ITEM_ID"/&gt;        &lt;many-to-many class="Category" column="CATEGORY_ID"/&gt;    &lt;/bag&gt;&lt;/class&gt;</pre><p>                &#22914;&#26524;&#21482;&#23545;&#20851;&#32852;&#30340;&#21453;&#21521;&#31471;&#36827;&#34892;&#20102;&#25913;&#21464;&#65292;&#36825;&#20010;&#25913;&#21464;<span class="emphasis"><em>&#19981;&#20250;</em></span>&#34987;&#25345;&#20037;&#21270;&#12290;            &#36825;&#34920;&#31034;Hibernate&#20026;&#27599;&#20010;&#21452;&#21521;&#20851;&#32852;&#22312;&#20869;&#23384;&#20013;&#23384;&#22312;&#20004;&#27425;&#34920;&#29616;,&#19968;&#20010;&#20174;A&#36830;&#25509;&#21040;B,&#21478;&#19968;&#20010;&#20174;B&#36830;&#25509;&#21040;A&#12290;&#22914;&#26524;&#20320;&#22238;&#24819;&#19968;&#19979;Java&#23545;&#35937;&#27169;&#22411;&#65292;&#25105;&#20204;&#26159;&#22914;&#20309;&#22312;Java&#20013;&#21019;&#24314;&#22810;&#23545;&#22810;&#20851;&#31995;&#30340;&#65292;&#36825;&#21487;&#20197;&#35753;&#20320;&#26356;&#23481;&#26131;&#29702;&#35299;&#65306;            </p><pre class="programlisting">category.getItems().add(item);          // The category now "knows" about the relationshipitem.getCategories().add(category);     // The item now "knows" about the relationshipsession.persist(item);                   // The relationship won''t be saved!session.persist(category);               // The relationship will be saved</pre><p>            &#38750;&#21453;&#21521;&#31471;&#29992;&#20110;&#25226;&#20869;&#23384;&#20013;&#30340;&#34920;&#31034;&#20445;&#23384;&#21040;&#25968;&#25454;&#24211;&#20013;&#12290;        </p><p>                &#35201;&#24314;&#31435;&#19968;&#20010;&#19968;&#23545;&#22810;&#30340;&#21452;&#21521;&#20851;&#32852;&#65292;&#20320;&#21487;&#20197;&#36890;&#36807;&#25226;&#19968;&#20010;&#19968;&#23545;&#22810;&#20851;&#32852;&#65292;&#20316;&#20026;&#19968;&#20010;&#22810;&#23545;&#19968;&#20851;&#32852;&#26144;&#23556;&#21040;&#21040;&#21516;&#19968;&#24352;&#34920;&#30340;&#23383;&#27573;&#19978;&#65292;&#24182;&#19988;&#22312;"&#22810;"&#30340;&#37027;&#19968;&#31471;&#23450;&#20041;<tt class="literal">inverse="true"</tt>&#12290;            </p><pre class="programlisting">&lt;class name="Parent"&gt;    &lt;id name="id" column="parent_id"/&gt;    ....    &lt;set name="children" inverse="true"&gt;        &lt;key column="parent_id"/&gt;        &lt;one-to-many class="Child"/&gt;    &lt;/set&gt;&lt;/class&gt;&lt;class name="eg.Child"&gt;    &lt;id name="id" column="id"/&gt;    ....    &lt;many-to-one name="parent"         class="Parent"         column="parent_id"        not-null="true"/&gt;&lt;/class&gt;</pre><p>                &#22312;&#8220;&#19968;&#8221;&#36825;&#19968;&#31471;&#23450;&#20041;<tt class="literal">inverse="true"</tt>&#19981;&#20250;&#24433;&#21709;&#32423;&#32852;&#25805;&#20316;&#65292;&#20108;&#32773;&#26159;&#27491;&#20132;&#30340;&#27010;&#24565;&#65281;            </p></div><div class="sect2" lang="zh-cn"><div class="titlepage"><div><div><h3 class="title"><a name="collections-ternary"></a>7.3.3.&nbsp;&#19977;&#37325;&#20851;&#32852;&#65288;Ternary associations&#65289;</h3></div></div><div></div></div><p>            	&#26377;&#19977;&#31181;&#21487;&#33021;&#30340;&#36884;&#24452;&#26469;&#26144;&#23556;&#19968;&#20010;&#19977;&#37325;&#20851;&#32852;&#12290;&#31532;&#19968;&#31181;&#26159;&#20351;&#29992;&#19968;&#20010;<tt class="literal">Map</tt>&#65292;&#25226;&#19968;&#20010;&#20851;&#32852;&#20316;&#20026;&#20854;&#32034;&#24341;&#65306;            </p><pre class="programlisting">&lt;map name="contracts"&gt;    &lt;key column="employer_id" not-null="true"/&gt;    &lt;map-key-many-to-many column="employee_id" class="Employee"/&gt;    &lt;one-to-many class="Contract"/&gt;&lt;/map&gt;</pre><pre class="programlisting">&lt;map name="connections"&gt;    &lt;key column="incoming_node_id"/&gt;    &lt;map-key-many-to-many column="outgoing_node_id" class="Node"/&gt;    &lt;many-to-many column="connection_id" class="Connection"/&gt;&lt;/map&gt;</pre><p>        &#31532;&#20108;&#31181;&#26041;&#27861;&#26159;&#31616;&#21333;&#30340;&#25226;&#20851;&#32852;&#37325;&#26032;&#24314;&#27169;&#20026;&#19968;&#20010;&#23454;&#20307;&#31867;&#12290;&#36825;&#20351;&#25105;&#20204;&#26368;&#32463;&#24120;&#20351;&#29992;&#30340;&#26041;&#27861;&#12290;        </p><p>        &#26368;&#21518;&#19968;&#31181;&#36873;&#25321;&#26159;&#20351;&#29992;&#22797;&#21512;&#20803;&#32032;&#65292;&#25105;&#20204;&#20250;&#22312;&#21518;&#38754;&#35752;&#35770;        </p></div><div class="sect2" lang="zh-cn"><div class="titlepage"><div><div><h3 class="title"><a name="collections-idbag"></a>7.3.4.&nbsp;<tt class="literal">&#20351;&#29992;&lt;idbag&gt;</tt></h3></div></div><div></div></div><p>                &#22914;&#26524;&#20320;&#23436;&#20840;&#20449;&#22857;&#25105;&#20204;&#23545;&#20110;&#8220;&#32852;&#21512;&#20027;&#38190;&#65288;composite keys&#65289;&#26159;&#20010;&#22351;&#19996;&#35199;&#8221;&#65292;&#21644;&#8220;&#23454;&#20307;&#24212;&#35813;&#20351;&#29992;&#65288;&#26080;&#26426;&#30340;&#65289;&#33258;&#24049;&#29983;&#25104;&#30340;&#20195;&#29992;&#26631;&#35782;&#31526;&#65288;surrogate keys&#65289;&#8221;&#30340;&#35266;&#28857;&#65292;&#20063;&#35768;&#20320;&#20250;&#24863;&#21040;&#26377;&#19968;&#20123;&#22855;&#24618;&#65292;&#25105;&#20204;&#30446;&#21069;&#20026;&#27490;&#23637;&#31034;&#30340;&#22810;&#23545;&#22810;&#20851;&#32852;&#21644;&#20540;&#38598;&#21512;&#37117;&#26159;&#26144;&#23556;&#25104;&#20026;&#24102;&#26377;&#32852;&#21512;&#20027;&#38190;&#30340;&#34920;&#30340;&#65281;&#29616;&#22312;&#65292;&#36825;&#19968;&#28857;&#38750;&#24120;&#20540;&#24471;&#20105;&#36777;&#65307;&#30475;&#19978;&#21435;&#19968;&#20010;&#21333;&#32431;&#30340;&#20851;&#32852;&#34920;&#24182;&#19981;&#33021;&#20174;&#20195;&#29992;&#26631;&#35782;&#31526;&#20013;&#33719;&#24471;&#20160;&#20040;&#22909;&#22788;&#65288;&#34429;&#28982;&#20351;&#29992;&#32452;&#21512;&#20540;&#30340;&#38598;&#21512;<span class="emphasis"><em>&#21487;&#33021;</em></span>&#20250;&#33719;&#24471;&#19968;&#28857;&#22909;&#22788;&#65289;&#12290;&#19981;&#36807;&#65292;Hibernate&#25552;&#20379;&#20102;&#19968;&#20010;&#65288;&#19968;&#28857;&#28857;&#35797;&#39564;&#24615;&#36136;&#30340;&#65289;&#21151;&#33021;&#65292;&#35753;&#20320;&#25226;&#22810;&#23545;&#22810;&#20851;&#32852;&#21644;&#20540;&#38598;&#21512;&#24212;&#24471;&#21040;&#19968;&#20010;&#20351;&#29992;&#20195;&#29992;&#26631;&#35782;&#31526;&#30340;&#34920;&#21435;&#12290;            </p><p>                <tt class="literal">&lt;idbag&gt;</tt> &#23646;&#24615;&#35753;&#20320;&#20351;&#29992;bag&#35821;&#20041;&#26469;&#26144;&#23556;&#19968;&#20010;<tt class="literal">List</tt> (&#25110;<tt class="literal">Collection</tt>)&#12290;            </p><pre class="programlisting">&lt;idbag name="lovers" table="LOVERS"&gt;    &lt;collection-id column="ID" type="long"&gt;        &lt;generator class="sequence"/&gt;    &lt;/collection-id&gt;    &lt;key column="PERSON1"/&gt;    &lt;many-to-many column="PERSON2" class="eg.Person" outer-join="true"/&gt;&lt;/idbag&gt;</pre><p>                &#20320;&#21487;&#20197;&#29702;&#35299;&#65292;<tt class="literal">&lt;idbag&gt;</tt>&#20154;&#24037;&#30340;id&#29983;&#25104;&#22120;&#65292;&#23601;&#22909;&#20687;&#26159;&#23454;&#20307;&#31867;&#19968;&#26679;&#65281;&#38598;&#21512;&#30340;&#27599;&#19968;&#34892;&#37117;&#26377;&#19968;&#20010;&#19981;&#21516;&#30340;&#20154;&#36896;&#20851;&#38190;&#23383;&#12290;&#20294;&#26159;&#65292;Hibernate&#27809;&#26377;&#25552;&#20379;&#20219;&#20309;&#26426;&#21046;&#26469;&#35753;&#20320;&#21462;&#24471;&#26576;&#20010;&#29305;&#23450;&#34892;&#30340;&#20154;&#36896;&#20851;&#38190;&#23383;&#12290;            </p><p>                &#27880;&#24847;<tt class="literal">&lt;idbag&gt;</tt>&#30340;&#26356;&#26032;&#24615;&#33021;&#35201;&#27604;&#26222;&#36890;&#30340;<tt class="literal">&lt;bag&gt;</tt>&#39640;&#24471;&#22810;&#65281;Hibernate&#21487;&#20197;&#26377;&#25928;&#30340;&#23450;&#20301;&#21040;&#19981;&#21516;&#30340;&#34892;&#65292;&#20998;&#21035;&#36827;&#34892;&#26356;&#26032;&#25110;&#21024;&#38500;&#24037;&#20316;&#65292;&#23601;&#22914;&#21516;&#22788;&#29702;&#19968;&#20010;list, map&#25110;&#32773;set&#19968;&#26679;&#12290;                            </p><p>                &#22312;&#30446;&#21069;&#30340;&#23454;&#29616;&#20013;&#65292;&#36824;&#19981;&#25903;&#25345;&#20351;&#29992;<tt class="literal">identity</tt>&#26631;&#35782;&#31526;&#29983;&#25104;&#22120;&#31574;&#30053;&#26469;&#29983;&#25104;<tt class="literal">&lt;idbag&gt;</tt>&#38598;&#21512;&#30340;&#26631;&#35782;&#31526;&#12290;            </p></div></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="collections-example"></a>7.4.&nbsp;&#38598;&#21512;&#20363;&#23376;&#65288;Collection example&#65289;</h2></div></div><div></div></div><p>                &#22312;&#21069;&#38754;&#30340;&#20960;&#20010;&#31456;&#33410;&#30340;&#30830;&#38750;&#24120;&#20196;&#20154;&#36855;&#24785;&#12290; &#22240;&#27492;&#35753;&#25105;&#20204;&#26469;&#30475;&#19968;&#20010;&#20363;&#23376;&#12290;&#36825;&#20010;&#31867;&#65306;            </p><pre class="programlisting">package eg;import java.util.Set;public class Parent {    private long id;    private Set children;    public long getId() { return id; }    private void setId(long id) { this.id=id; }    private Set getChildren() { return children; }    private void setChildren(Set children) { this.children=children; }    ....    ....}</pre><p>                &#36825;&#20010;&#31867;&#26377;&#19968;&#20010;<tt class="literal">Child</tt>&#30340;&#23454;&#20363;&#38598;&#21512;&#12290;&#22914;&#26524;&#27599;&#19968;&#20010;&#23376;&#23454;&#20363;&#33267;&#22810;&#26377;&#19968;&#20010;&#29238;&#23454;&#20363;, &#37027;&#20040;&#26368;&#33258;&#28982;&#30340;&#26144;&#23556;&#26159;&#19968;&#20010;one-to-many&#30340;&#20851;&#32852;&#20851;&#31995;&#65306;            </p><pre class="programlisting">&lt;hibernate-mapping&gt;    &lt;class name="Parent"&gt;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?