ï»??xml version="1.0" encoding="utf-8" standalone="yes"?>91精品国产高清,国产视频精选在线,国产在线视频欧美一区二区三区http://www.aygfsteel.com/AstroQi/category/42391.htmlI'm Astro Qi. If call me, please send email to closoastroqi@126.comzh-cnFri, 22 Apr 2011 08:26:57 GMTFri, 22 Apr 2011 08:26:57 GMT60hibernate集合映射cascadeå’Œinverse详解http://www.aygfsteel.com/AstroQi/archive/2011/04/22/348779.htmlAstro.QiAstro.QiFri, 22 Apr 2011 02:51:00 GMThttp://www.aygfsteel.com/AstroQi/archive/2011/04/22/348779.htmlhttp://www.aygfsteel.com/AstroQi/comments/348779.htmlhttp://www.aygfsteel.com/AstroQi/archive/2011/04/22/348779.html#Feedback0http://www.aygfsteel.com/AstroQi/comments/commentRss/348779.htmlhttp://www.aygfsteel.com/AstroQi/services/trackbacks/348779.html阅读全文

Astro.Qi 2011-04-22 10:51 发表评论
]]>
Hibernate中cascade与inverse属性详è§?/title><link>http://www.aygfsteel.com/AstroQi/archive/2011/04/22/348777.html</link><dc:creator>Astro.Qi</dc:creator><author>Astro.Qi</author><pubDate>Fri, 22 Apr 2011 02:47:00 GMT</pubDate><guid>http://www.aygfsteel.com/AstroQi/archive/2011/04/22/348777.html</guid><wfw:comment>http://www.aygfsteel.com/AstroQi/comments/348777.html</wfw:comment><comments>http://www.aygfsteel.com/AstroQi/archive/2011/04/22/348777.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/AstroQi/comments/commentRss/348777.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/AstroQi/services/trackbacks/348777.html</trackback:ping><description><![CDATA[<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">在学习Hibernate的过½E‹ä¸­æœ€ä¸å¥½ç†è§£çš„就是这两个属性了ã€?br style="line-height: normal; " /> (我当初学习Hibernate的时候,发现¾|‘上介绍˜q™ä¸¤ä¸ªå±žæ€§çš„æ–‡ç« å€’是不少åQŒä½†æ˜¯ï¼Œå±…然有好多都是è{帖。。。还有的ž®±æ˜¯ 照书搬~~-_-!!!)。。ã€?/span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">据个例子åQšä¹¦ä¸Šè¯´inverse=falseæ—Óž¼Œç”׃¸»æŽ§æ–¹¾l´æŒå…³ç³»ã€‚。ã€?br style="line-height: normal; " /> ç”׃ºŽæˆ‘也是初学者。。。再加上语文水åã^偏低。。。不理解“¾l´æŒå…³ç³»æ˜¯å•¥æ„æ€?#8221;å›§~</span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">提示åQ?br style="line-height: normal; " /> (1)如果åQšæ‚¨ä¸äº†è§£Hibernateçš„one-to-many或many-to-one的概å¿üc€?/span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">(2)如果åQšä½ ä¸äº†è§£Hibernateçš?#8220;自由æ€?#8221;“持久æ€?#8221;“游离æ€?#8221;的概å¿üc€?/span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">(3)如果åQšæ‚¨ä¸äº†è§£Hibernate中的“脏数æ?#8221;的概å¿üc€?/span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">(4)如果åQ𿂍坹Hibernate中Session¾~“å­˜åQŒæ²¡æœ‰åˆæ­¥äº†è§£çš„话ã€?br style="line-height: normal; " /> (在Hibernate中调用save˜q›è¡Œå­˜å‚¨æ•°æ®çš„æ—¶å€?òq¶ä¸æ˜¯é©¬ä¸Šå°±å¯ÒŽ•°æ®åº“˜q›è¡Œinsert操作åQŒè€Œæ˜¯ä¼šå°†å…?#8220;数据对象(vo)”¾U›_…¥Hibernateçš„Session¾~“å­˜ã€?</span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">在上面的4条提½CÞZ¸­åQŒå¦‚果您对其中的某一条,不是很清楚的话。希望请先了解有关知识ã€?br style="line-height: normal; " /> 否则åQŒå¯èƒ½æ‚¨ž®?“无法或很éš?#8221;理解 cascade æˆ?inverse ˜q?个属性ã€?/span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">首相åQŒcascade ä¸?inverse ˜q™ä¸¤ä¸ªå±žæ€§ï¼Œå…¶å®žæ˜¯å®Œå…¨ä¸åŒçš„两个东西åQŒæƒ³è¦äº†è§£ä»–们各自的“用途与区别”åQŒè¯¦è§å¦‚下介¾lï¼š</span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">˜q™é‡Œæœ‰ä¸¤ä¸ªè¡¨:</span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">åQ?åQ‰class   (班çñ”è¡?<br style="line-height: normal; " /> 相应字段åQ?br style="line-height: normal; " /> cid    varchar(32) 主键 not-null (班çñ”id)<br style="line-height: normal; " /> cname varchar(16)      not-null (班çñ”名称)   <br style="line-height: normal; " /> <br style="line-height: normal; " /> åQ?åQ‰student (学生è¡?<br style="line-height: normal; " /> 相应字段åQ?br style="line-height: normal; " /> sid    varchar(32) 主键 not-null (学生id)<br style="line-height: normal; " /> sname varchar(16)      not-null (学生姓名)<br style="line-height: normal; " /> class_id varchar(32)   not-null (学生所属班¾U?</span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">一个班¾U?class)对应多个学生(student)åQŒæ‰€ä»¥ç­¾U§è¡¨(class)ž®±æ˜¯“one-to-many”ç«?br style="line-height: normal; " /> 反之studentž®±æ˜¯many-to-one</span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">//--------Class¾cȝš„代码--------<br style="line-height: normal; " /> public class Class implements.....<br style="line-height: normal; " /> {<br style="line-height: normal; " /> private cId = "";<br style="line-height: normal; " /> private cName = "";<br style="line-height: normal; " /> private students = java.util.HashMap();<br style="line-height: normal; " /> // 省略对应çš?geter setter<br style="line-height: normal; " /> }<br style="line-height: normal; " /> //--------Class.hbm.xml--------<br style="line-height: normal; " /> <hibernate-mapping><br style="line-height: normal; " /> <class name="lcx.vo.Class" table="class"<br style="line-height: normal; " />    catalog="demo"><br style="line-height: normal; " />    <id name="cid" type="java.lang.String"><br style="line-height: normal; " />     <column name="cid" length="32" /><br style="line-height: normal; " />     <generator class="uuid.hex" /><br style="line-height: normal; " />    </id><br style="line-height: normal; " />    <property name="name" type="java.lang.String"><br style="line-height: normal; " />     <column name="cname" length="16" not-null="true" /><br style="line-height: normal; " />    </property><br style="line-height: normal; " />   <br style="line-height: normal; " />    <set name="students" table="student" cascade="save-update"><br style="line-height: normal; " />     <key column="class" /><br style="line-height: normal; " />     <one-to-many class="lcx.vo.Student" /><br style="line-height: normal; " />    </set> <br style="line-height: normal; " /> </class><br style="line-height: normal; " /> </hibernate-mapping></span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">//--------Student¾cȝš„代码;*******<br style="line-height: normal; " /> public class Student implements.....<br style="line-height: normal; " /> {<br style="line-height: normal; " /> private sId = "";<br style="line-height: normal; " /> private sName = "";<br style="line-height: normal; " /> private Class class = null;<br style="line-height: normal; " /> // 省略对应çš?geter setter<br style="line-height: normal; " /> }<br style="line-height: normal; " /> // Student.hbm.xml<br style="line-height: normal; " /> <hibernate-mapping><br style="line-height: normal; " /> <class name="lcx.vo.Student" table="student" catalog="demo"><br style="line-height: normal; " />    <id name="cid" type="java.lang.String"><br style="line-height: normal; " />     <column name="sid" length="32" /><br style="line-height: normal; " />     <generator class="uuid.hex" /><br style="line-height: normal; " />    </id><br style="line-height: normal; " />    <many-to-one name="class" <br style="line-height: normal; " />     class="lcx.vo.Class"<br style="line-height: normal; " />     column="class_id" <br style="line-height: normal; " />     not-null="true" <br style="line-height: normal; " />    />   <br style="line-height: normal; " /> </class><br style="line-height: normal; " /> </hibernate-mapping></span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">åQˆä¸€åQ?cascade 的介¾lï¼š<br style="line-height: normal; " /> 当Hibernate持久化一ä¸?#8220;临时对象(也叫自由态对è±?”æ—Óž¼Œåœ¨é»˜è®¤çš„æƒ…况ä¸?卻I¼šæ²¡æœ‰è®„¡½®cascade属性或cascade=noneæ—?åQŒHibernate不会自动“持久化他所兌™””的其他äÍ时对象ã€?/span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">上面˜q™äº›è¯æ˜¯ä»€ä¹ˆæ„æ€å‘¢åQ?什么叫不会自动 “持久åŒ?#8221;å…Œ™”çš„äÍ时对象呢åQ?/span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">看如下代码:</span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">// 创徏一ä¸?临时对象(也叫自由态对è±? <br style="line-height: normal; " /> // 也就是说˜q™ä¸ª class 没有被Hibernate¾U›_…¥Session¾~“å­˜½Ž¡ç†ã€?br style="line-height: normal; " /> Class class = new Class();<br style="line-height: normal; " /> //class.id 䏸™‡ªåŠ¨ç”Ÿæˆ?br style="line-height: normal; " /> class.setName("一òq´çñ”1ç?);</span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">Student stu = new Student();<br style="line-height: normal; " /> //student.id 䏸™‡ªåŠ¨ç”Ÿæˆ?br style="line-height: normal; " /> stu.setName("ž®ç™½å…?);<br style="line-height: normal; " /> stu.setClass(class);</span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">// 关键ž®±æ˜¯˜q™é‡Œã€‚。ã€?br style="line-height: normal; " /> class.getStudents().add(stu);</span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">session.save(class);<br style="line-height: normal; " /> // 提交</span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">// 注意: Class.hbm.xmlæ–‡äšgä¸?cascade="save-update"òq¶ä¸”也没有设¾|®inverse属æ€?也就是说inverse=false;<br style="line-height: normal; " /> // 此时如果你开启了Hibernate的显½CºHQL语句功能åQŒé‚£ä¹ˆæŽ§åˆ¶å°ž®†ä¼šæ˜„¡¤ºå¦‚下3条HQLåQ?/span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">//----------------------------------------********<br style="line-height: normal; " /> insert into demo.class (cid, cname) values (66666666666666666666666666666666, 一òq´çñ”1ç?<br style="line-height: normal; " /> insert into demo.student (sid,sname,class_id) values (8888888888888888811cb2e04c888888, ž®ç™½å…? 66666666666666666666666666666666)<br style="line-height: normal; " /> update demo.student set class_id=66666666666666666666666666666666 where sid=8888888888888888811cb2e04c888888<br style="line-height: normal; " /> //----------------------------------------********</span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">那么ä¸ÞZ»€ä¹ˆä¼šå‡ºçްåQŒè¿™3条HQL语句呢,我们来一一分析一下:</span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">½W?条HQL语句:<br style="line-height: normal; " /> 其实½W¬ä¸€æ¡HQL比较好理解,<br style="line-height: normal; " /> 当我们调ç”?session.save(class) 后,在Hibernate˜q›è¡Œæäº¤çš„æ—¶å€™ï¼Œ<br style="line-height: normal; " /> 会发çŽ?#8220;æœ?#8221;一æ?#8220;æ–?#8221;的数据要插入(insert)åQŒæ‰€ä»¥å°±å¾€class表中,插入了这条新的class记录ã€?/span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">½W?条HQL语句åQ?br style="line-height: normal; " /> 注意问题ž®±åœ¨˜q™é‡ŒåQ?br style="line-height: normal; " /> ˜q™é‡Œä¸ÞZ»€ä¹ˆåˆå‡ºçŽ°äº†ä¸€æ¡insert语句呢?而且˜q˜æ˜¯å‘student表中插入数据ã€?br style="line-height: normal; " /> 我们在上面的代码中,òq¶æ²¡æœ‰ç¼–写类ä¼?#8220;session.save(student)”˜q™æ ·çš„语句啊ã€?br style="line-height: normal; " /> ˜q™æ˜¯ä¸ÞZ»€ä¹ˆå‘¢åQ?br style="line-height: normal; " /> 其实原因åQŒæ˜¯˜q™ä¹ˆå›žäº‹åQšå› ä¸ºæˆ‘们在class端,讄¡½®äº?¾U§è”æ›´æ–°"(å?cascade="save-update")åQ?br style="line-height: normal; " /> 也就是说åQŒå½“Hibernate在向class表中插入“æ–?#8221;对象记录æ—Óž¼Œä¼šæ£€æŸ?#8220;Class对象”所兌™”的属æ€?ž®±æ˜¯<set>对应的属æ€?åQŒæ˜¯å¦å‘生过变化åQŒå¦‚果发生了变化åQŒå°±æŒ‰ç…§“¾U§è”属æ€?cascade)”所讑֮šçš„内å®?br style="line-height: normal; " /> ˜q›è¡Œæ“ä½œã€?/span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">上面讲的˜q™å¥è¯åˆ°åº•是什么意思呢åQ?br style="line-height: normal; " /> 用你ä»?#8220;äº?#8221;话说åQŒå°±æ˜¯ï¼š<br style="line-height: normal; " /> å› äØ“è°ƒç”¨äº?class.getStudents().add(stu);<br style="line-height: normal; " /> 所以,在Hibernate在进行插å…?class对象的时候,发现class对象åQŒæ‰€å…Œ™”的集合中åQŒæœ‰ä¸€æ?br style="line-height: normal; " /> “自由æ€?#8221;çš„å¯¹è±¡ï¼Œè€Œåˆå› äØ“class端设¾|®äº†“¾U§è”属性cascade”åQŒæ‰€ä»¥ï¼Œåœ¨æ’入这æ?“æ–°class对象”æ—Óž¼Œä¹Ÿä¸€åŒæŠŠä»–内部的那些åQŒè¿˜å±žäºŽ“自由æ€?#8221;的其他对象,也一同插入到åQŒä»–们所对应的表中去了ã€?/span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">˜q˜æ˜¯ä¸æ˜Žç™½çš„话。。。可以看看。孙卫琴的《精通Hibernate》,在书上的½W?49™å‰|œ‰ã€?br style="line-height: normal; " /> 但是关于inverse的介¾lã€‚。。写的就有些书面化了åQŒå¦‚果语文不好的话。。。就难懂咯~</span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">½W?条HQL语句åQ?br style="line-height: normal; " /> ½W¬ä¸‰æ¡HQL语句是一条update语句åQŒæ˜¯ä¸æ˜¯è§‰å¾—åQŒå¾ˆèŽ«åå…¶å¦™ã€‚ã€‚ã€‚ã€?br style="line-height: normal; " /> Hibernate大脑˜q›æ°´äº†å§åQŒæ€Žä¹ˆåƒé¥±äº†æ’‘得,重复更新记录啊啊啊啊å•?br style="line-height: normal; " /> 假如åQšæˆ‘ä»¬æŠŠ class端的配置文档中的 invser属性设¾|®äØ“true(卻I¼šinverse=true)<br style="line-height: normal; " /> 在执行上面的½E‹åºåQŒå‘玎ͼŒž®±å˜æˆ?条insert语句啦。。。。ã€?update没啦。。ã€?<br style="line-height: normal; " /> 看来½W¬ä¸‰æ¡çš„update语句和inverse有着密切的关¾p»ï¼ˆä»–两有一腿~åQ‰ã€?/span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">所以我们下边,ž®±æ¥ä»‹ç»ä¸€ä¸‹inverse属æ€?</span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">当调ç”?Class.getStudents().add(stu)æ–ÒŽ³•åQŒè¿›è¡Œæ·»åŠ æ“ä½œæ—¶åQ?nbsp;<br style="line-height: normal; " /> (卻I¼šå?"˜q™ä¸ªClass对象"所属的“集合 (也就是调用getStudentsæ–ÒŽ³•所˜q”回的那个Set集合)”中添加一个Student(å?add(stu))åQŒä¹Ÿž®±æ˜¯è¯ß_¼Œ˜q™ä¸ª“æ–?#8221;æ·ÕdŠ çš„Student对象(stu)åQ?nbsp;<br style="line-height: normal; " /> ä»–çš„Student.class_id字段“å¿…é¡»”åQŒè¦½{‰äºŽ“被添加方Class”的主é”?å?Class.cid)ã€?nbsp;<br style="line-height: normal; " /> ä»?#8220;数据åº?#8221;层面来讲åQŒä¹Ÿž®±æ˜¯è¯ß_¼Œ˜q™ä¸ª“æ–?#8221;æ·ÕdŠ çš?#8220;Student”çš„class_id字段åQŒå¿…™å»è¦ä¸?#8220;Class”çš„cid字段åQŒå­˜åœ?ä¸Õd¤–键关è?ã€?</span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">正因为如此:所以Hibernate“æ€?#8221; 在进è¡?"Class.getStudents().add(stu)" ˜q™æ ·çš„æ“ä½œæ—¶åQ?nbsp;<br style="line-height: normal; " /> 出现意外情况(å¦? stu.getClass=nullåQŒå³åQšstu没有所属班¾U?åQ?br style="line-height: normal; " /> å?#8220;æ·ÕdŠ æ–?#8221;(Student)ä¸?#8220;被添加方”(Class)åQŒå­˜åœ?#8220;外键”不一致的情况发生ã€?nbsp;<br style="line-height: normal; " /> 所以就出现äº?那条多余的update语句。即åQšone-to-manyåQˆClass端)ä¸ÕdЍåŽÈ»´æŠ¤Child.Class_id <br style="line-height: normal; " /> 所以就是说åQŒHibernate怕出错,ž®Þq»™ä½ å¤šæ‰§è¡Œä¸€‹Æ¡æ— ç”¨çš„æ›´æ–°è¯­å¥åQŒä»¥ä¿è¯ add åˆ?Class“集合”中的所有Student<br style="line-height: normal; " /> 都是要与Class有外键关联的ã€?/span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">用普通话说就æ˜?<br style="line-height: normal; " /> 一òq?ç?getStudents().add(ž®ç™½å…?;<br style="line-height: normal; " /> 一òq?ç?getStudents().add(大白å…?;</span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">也就是说现在不管æ˜?ž®ç™½å…?˜q˜æ˜¯ 大白å…?nbsp;<br style="line-height: normal; " /> 如果他们åQŒç›®å‰è¿˜æ²¡æœ‰è‡ªå·±çš„班¾U§çš„话,<br style="line-height: normal; " /> 一òq?班的班主ä»Õd°±ä¼šä¸»åŠ¨é‚€è¯·ä»–ä»¬æˆä¸ÞZ¸€òq?班的同学啦~ã€?/span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">也就是说 一òq?班的班主ä»?ä¸ÕdЍ邀è¯?同学åQŒè€Œä¸æ˜?同学自己来~~~ 所以效率也降低了。。。ã€?/span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">所以我们一般把 一对多ç«?invser讄¡½®ä¸ºtrueåQŒå³åQšä¸è®©ä¸»æŽ§ç«¯åŽÈ»´æŠ¤ä¸»é”®å…³è”,<br style="line-height: normal; " /> åQˆå³åQšè®©åŒå­¦è‡ªå·±åŽÀL‰¾ç­çñ”åQ?br style="line-height: normal; " /> 说白了,ž®±æ˜¯åQŒone-to-many端不用去½Ž¡ç† “新添加对è±?#8221; 的主外键¾U¦æŸé—®é¢˜ã€?/span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">把one-to-many端(å?classç«?çš„invser讄¡½®ä¸ºtrue<br style="line-height: normal; " /> (卻I¼šæ¯æ¬¡å‘class.getStudents˜q™ä¸ªé›†åˆä¸­æ·»åŠ?studentæ—Óž¼Œä¸åŽ»ä¸ÕdЍupdate对应的外é”?åQ?br style="line-height: normal; " /> 而是在student端去手动讄¡½®<br style="line-height: normal; " /> 例如åQ?br style="line-height: normal; " /> student.setClass(class);<br style="line-height: normal; " /> session.save(student);<br style="line-height: normal; " /> ˜q™æ ·æ‰‹åŠ¨è®„¡½® student与classå…Œ™”啦。。。ã€?br style="line-height: normal; " /> 所以上面的½E‹åº“最å¥?#8221;˜q˜æ˜¯å†™æˆ˜q™æ ·åQ?/span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">Class class = new Class();<br style="line-height: normal; " /> class.setName("一òq´çñ”1ç?);<br style="line-height: normal; " /> session.save(class);</span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">Student stu = new Student();<br style="line-height: normal; " /> stu.setName("ž®ç™½å…?);<br style="line-height: normal; " /> stu.setClass(class);<br style="line-height: normal; " /> session.save(class);</span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">/*<br style="line-height: normal; " /> 此时向class集合add内容åQŒä¸ä¼šè¿›è¡Œæ•°æ®åº“操作(update)ã€?br style="line-height: normal; " /> “æ›´æ–°”的只是session¾~“存中,数据镜像ã€?br style="line-height: normal; " /> ˜q™æ ·åšçš„好处是:不仅减少了update语句åQ?br style="line-height: normal; " /> 而且åQŒåŒæ—¶ä¹Ÿæ›´æ–°äº†session¾~“å­˜ã€?br style="line-height: normal; " /> ------------------------<br style="line-height: normal; " /> 而在原来:<br style="line-height: normal; " /> one-to-many端inverse=falseæ—Óž¼Œè™½ç„¶ä¹Ÿæ›´æ–°seesion¾~“存中的class集合åQ?br style="line-height: normal; " /> 但是有却又多余update<br style="line-height: normal; " /> */<br style="line-height: normal; " /> class.getStudents().add(stu);<br style="line-height: normal; " /> // 提交</span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">æ€È»“:<br style="line-height: normal; " /> 当inverse=false òq¶ä¸”向one-to-many端的兌™”集合åQŒæ·»åŠ?#8220;新对è±?å? 自由态对è±?” æ—Óž¼Œ<br style="line-height: normal; " /> Hibernatež®×ƒ¼šè‡ªåЍåQŒåŽ»updateé‚?#8220;个刚刚到来的” “自由态对è±?#8221;的外键ã€?br style="line-height: normal; " /> åQˆå¦‚果你向,one-to-many端添的集合中åQŒadd一ä¸?#8220;已经持久化了的对è±?#8221;åQŒé‚£ž®×ƒ¸ä¼šå‡ºçްupdateäº?å› äØ“å·²ç»æŒä¹…åŒ–è¿‡äº?åQŒé™¤éžï¼Œä½ åŽ» 更改“那个持久化对è±?#8221;所对应的外键。。。那æ ïLš„话。。。呵呵呵~~~<br style="line-height: normal; " /> 你可以试一试,应该不会报错åQŒä½ å¯ä»¥å½“做¾lƒä¹ åŽÕdšä¸€ä¸‹ï¼ŒåŠ æ·±cascadeå’Œinverse˜q™ä¸¤ä¸ªå±žæ€§çš„理解åQ?/span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><br style="line-height: normal; " /> <span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">// 如果看懂了上面的内容。来看一下,下面的东è¥Ñ€?br style="line-height: normal; " /> 假如åQŒå°†one-to-manyç«?å?Classç«?çš?hbm.xml 文档中的cascade¿U»é™¤æŽ?或把cascade="none"ã€?br style="line-height: normal; " /> 那么上面的代码会出现什么情况呢ã€?br style="line-height: normal; " /> ¾l“果会出çŽ?条HQLåQŒå’Œä¸€å †Exception</span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">insert into demo.class (cid, cname) values (66666666666666666666666666666666, 一òq´çñ”1ç?<br style="line-height: normal; " /> update demo.student set class_id=66666666666666666666666666666666 where sid=8888888888888888811cb2e04c888888<br style="line-height: normal; " /> Hibernate Exceptinon......................................</span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">相比较cascade被设¾|?save-update"的时候,¾~ºå°‘äº?æ?insert语句åQŒè€Œä¸”也多了一些Exceptionã€?/span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">那么åQŒåˆ°åº•是ž®‘了å“?条insert语句呢?<br style="line-height: normal; " /> ž®±æ˜¯˜q™æ¡åQ?br style="line-height: normal; " /> insert into demo.student (sid,sname,class_id) values (8888888888888888811cb2e04c888888, ž®ç™½å…? 66666666666666666666666666666666)</span></span></span></span></span></span></p> <p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">之所以会出现åQŒè¿™æ ïLš„现象åQŒæƒ³å¿…您已经早就看出来了ã€?br style="line-height: normal; " /> 因䨓åQŒæˆ‘没有讄¡½®Class端的CascadeåQŒæ‰€ä»¥åœ¨save(class)的时候,òq¶æ²¡æœ‰è‡ªåŠ¨å°†å…¶æ‰€å…Œ™”çš?#8220;自由态对è±?#8221;˜q›è¡ŒæŒä¹…化操作ã€?br style="line-height: normal; " /> 然而,又因ä¸?Class端的inverse=falseåQŒæ‰€ä»¥ï¼ŒClass会自动去¾l´æŒåQŒé‚£ä¸?“新来的student” 的外键ã€?br style="line-height: normal; " /> 所以会出现åQŒæ²¡æœ‰insertž®Þp¦update啦。。。ã€?br style="line-height: normal; " /> 然后在就是Exceptionäº?/span></span></span></span></span></span></p> <img src ="http://www.aygfsteel.com/AstroQi/aggbug/348777.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/AstroQi/" target="_blank">Astro.Qi</a> 2011-04-22 10:47 <a href="http://www.aygfsteel.com/AstroQi/archive/2011/04/22/348777.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hibernate自定义表单完全解å†Ïx–¹æ¡?无需重置SessionFactory)http://www.aygfsteel.com/AstroQi/archive/2009/10/26/299764.htmlAstro.QiAstro.QiMon, 26 Oct 2009 05:16:00 GMThttp://www.aygfsteel.com/AstroQi/archive/2009/10/26/299764.htmlhttp://www.aygfsteel.com/AstroQi/comments/299764.htmlhttp://www.aygfsteel.com/AstroQi/archive/2009/10/26/299764.html#Feedback1http://www.aygfsteel.com/AstroQi/comments/commentRss/299764.htmlhttp://www.aygfsteel.com/AstroQi/services/trackbacks/299764.html阅读全文

