??xml version="1.0" encoding="utf-8" standalone="yes"?>国产97色在线,狠狠久久综合,在线亚洲电影http://www.aygfsteel.com/huyi0616/category/28655.htmlzh-cnTue, 29 Dec 2009 17:23:40 GMTTue, 29 Dec 2009 17:23:40 GMT60hibernate中inverse的用?/title><link>http://www.aygfsteel.com/huyi0616/archive/2009/12/18/306563.html</link><dc:creator>一杯清?/dc:creator><author>一杯清?/author><pubDate>Fri, 18 Dec 2009 07:17:00 GMT</pubDate><guid>http://www.aygfsteel.com/huyi0616/archive/2009/12/18/306563.html</guid><wfw:comment>http://www.aygfsteel.com/huyi0616/comments/306563.html</wfw:comment><comments>http://www.aygfsteel.com/huyi0616/archive/2009/12/18/306563.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/huyi0616/comments/commentRss/306563.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/huyi0616/services/trackbacks/306563.html</trackback:ping><description><![CDATA[<p>转自:http://blog.csdn.net/leader_lx/archive/2008/08/06/2774137.aspx<br /> <br /> 一、Inverse是hibernate双向关系中的基本概念。inverse的真正作用就是指定由哪一Ҏl护之间的关联关pR当一方中指定?#8220;inverse=false”Q默认)Q那么那一方就有责任负责之间的兌关系Q说白了是<font color="#ff0000"><strong>hibernate如何生成Sql来维护关联的记录</strong></font>Q?nbsp;</p> <p> </p> <p>        Hibernate仅仅按照L方对象的状态的变化来同步更新数据库。按照原来的映射文gQpeople.getAddresses().add(address)Q即L方对象的状态发生了改变Q因此数据库会跟着对象状态的变化来同步更新数据库Q而address.setPeople(people)Q即被控方对象的状态发生了改变Q它是不能触发对象和数据库的同步更新的?/p> <p> </p> <p><strong style="font-size: 12pt">Q实?Q:</strong></p> <p>举个最单的一对多父子关系。那么代码就写成Q?/p> <p>父亲中的关系映射<br /> </p> <div style="border-right: #cccccc 1px solid; padding-right: 5px; border-top: #cccccc 1px solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left: #cccccc 1px solid; width: 98%; word-break: break-all; padding-top: 4px; border-bottom: #cccccc 1px solid; background-color: #eeeeee"><img id="Codehighlighter1_0_47_Open_Image" onclick="this.style.display='none'; Codehighlighter1_0_47_Open_Text.style.display='none'; Codehighlighter1_0_47_Closed_Image.style.display='inline'; Codehighlighter1_0_47_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" alt="" /><img id="Codehighlighter1_0_47_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_0_47_Closed_Text.style.display='none'; Codehighlighter1_0_47_Open_Image.style.display='inline'; Codehighlighter1_0_47_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedBlock.gif" align="top" alt="" /><span id="Codehighlighter1_0_47_Closed_Text" style="border-right: #808080 1px solid; border-top: #808080 1px solid; display: none; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff"><img src="http://www.aygfsteel.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_0_47_Open_Text"><span style="color: #000000">{set name</span><span style="color: #000000">=</span><span style="color: #000000">"</span><span style="color: #000000">children</span><span style="color: #000000">"</span><span style="color: #000000"> lazy</span><span style="color: #000000">=</span><span style="color: #000000">"</span><span style="color: #000000">true</span><span style="color: #000000">"</span><span style="color: #000000"> inverse</span><span style="color: #000000">=</span><span style="color: #000000">"</span><span style="color: #000000">true</span><span style="color: #000000">"</span><span style="color: #000000">}</span></span><span style="color: #000000"><br /> <img id="Codehighlighter1_55_79_Open_Image" onclick="this.style.display='none'; Codehighlighter1_55_79_Open_Text.style.display='none'; Codehighlighter1_55_79_Closed_Image.style.display='inline'; Codehighlighter1_55_79_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" alt="" /><img id="Codehighlighter1_55_79_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_55_79_Closed_Text.style.display='none'; Codehighlighter1_55_79_Open_Image.style.display='inline'; Codehighlighter1_55_79_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedBlock.gif" align="top" alt="" />      </span><span id="Codehighlighter1_55_79_Closed_Text" style="border-right: #808080 1px solid; border-top: #808080 1px solid; display: none; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff"><img src="http://www.aygfsteel.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_55_79_Open_Text"><span style="color: #000000">{key column</span><span style="color: #000000">=</span><span style="color: #000000">"</span><span style="color: #000000">parent_id</span><span style="color: #000000">"</span><span style="color: #000000">/</span><span style="color: #000000">}</span></span><span style="color: #000000"><br /> <img id="Codehighlighter1_87_119_Open_Image" onclick="this.style.display='none'; Codehighlighter1_87_119_Open_Text.style.display='none'; Codehighlighter1_87_119_Closed_Image.style.display='inline'; Codehighlighter1_87_119_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" alt="" /><img id="Codehighlighter1_87_119_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_87_119_Closed_Text.style.display='none'; Codehighlighter1_87_119_Open_Image.style.display='inline'; Codehighlighter1_87_119_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedBlock.gif" align="top" alt="" />      </span><span id="Codehighlighter1_87_119_Closed_Text" style="border-right: #808080 1px solid; border-top: #808080 1px solid; display: none; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff"><img src="http://www.aygfsteel.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_87_119_Open_Text"><span style="color: #000000">{one</span><span style="color: #000000">-</span><span style="color: #000000">to</span><span style="color: #000000">-</span><span style="color: #000000">many </span><span style="color: #0000ff">class</span><span style="color: #000000">=</span><span style="color: #000000">"</span><span style="color: #000000">test.Child</span><span style="color: #000000">"</span><span style="color: #000000">/</span><span style="color: #000000">}</span></span><span style="color: #000000"><br /> <img id="Codehighlighter1_121_126_Open_Image" onclick="this.style.display='none'; Codehighlighter1_121_126_Open_Text.style.display='none'; Codehighlighter1_121_126_Closed_Image.style.display='inline'; Codehighlighter1_121_126_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" alt="" /><img id="Codehighlighter1_121_126_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_121_126_Closed_Text.style.display='none'; Codehighlighter1_121_126_Open_Image.style.display='inline'; Codehighlighter1_121_126_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedBlock.gif" align="top" alt="" /></span><span id="Codehighlighter1_121_126_Closed_Text" style="border-right: #808080 1px solid; border-top: #808080 1px solid; display: none; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff"><img src="http://www.aygfsteel.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_121_126_Open_Text"><span style="color: #000000">{</span><span style="color: #000000">/</span><span style="color: #000000">set}</span></span></div> <p> </p> <p>儿子中关pL?br /> </p> <div style="border-right: #cccccc 1px solid; padding-right: 5px; border-top: #cccccc 1px solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left: #cccccc 1px solid; width: 98%; word-break: break-all; padding-top: 4px; border-bottom: #cccccc 1px solid; background-color: #eeeeee"><img id="Codehighlighter1_0_62_Open_Image" onclick="this.style.display='none'; Codehighlighter1_0_62_Open_Text.style.display='none'; Codehighlighter1_0_62_Closed_Image.style.display='inline'; Codehighlighter1_0_62_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" alt="" /><img id="Codehighlighter1_0_62_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_0_62_Closed_Text.style.display='none'; Codehighlighter1_0_62_Open_Image.style.display='inline'; Codehighlighter1_0_62_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedBlock.gif" align="top" alt="" /><span id="Codehighlighter1_0_62_Closed_Text" style="border-right: #808080 1px solid; border-top: #808080 1px solid; display: none; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff"><img src="http://www.aygfsteel.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_0_62_Open_Text"><span style="color: #000000">{many</span><span style="color: #000000">-</span><span style="color: #000000">to</span><span style="color: #000000">-</span><span style="color: #000000">one name</span><span style="color: #000000">=</span><span style="color: #000000">"</span><span style="color: #000000">parent</span><span style="color: #000000">"</span><span style="color: #000000"> column</span><span style="color: #000000">=</span><span style="color: #000000">"</span><span style="color: #000000">parent_id</span><span style="color: #000000">"</span><span style="color: #000000"> not</span><span style="color: #000000">-</span><span style="color: #0000ff">null</span><span style="color: #000000">=</span><span style="color: #000000">"</span><span style="color: #000000">true</span><span style="color: #000000">"</span><span style="color: #000000">/</span><span style="color: #000000">}</span></span></div> <p> </p> <p><br />  </p> <div style="border-right: #cccccc 1px solid; padding-right: 5px; border-top: #cccccc 1px solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left: #cccccc 1px solid; width: 98%; word-break: break-all; padding-top: 4px; border-bottom: #cccccc 1px solid; background-color: #eeeeee"><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" alt="" /><span style="color: #000000">Parent p </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> Parent();<br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" alt="" />Child c </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> Child();<br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" alt="" />c.setParent(p);  </span><span style="color: #008000">//</span><span style="color: #008000">l护父子之间关系</span><span style="color: #008000"><br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" alt="" /></span><span style="color: #000000">p.getChildren().add(c);<br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" alt="" /><br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" alt="" />session.save(p);<br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" alt="" />session.flush();<br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" alt="" /></span></div> <p> </p> <p><font color="#ff0000">注意Q{many-to-one}L设成“inverse=false”的,而且q个属性在Mapping中是不存在的Q?/font></p> <p>q样q行的下来的l果是Q?/p> <p>Hibernate: insert into parent (id) values (?)<br /> Hibernate: insert into child (parent_id, id) values (?, ?)</p> <p>那么假如c.setParent(p)注释掉,l果是Q?/p> <p>Hibernate: insert into parent (id) values (?)</p> <p> </p> <p>Q=Q=Q=Q=Q=Q=Q=Q=Q=Q=Q=Q=Q=Q=Q=Q=Q=Q=Q=Q=Q=Q=Q=Q=Q=Q?/p> <p><strong style="font-size: 12pt">Q实?Q:</strong></p> <p>一个Person可以参加多个EventQ一个Event有多个Person参加?br /> 映射文g如下Q?br /> </p> <div style="border-right: rgb(204,204,204) 1px solid; padding-right: 5px; border-top: rgb(204,204,204) 1px solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left: rgb(204,204,204) 1px solid; width: 98%; padding-top: 4px; border-bottom: rgb(204,204,204) 1px solid; background-color: rgb(238,238,238)"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: rgb(0,0,255)"><!-- Person.hbm.xml --><br /> <</span><span style="color: rgb(128,0,0)">hibernate-mapping </span><span style="color: rgb(255,0,0)">package</span><span style="color: rgb(0,0,255)">="events"</span><span style="color: rgb(0,0,255)">></span><span style="color: rgb(0,0,0)"><br />   </span><span style="color: rgb(0,0,255)"><</span><span style="color: rgb(128,0,0)">class </span><span style="color: rgb(255,0,0)">name</span><span style="color: rgb(0,0,255)">="Person"</span><span style="color: rgb(255,0,0)"> table</span><span style="color: rgb(0,0,255)">="person"</span><span style="color: rgb(0,0,255)">></span><span style="color: rgb(0,0,0)"><br />       </span><span style="color: rgb(0,0,255)"><</span><span style="color: rgb(128,0,0)">id </span><span style="color: rgb(255,0,0)">name</span><span style="color: rgb(0,0,255)">="id"</span><span style="color: rgb(255,0,0)"> column</span><span style="color: rgb(0,0,255)">="person_id"</span><span style="color: rgb(0,0,255)">></span><span style="color: rgb(0,0,0)"><br />           </span><span style="color: rgb(0,0,255)"><</span><span style="color: rgb(128,0,0)">generator </span><span style="color: rgb(255,0,0)">class</span><span style="color: rgb(0,0,255)">="native"</span><span style="color: rgb(0,0,255)">/></span><span style="color: rgb(0,0,0)"><br />       </span><span style="color: rgb(0,0,255)"></</span><span style="color: rgb(128,0,0)">id</span><span style="color: rgb(0,0,255)">></span><span style="color: rgb(0,0,0)"><br />       </span><span style="color: rgb(0,0,255)"><</span><span style="color: rgb(128,0,0)">property </span><span style="color: rgb(255,0,0)">name</span><span style="color: rgb(0,0,255)">="age"</span><span style="color: rgb(255,0,0)"> length</span><span style="color: rgb(0,0,255)">="0"</span><span style="color: rgb(0,0,255)">/></span><span style="color: rgb(0,0,0)"><br />       </span><span style="color: rgb(0,0,255)"><</span><span style="color: rgb(128,0,0)">property </span><span style="color: rgb(255,0,0)">name</span><span style="color: rgb(0,0,255)">="firstname"</span><span style="color: rgb(0,0,255)">/></span><span style="color: rgb(0,0,0)"><br />       </span><span style="color: rgb(0,0,255)"><</span><span style="color: rgb(128,0,0)">property </span><span style="color: rgb(255,0,0)">name</span><span style="color: rgb(0,0,255)">="lastname"</span><span style="color: rgb(0,0,255)">/></span><span style="color: rgb(0,0,0)"><br />       </span><span style="color: rgb(0,0,255)"><</span><span style="color: rgb(128,0,0)">set </span><span style="color: rgb(255,0,0)">name</span><span style="color: rgb(0,0,255)">="events"</span><span style="color: rgb(255,0,0)"> table</span><span style="color: rgb(0,0,255)">="person_event"</span><span style="color: rgb(0,0,255)">></span><span style="color: rgb(0,0,0)"><br />           </span><span style="color: rgb(0,0,255)"><</span><span style="color: rgb(128,0,0)">key </span><span style="color: rgb(255,0,0)">column</span><span style="color: rgb(0,0,255)">="person_id"</span><span style="color: rgb(0,0,255)">/></span><span style="color: rgb(0,0,0)"><br />           </span><span style="color: rgb(0,0,255)"><</span><span style="color: rgb(128,0,0)">many-to-many </span><span style="color: rgb(255,0,0)">column</span><span style="color: rgb(0,0,255)">="event_id"</span><span style="color: rgb(255,0,0)"> class</span><span style="color: rgb(0,0,255)">="events.Event"</span><span style="color: rgb(0,0,255)">/></span><span style="color: rgb(0,0,0)"><br />       </span><span style="color: rgb(0,0,255)"></</span><span style="color: rgb(128,0,0)">set</span><span style="color: rgb(0,0,255)">></span><span style="color: rgb(0,0,0)"><br />   </span><span style="color: rgb(0,0,255)"></</span><span style="color: rgb(128,0,0)">class</span><span style="color: rgb(0,0,255)">></span><span style="color: rgb(0,0,0)"><br /> </span><span style="color: rgb(0,0,255)"></</span><span style="color: rgb(128,0,0)">hibernate-mapping</span><span style="color: rgb(0,0,255)">></span></div> <br /> <div style="border-right: rgb(204,204,204) 1px solid; padding-right: 5px; border-top: rgb(204,204,204) 1px solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left: rgb(204,204,204) 1px solid; width: 98%; padding-top: 4px; border-bottom: rgb(204,204,204) 1px solid; background-color: rgb(238,238,238)"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: rgb(0,0,255)"><!-- Event.hbm.xml --><br /> <</span><span style="color: rgb(128,0,0)">hibernate-mapping</span><span style="color: rgb(0,0,255)">></span><span style="color: rgb(0,0,0)"><br />   </span><span style="color: rgb(0,0,255)"><</span><span style="color: rgb(128,0,0)">class </span><span style="color: rgb(255,0,0)">name</span><span style="color: rgb(0,0,255)">="events.Event"</span><span style="color: rgb(255,0,0)"> table</span><span style="color: rgb(0,0,255)">="events"</span><span style="color: rgb(0,0,255)">></span><span style="color: rgb(0,0,0)"><br />       </span><span style="color: rgb(0,0,255)"><</span><span style="color: rgb(128,0,0)">id </span><span style="color: rgb(255,0,0)">name</span><span style="color: rgb(0,0,255)">="id"</span><span style="color: rgb(255,0,0)"> column</span><span style="color: rgb(0,0,255)">="event_id"</span><span style="color: rgb(0,0,255)">></span><span style="color: rgb(0,0,0)"><br />             </span><span style="color: rgb(0,0,255)"><</span><span style="color: rgb(128,0,0)">generator </span><span style="color: rgb(255,0,0)">class</span><span style="color: rgb(0,0,255)">="native"</span><span style="color: rgb(0,0,255)">/></span><span style="color: rgb(0,0,0)"><br />       </span><span style="color: rgb(0,0,255)"></</span><span style="color: rgb(128,0,0)">id</span><span style="color: rgb(0,0,255)">></span><span style="color: rgb(0,0,0)"><br />       </span><span style="color: rgb(0,0,255)"><</span><span style="color: rgb(128,0,0)">property </span><span style="color: rgb(255,0,0)">name</span><span style="color: rgb(0,0,255)">="date"</span><span style="color: rgb(255,0,0)"> column</span><span style="color: rgb(0,0,255)">="events_date"</span><span style="color: rgb(255,0,0)"> type</span><span style="color: rgb(0,0,255)">="timestamp"</span><span style="color: rgb(0,0,255)">/></span><span style="color: rgb(0,0,0)"><br />       </span><span style="color: rgb(0,0,255)"><</span><span style="color: rgb(128,0,0)">property </span><span style="color: rgb(255,0,0)">name</span><span style="color: rgb(0,0,255)">="title"</span><span style="color: rgb(255,0,0)"> column</span><span style="color: rgb(0,0,255)">="events_title"</span><span style="color: rgb(0,0,255)">/></span><span style="color: rgb(0,0,0)"><br />       </span><span style="color: rgb(0,0,255)"><</span><span style="color: rgb(128,0,0)">set </span><span style="color: rgb(255,0,0)">name</span><span style="color: rgb(0,0,255)">="participants"</span><span style="color: rgb(255,0,0)"> table</span><span style="color: rgb(0,0,255)">="person_event"</span><span style="color: rgb(255,0,0)"> inverse</span><span style="color: rgb(0,0,255)">="true"</span><span style="color: rgb(0,0,255)">></span><span style="color: rgb(0,0,0)"><br />           </span><span style="color: rgb(0,0,255)"><</span><span style="color: rgb(128,0,0)">key </span><span style="color: rgb(255,0,0)">column</span><span style="color: rgb(0,0,255)">="event_id"</span><span style="color: rgb(0,0,255)">/></span><span style="color: rgb(0,0,0)"><br />           </span><span style="color: rgb(0,0,255)"><</span><span style="color: rgb(128,0,0)">many-to-many </span><span style="color: rgb(255,0,0)">column</span><span style="color: rgb(0,0,255)">="person_id"</span><span style="color: rgb(255,0,0)"> class</span><span style="color: rgb(0,0,255)">="events.Person"</span><span style="color: rgb(0,0,255)">/></span><span style="color: rgb(0,0,0)"><br />       </span><span style="color: rgb(0,0,255)"></</span><span style="color: rgb(128,0,0)">set</span><span style="color: rgb(0,0,255)">></span><span style="color: rgb(0,0,0)"><br />   </span><span style="color: rgb(0,0,255)"></</span><span style="color: rgb(128,0,0)">class</span><span style="color: rgb(0,0,255)">></span><span style="color: rgb(0,0,0)"><br /> </span><span style="color: rgb(0,0,255)"></</span><span style="color: rgb(128,0,0)">hibernate-mapping</span><span style="color: rgb(0,0,255)">></span></div> <br /> inverse=true的含? 由双向关联另一方维护该兌,己方不维护该兌(只能q行查询操作)。在上述代码中,由Person方维护该<many-to-many>关系Q示例代码如?以向Person参与的Event中加入新的EventZ)Q?br /> <div style="border-right: rgb(204,204,204) 1px solid; padding-right: 5px; border-top: rgb(204,204,204) 1px solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left: rgb(204,204,204) 1px solid; width: 98%; padding-top: 4px; border-bottom: rgb(204,204,204) 1px solid; background-color: rgb(238,238,238)"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: rgb(0,0,0)">        Session session </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> HibernateUtil.getSessionFactory().getCurrentSession();<br />         session.beginTransaction();<br />         Person p </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> (Person) session.load(Person.</span><span style="color: rgb(0,0,255)">class</span><span style="color: rgb(0,0,0)">, personId);<br />         Event e </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> (Event) session.load(Event.</span><span style="color: rgb(0,0,255)">class</span><span style="color: rgb(0,0,0)">, eventId);<br />        <strong><font color="#0000ff"> p.getEvents().add(e);</font></strong>//执行该代码时Qhibernate会向中间?person_event中插入person_id和event_id记录Q如果换?font color="#a52a2a"><strong>e.getParticipants().add(p)</strong></font>的话Q该代码不会被执行Q即hibernate不会向表person_event中插入记录?br />         session.getTransaction().commit();</span></div> <p><br /> 要注意的一点:在双向关联的关系中,映射的column(和table)的D一?卌用相同的表名和列?Q不然设|ؓinverse="true"的这方将失去q个双向关系Q而变成了一个单向关联?br /> </p> <p>二、Inverse和Cascade的比?/p> <p>InverseQ负责控制关p,默认为falseQ也是关系的两端都能控Ӟ但这样会造成一些问题,更新的时候会因ؓ两端都控制关p,于是重复更新。一般来说有一端要设ؓtrue?br /> CascadeQ负责控制关联对象的U联操作Q包括更新、删除等Q也是说对一个对象进行更新、删除时Q其它对象也受媄响,比如我删除一个对象,那么跟它是多对一关系的对象也全部被删除?br /> 举例说明区别Q删?#8220;一”那一端一个对象O的时候,如果“?#8221;的那一端的Inverse设ؓtrueQ则?#8220;?#8221;的那一端所有与O相关联的对象外键清空Q如?#8220;?#8221;的那一端的Cascade设ؓDeleteQ则?#8220;?#8221;的那一端所有与O相关联的对象全部删除?/p> <img src ="http://www.aygfsteel.com/huyi0616/aggbug/306563.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/huyi0616/" target="_blank">一杯清?/a> 2009-12-18 15:17 <a href="http://www.aygfsteel.com/huyi0616/archive/2009/12/18/306563.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hibernate的检索策略小l??http://www.aygfsteel.com/huyi0616/archive/2008/12/17/246878.html一杯清?/dc:creator>一杯清?/author>Wed, 17 Dec 2008 07:24:00 GMThttp://www.aygfsteel.com/huyi0616/archive/2008/12/17/246878.htmlhttp://www.aygfsteel.com/huyi0616/comments/246878.htmlhttp://www.aygfsteel.com/huyi0616/archive/2008/12/17/246878.html#Feedback0http://www.aygfsteel.com/huyi0616/comments/commentRss/246878.htmlhttp://www.aygfsteel.com/huyi0616/services/trackbacks/246878.html        cȝ别检索策略有立即索和延迟索,默认的检索策略是立即索。在Hibernate映射文g中,通过?font face="文鼎PLl上宋Uni, serif"><class>上配|?font face="文鼎PLl上宋Uni, serif">lazy属性来定索策略。对?font face="文鼎PLl上宋Uni, serif">Session的检索方式,cȝ别检索策略仅适用?font face="文鼎PLl上宋Uni, serif">loadҎQ也pQ对?font face="文鼎PLl上宋Uni, serif">get?font face="文鼎PLl上宋Uni, serif">qurey索,持久化对象都会被立即加蝲而不?font face="文鼎PLl上宋Uni, serif">lazy?font face="文鼎PLl上宋Uni, serif">falseq是true。一般来_我们索对象就是要讉K它,因此立即索是通常的选择。由?font face="文鼎PLl上宋Uni, serif">loadҎ在检索不到对象时会抛出异常(立即索的情况下)Q因此我个hq不使用load索;而由?font face="文鼎PLl上宋Uni, serif"><class>中的lazy属性还影响到多对一及一对一的检索策略,因此使用loadҎ更没必要了?/p>

        兌U别索策略有立即索、gq检索和q切左外q接索。对于关联别检索,又可分ؓ一对多和多对多、多对一和一对一两种情况讨论?/p>

        一对多和多对多兌关系一般?font face="文鼎PLl上宋Uni, serif"><set>配置?font face="文鼎PLl上宋Uni, serif"><set>?font face="文鼎PLl上宋Uni, serif">lazy?font face="文鼎PLl上宋Uni, serif">outer-join属性,它们的不同取值绝对了索策略?/p>

        1Q立x索:q是一对多默认的检索策略,此时lazy=falseQ?font face="文鼎PLl上宋Uni, serif">outer-join=false。尽这是默认的索策略,但如果关联的集合是无用的Q那么就不要使用q种索方式?/p>

        2Qgq检索:此时lazy=trueQ?font face="文鼎PLl上宋Uni, serif">outer-join=falseQ?font face="文鼎PLl上宋Uni, serif">outer-join=true是无意义的)Q这是优先考虑的检索方式?/p>

        3Q迫切左外连接检索:此时 lazy=falseQ?font face="文鼎PLl上宋Uni, serif">outer-join=trueQ这U检索策略只适用于依?font face="文鼎PLl上宋Uni, serif">id索方式(load?font face="文鼎PLl上宋Uni, serif">getQ,而不适用?font face="文鼎PLl上宋Uni, serif">query的集合检索(它会采用立即索策略)。相比于立即索,q种索策略减了一?font face="文鼎PLl上宋Uni, serif">sql语句Q但?font face="文鼎PLl上宋Uni, serif">Hibernate中,只能有一?font face="文鼎PLl上宋Uni, serif"><set>配置?outer-join=true?/p>

        多对一和一对一索策略一般?font face="文鼎PLl上宋Uni, serif"><many-to-one>?font face="文鼎PLl上宋Uni, serif"><one-to-one>配置?font face="文鼎PLl上宋Uni, serif"><many-to-one>中需要配|的属性是 outer-joinQ同时还需要配|?font face="文鼎PLl上宋Uni, serif">one端关联的<class>?font face="文鼎PLl上宋Uni, serif">lazy属性(配置的可不是<many-to-one>中的lazy哦)Q它们的l合后的索策略如下:

        1Q?outer-join=autoQ这是默认|如果lazy=true为gq检索,如果lazy=false切左外连接检索?/p>

        2Q?outer-join=trueQ无关于lazyQ都切左外连接检索?/p>

        3Q?outer-join=falseQ如?font face="文鼎PLl上宋Uni, serif">lazy=true为gq检索,否则为立x索?/p>

        可以看到Q在默认的情况下Q?font face="文鼎PLl上宋Uni, serif">outer-join=autoQ?font face="文鼎PLl上宋Uni, serif">lazy=falseQ,对关联的one端对?font face="文鼎PLl上宋Uni, serif">Hibernate采用的迫切左外连接检索。依我看Q很多情况下Q我们ƈ不需要加?font face="文鼎PLl上宋Uni, serif">one端关联的对象Q很可能我们需要的仅仅是关联对象的idQ;另外Q如果关联对象也采用了迫切左外连接检索,׃出现select语句中有多个外连接表Q如果个数多的话会媄响检索性能Q这也是Z?font face="文鼎PLl上宋Uni, serif">Hibernate通过hibernate.max_fetch_depth属性来控制外连接的深度。对于迫切左外连接检索,query的集合检索ƈ不适用Q它会采用立x索策略?/p>

        对于索策略,需要根据实际情况进行选择。对于立x索和延迟索,它们的优点在?font face="文鼎PLl上宋Uni, serif">select语句单(每张表一条语句)、查询速度快,~点在于兌表时需要多?font face="文鼎PLl上宋Uni, serif">select语句Q增加了讉K数据库的频率。因此在选择x索和延迟索时Q可以考虑使用扚w索策略来减少select语句的数量(配置batch-size属性)。对于切左外q接索,优点在于select较少Q但~点?font face="文鼎PLl上宋Uni, serif">select语句的复杂度提高Q多表之间的兌会是很耗时的操作。另外,配置文g是死的,但程序是zȝQ可以根据需要在E序里显C的指定索策略(可能l常需要在E序中显C指定迫切左外连接检索)。ؓ了清楚检索策略的配置效果如何Q可以配|?font face="文鼎PLl上宋Uni, serif">show_sql属性查看程序运行时Hibernate执行?font face="文鼎PLl上宋Uni, serif">sql语句?/p>

       

        注:本文是《精?font face="文鼎PLl上宋Uni, serif">Hibernate》(孙卫琴著Q的温习W记Q关?font face="文鼎PLl上宋Uni, serif">Hibernate的检索策略的详细内容Q请参考原书?/p>

]]>
使用dom4jdxml文档Qƈ修改文档http://www.aygfsteel.com/huyi0616/archive/2008/04/23/195105.html一杯清?/dc:creator>一杯清?/author>Wed, 23 Apr 2008 07:06:00 GMThttp://www.aygfsteel.com/huyi0616/archive/2008/04/23/195105.html