Astro.Qi 2009-10-26 13:16 发表评论
]]>
Ö÷Õ¾Ö©Öë³ØÄ£°å£º ¹àÔÆÏØ| »ª³ØÏØ| Ðãɽ| Æ½Ì¶ÏØ| ÐËÒåÊÐ| ÒæÑôÊÐ| Î÷ºÍÏØ| ·ð¸ÔÏØ| ÖÜÄþÏØ| ÓÀ¿µÊÐ| »ªÝöÊÐ| Á¬Æ½ÏØ| ÑôÐÂÏØ| Ì«ºþÏØ| ´ÈϪÊÐ| °¢°Í¸ÂÆì| Á°Ô´ÊÐ| кÍÏØ| ºÍÕþÏØ| ÁèÔ´ÊÐ| ÎߺþÊÐ| ÕòÄþ| ½­Î÷Ê¡| ÊÙ¹âÊÐ| ¸ßÇå| Æô¶«ÊÐ| ¬ÁúÏØ| ÕòÔ­ÏØ| º£ÄþÊÐ| ½ðÏçÏØ| Îä³ÇÏØ| аͶû»¢×óÆì| º¼½õÆì| ºÓ±±Ê¡| µç°×ÏØ| ¶¼ÔÈÊÐ| ¹þÃÜÊÐ| ¹ÅÕÉÏØ| ÈýËëÏØ| ÈêÄÏÏØ| ³Ç¹ÌÏØ|