public void prot() throws Exception{   
            try {
                String path=FileLocator.toFileURL(Platform.getBundle(AppConn.APPLICATION_ID).getEntry("")).getPath()
                                       .toString()+"src/hibernate.cfg.xml";
                File cfgFile=new File(path);
                SAXReader reader=new SAXReader();           
                reader.setEntityResolver(new EntityResolver()      
                {      
                    public InputSource resolveEntity(String publicId,      
                            String systemId)      
                    {      
                        if (publicId.equals("-//Hibernate/Hibernate Configuration DTD 3.0//EN"))      
                        {      
                            InputStream in = getClass().getResourceAsStream(      
                                    "/hibernate-configuration-3.0.dtd");      
                            return new InputSource(in);      
                        }      
                        return null;      
                    }      
                });            
                Document document=null;
                try {
                    document = reader.read(cfgFile);
                } catch (DocumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Element root=document.getRootElement();
                List list=root.selectNodes("http://property");
                Iterator it=list.iterator();
                while(it.hasNext()){
                    Element nextelement=(Element)it.next();
                    String  text=nextelement.attributeValue("name");
                    if(text.equals("connection.username")){
                            nextelement.setText("");                                                               
                    }
                    if(text.equals("connection.password")){                       
                            nextelement.setText("");                   
                    }
                    if(text.equals("connection.url")){
                            nextelement.setText("");                                       
                    }               
                }               
                    XMLWriter output=new XMLWriter(new FileWriter(cfgFile),OutputFormat.createPrettyPrint());
                    output.write(document);
                    output.flush();
                    output.close();
           
            } catch (HibernateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }    
           
        }


]]>
վ֩ģ壺 ɽ| | ̨| ̫| | ư| ӽ| | | | | »| | ѳ| | ̫| | »| | | ƽ| | Ͽ| ˻| | Դ| | | | ׼| | | ɫ| | | ɽ| ƽ| ¡| ʹ| | ¤|