Hibernate4 更新部分更改的字段 hibernate update

Hibernate 中如果直接使用

<wbr></wbr>

Session.update(Object o);

<wbr>注意:很多人用Sesion.get()/load()查到某个对象后 用<span style="word-wrap:normal; word-break:normal">Session.update()</span>,发现自己想更新的字段更新了,但是其他的字段却变成了空,为什么呢? 原因是<span style="word-wrap:normal; word-break:normal">Sesion.get()/load()和</span><span style="word-wrap:normal; word-break:normal">Session.update()用了不同的session.</span></wbr>

会把这个表中的所有字段更新一遍。

<wbr></wbr>

比如:

<wbr></wbr>

[java]<wbr><a target="_blank" href="http://blog.csdn.net/Baykermr/article/details/5359913#" title="view plain" style="text-decoration:none; color:rgb(62,115,160)">view plain</a><a target="_blank" href="http://blog.csdn.net/Baykermr/article/details/5359913#" title="copy" style="text-decoration:none; color:rgb(62,115,160)">copy</a> <div></div> </wbr>
  1. public<wbr></wbr>class<wbr>TeacherTest<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  2. <wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:rgb(100,100,100); background-color:inherit">@Test</span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; background-color:inherit"><wbr><wbr></wbr></wbr></span></wbr></wbr></wbr></wbr>
  3. <wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:rgb(0,102,153); background-color:inherit"><strong>public</strong></span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; background-color:inherit"><wbr></wbr></span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:rgb(0,102,153); background-color:inherit"><strong>void</strong></span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; background-color:inherit"><wbr>update(){<wbr><wbr></wbr></wbr></wbr></span></wbr></wbr></wbr></wbr>
  4. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>Session<wbr>session<wbr>=<wbr><wbr>HibernateUitl.getSessionFactory().getCurrentSession();<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  5. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>session.beginTransaction();<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  6. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>Teacher<wbr>t<wbr>=<wbr>(Teacher)<wbr>session.get(Teacher.<span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:rgb(0,102,153); background-color:inherit"><strong>class</strong></span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; background-color:inherit">,<wbr></wbr></span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:rgb(192,0,0); background-color:inherit">3</span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; background-color:inherit">);<wbr><wbr></wbr></wbr></span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  7. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>t.setName(<span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"yangtb2"</span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; background-color:inherit">);<wbr><wbr></wbr></wbr></span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  8. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>session.update(t);<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  9. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  10. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>session.getTransaction().commit();<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  11. <wbr><wbr><wbr><wbr>}<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  12. }<wbr><wbr></wbr></wbr>

<wbr></wbr>

Hibernate 执行的SQL语句:

<wbr></wbr>

<wbr></wbr>

[xhtml]<wbr><a target="_blank" href="http://blog.csdn.net/Baykermr/article/details/5359913#" title="view plain" style="text-decoration:none; color:rgb(62,115,160)">view plain</a><a target="_blank" href="http://blog.csdn.net/Baykermr/article/details/5359913#" title="copy" style="text-decoration:none; color:rgb(62,115,160)">copy</a> <div></div> </wbr>
  1. Hibernate:<wbr><wbr><wbr></wbr></wbr></wbr>
  2. <wbr><wbr><wbr><wbr>update<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  3. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>Teacher<wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  4. <wbr><wbr><wbr><wbr>set<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  5. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:red; background-color:inherit">age</span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; background-color:inherit">=?,<wbr><wbr></wbr></wbr></span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  6. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:red; background-color:inherit">birthday</span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; background-color:inherit">=?,<wbr><wbr></wbr></wbr></span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  7. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:red; background-color:inherit">name</span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; background-color:inherit">=?,<wbr><wbr></wbr></wbr></span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  8. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:red; background-color:inherit">title</span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; background-color:inherit">=?<wbr><wbr><wbr></wbr></wbr></wbr></span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  9. <wbr><wbr><wbr><wbr>where<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  10. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:red; background-color:inherit">id</span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; background-color:inherit">=?<wbr><wbr></wbr></wbr></span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr></wbr>

我们只更改了Name属性,而Hibernate 的sql语句 把所有字段都更改了一次。

<wbr></wbr>

这样要是我们有字段是文本类型,这个类型存储的内容是几千,几万字,这样效率会很低。

那么怎么只更改我们更新的字段呢?

<wbr></wbr>

有三中方法:

<wbr>1.XML中设置property 标签 update = "false" ,如下:我们设置 age 这个属性在更改中不做更改</wbr>

<wbr></wbr>

[xhtml]<wbr><a target="_blank" href="http://blog.csdn.net/Baykermr/article/details/5359913#" title="view plain" style="text-decoration:none; color:rgb(62,115,160)">view plain</a><a target="_blank" href="http://blog.csdn.net/Baykermr/article/details/5359913#" title="copy" style="text-decoration:none; color:rgb(62,115,160)">copy</a> <div></div> </wbr>
  1. <</span>property<wbr></wbr>name="age"<wbr></wbr>update="false"></</span>property><wbr><wbr></wbr></wbr>
<wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(188,211,229)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(188,211,229)"></span> <p style="margin-top:0px; margin-bottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:26px; font-size:14.399999618530273px; color:rgb(51,51,51); font-family:Arial"> <wbr></wbr></p> <p style="margin-top:0px; margin-bottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:26px; font-size:14.399999618530273px; color:rgb(51,51,51); font-family:Arial"> <wbr>在Annotation中<wbr>在属性GET方法上加上@Column(updatable=false)</wbr></wbr></p> <p style="margin-top:0px; margin-bottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:26px; font-size:14.399999618530273px; color:rgb(51,51,51); font-family:Arial"> <wbr></wbr></p> <div style="color:rgb(70,70,70); line-height:21.600000381469727px; font-family:Consolas,'Courier new',Courier,mono,serif; background-color:rgb(231,229,220); width:687.0499877929688px; overflow:auto; padding-top:1px; margin:18px 0px!important"> <div style="padding-left:45px"> <div style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; background-color:rgb(248,248,248); border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108)"> <strong>[java]</strong><wbr><a target="_blank" href="http://blog.csdn.net/Baykermr/article/details/5359913#" title="view plain" style="text-decoration:none; color:rgb(62,115,160)">view plain</a><a target="_blank" href="http://blog.csdn.net/Baykermr/article/details/5359913#" title="copy" style="text-decoration:none; color:rgb(62,115,160)">copy</a> <div></div> </wbr> </div> </div> <ol start="1" style="margin:0px 0px 1px 45px!important; padding:0px; border:none; list-style:none; background-color:rgb(255,255,255); color:rgb(92,92,92)"> <li style="margin:0px!important; padding:0px 3px 0px 10px!important; border-width:0px 0px 0px 3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit"> <span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:rgb(100,100,100); background-color:inherit">@Column</span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; background-color:inherit">(updatable=</span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:rgb(0,102,153); background-color:inherit"><strong>false</strong></span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; background-color:inherit">)<wbr><wbr></wbr></wbr></span></span> </li> <li style="margin:0px!important; padding:0px 3px 0px 10px!important; border-width:0px 0px 0px 3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; background-color:rgb(248,248,248)"> <span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:black; background-color:inherit"><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:rgb(0,102,153); background-color:inherit"><strong>public</strong></span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; background-color:inherit"><wbr></wbr></span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:rgb(0,102,153); background-color:inherit"><strong>int</strong></span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; background-color:inherit"><wbr>getAge()<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr></span></wbr></wbr></wbr></wbr></span> </li> <li style="margin:0px!important; padding:0px 3px 0px 10px!important; border-width:0px 0px 0px 3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit"> <span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:black; background-color:inherit"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:rgb(0,102,153); background-color:inherit"><strong>return</strong></span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; background-color:inherit"><wbr>age;<wbr><wbr></wbr></wbr></wbr></span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span> </li> <li style="margin:0px!important; padding:0px 3px 0px 10px!important; border-width:0px 0px 0px 3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; background-color:rgb(248,248,248)"> <span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:black; background-color:inherit"><wbr><wbr><wbr><wbr>}<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></span> </li> </ol> </div> <p style="margin-top:0px; margin-bottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:26px; font-size:14.399999618530273px; color:rgb(51,51,51); font-family:Arial"> <wbr></wbr></p> <p style="margin-top:0px; margin-bottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:26px; font-size:14.399999618530273px; color:rgb(51,51,51); font-family:Arial"> <wbr></wbr></p> <p style="margin-top:0px; margin-bottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:26px; font-size:14.399999618530273px; color:rgb(51,51,51); font-family:Arial"> 我们在执行 Update方法会发现,age 属性 不会被更改</p> <p style="margin-top:0px; margin-bottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:26px; font-size:14.399999618530273px; color:rgb(51,51,51); font-family:Arial"> <wbr></wbr></p> <p style="margin-top:0px; margin-bottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:26px; font-size:14.399999618530273px; color:rgb(51,51,51); font-family:Arial"> <wbr></wbr></p> <div style="color:rgb(70,70,70); line-height:21.600000381469727px; font-family:Consolas,'Courier new',Courier,mono,serif; background-color:rgb(231,229,220); width:687.0499877929688px; overflow:auto; padding-top:1px; margin:18px 0px!important"> <div style="padding-left:45px"> <div style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; background-color:rgb(248,248,248); border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108)"> <strong>[java]</strong><wbr><a target="_blank" href="http://blog.csdn.net/Baykermr/article/details/5359913#" title="view plain" style="text-decoration:none; color:rgb(62,115,160)">view plain</a><a target="_blank" href="http://blog.csdn.net/Baykermr/article/details/5359913#" title="copy" style="text-decoration:none; color:rgb(62,115,160)">copy</a> <div></div> </wbr> </div> </div> <ol start="1" style="margin:0px 0px 1px 45px!important; padding:0px; border:none; list-style:none; background-color:rgb(255,255,255); color:rgb(92,92,92)"> <li style="margin:0px!important; padding:0px 3px 0px 10px!important; border-width:0px 0px 0px 3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit"> <span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:black; background-color:inherit">Hibernate:<wbr><wbr><wbr></wbr></wbr></wbr></span> </li> <li style="margin:0px!important; padding:0px 3px 0px 10px!important; border-width:0px 0px 0px 3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; background-color:rgb(248,248,248)"> <span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:black; background-color:inherit"><wbr><wbr><wbr><wbr>update<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></span> </li> <li style="margin:0px!important; padding:0px 3px 0px 10px!important; border-width:0px 0px 0px 3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit"> <span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:black; background-color:inherit"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>Teacher<wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span> </li> <li style="margin:0px!important; padding:0px 3px 0px 10px!important; border-width:0px 0px 0px 3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; background-color:rgb(248,248,248)"> <span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:black; background-color:inherit"><wbr><wbr><wbr><wbr>set<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></span> </li> <li style="margin:0px!important; padding:0px 3px 0px 10px!important; border-width:0px 0px 0px 3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit"> <span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:black; background-color:inherit"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>birthday=?,<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span> </li> <li style="margin:0px!important; padding:0px 3px 0px 10px!important; border-width:0px 0px 0px 3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; background-color:rgb(248,248,248)"> <span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:black; background-color:inherit"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>name=?,<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span> </li> <li style="margin:0px!important; padding:0px 3px 0px 10px!important; border-width:0px 0px 0px 3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit"> <span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:black; background-color:inherit"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>title=?<wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span> </li> <li style="margin:0px!important; padding:0px 3px 0px 10px!important; border-width:0px 0px 0px 3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; background-color:rgb(248,248,248)"> <span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:black; background-color:inherit"><wbr><wbr><wbr><wbr>where<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></span> </li> <li style="margin:0px!important; padding:0px 3px 0px 10px!important; border-width:0px 0px 0px 3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit"> <span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:black; background-color:inherit"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>id=?<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span> </li> </ol> </div> <p style="margin-top:0px; margin-bottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:26px; font-size:14.399999618530273px; color:rgb(51,51,51); font-family:Arial"> <wbr></wbr></p> <p style="margin-top:0px; margin-bottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:26px; font-size:14.399999618530273px; color:rgb(51,51,51); font-family:Arial"> <span style="word-wrap:normal; word-break:normal; line-height:21.600000381469727px; color:rgb(255,0,0)">缺点:不灵活····</span></p> <p style="margin-top:0px; margin-bottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:26px; font-size:14.399999618530273px; color:rgb(51,51,51); font-family:Arial"> <wbr></wbr></p> <p style="margin-top:0px; margin-bottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:26px; font-size:14.399999618530273px; color:rgb(51,51,51); font-family:Arial"> 2.第2种方法··使用XML中的<wbr><span style="word-wrap:normal; word-break:normal; line-height:21.600000381469727px; color:rgb(255,0,0)">dynamic-update="true"</span></wbr></p> <p style="margin-top:0px; margin-bottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:26px; font-size:14.399999618530273px; color:rgb(51,51,51); font-family:Arial"> <wbr></wbr></p> <p style="margin-top:0px; margin-bottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:26px; font-size:14.399999618530273px; color:rgb(51,51,51); font-family:Arial"> <wbr></wbr></p> <div style="color:rgb(70,70,70); line-height:21.600000381469727px; font-family:Consolas,'Courier new',Courier,mono,serif; background-color:rgb(231,229,220); width:687.0499877929688px; overflow:auto; padding-top:1px; margin:18px 0px!important"> <div style="padding-left:45px"> <div style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; background-color:rgb(248,248,248); border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108)"> <strong>[xhtml]</strong><wbr><a target="_blank" href="http://blog.csdn.net/Baykermr/article/details/5359913#" title="view plain" style="text-decoration:none; color:rgb(62,115,160)">view plain</a><a target="_blank" href="http://blog.csdn.net/Baykermr/article/details/5359913#" title="copy" style="text-decoration:none; color:rgb(62,115,160)">copy</a> <div></div> </wbr> </div> </div> <ol start="1" style="margin:0px 0px 1px 45px!important; padding:0px; border:none; list-style:none; background-color:rgb(255,255,255); color:rgb(92,92,92)"><li style="margin:0px!important; padding:0px 3px 0px 10px!important; border-width:0px 0px 0px 3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit"> <span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:rgb(153,51,0); background-color:inherit"><strong>&lt;&lt;/span&gt;<span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; background-color:inherit">class</span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; background-color:inherit"><wbr></wbr></span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:red; background-color:inherit">name</span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; background-color:inherit">=</span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"com.sccin.entity.Student"</span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; background-color:inherit"><wbr><wbr></wbr></wbr></span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:red; background-color:inherit">table</span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; background-color:inherit">=</span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"student"</span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; background-color:inherit"><wbr></wbr></span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:red; background-color:inherit">dynamic-update</span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; background-color:inherit">=</span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"true"</span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; background-color:inherit">&gt;</span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; background-color:inherit"><wbr><wbr></wbr></wbr></span></strong></span></span> </li></ol> </div> <p style="margin-top:0px; margin-bottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:26px; font-size:14.399999618530273px; color:rgb(51,51,51); font-family:Arial"> <wbr></wbr></p> <p style="margin-top:0px; margin-bottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:26px; font-size:14.399999618530273px; color:rgb(51,51,51); font-family:Arial"> <wbr></wbr></p> <p style="margin-top:0px; margin-bottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:26px; font-size:14.399999618530273px; color:rgb(51,51,51); font-family:Arial"> <span style="word-wrap:normal; word-break:normal; line-height:21.600000381469727px; color:rgb(255,0,0)">OK,这样就不需要在字段上设置了。</span></p> <p style="margin-top:0px; margin-bottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:26px; font-size:14.399999618530273px; color:rgb(51,51,51); font-family:Arial"> <span style="word-wrap:normal; word-break:normal; line-height:21.600000381469727px; color:rgb(255,0,0)">但这样的方法在Annotation中没有</span></p> <p style="margin-top:0px; margin-bottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:26px; font-size:14.399999618530273px; color:rgb(51,51,51); font-family:Arial"> <wbr></wbr></p> <p style="margin-top:0px; margin-bottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:26px; font-size:14.399999618530273px; color:rgb(51,51,51); font-family:Arial"> 3.第三种方式:使用HQL语句(灵活,方便)</p> <p style="margin-top:0px; margin-bottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:26px; font-size:14.399999618530273px; color:rgb(51,51,51); font-family:Arial"> <wbr></wbr></p> <p style="margin-top:0px; margin-bottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:26px; font-size:14.399999618530273px; color:rgb(51,51,51); font-family:Arial"> 使用HQL语句修改数据</p> <p style="margin-top:0px; margin-bottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:26px; font-size:14.399999618530273px; color:rgb(51,51,51); font-family:Arial"> <wbr></wbr></p> <p style="margin-top:0px; margin-bottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:26px; font-size:14.399999618530273px; color:rgb(51,51,51); font-family:Arial"> <wbr></wbr></p> <div style="color:rgb(70,70,70); line-height:21.600000381469727px; font-family:Consolas,'Courier new',Courier,mono,serif; background-color:rgb(231,229,220); width:687.0499877929688px; overflow:auto; padding-top:1px; margin:18px 0px!important"> <div style="padding-left:45px"> <div style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; background-color:rgb(248,248,248); border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108)"> <strong>[java]</strong><wbr><a target="_blank" href="http://blog.csdn.net/Baykermr/article/details/5359913#" title="view plain" style="text-decoration:none; color:rgb(62,115,160)">view plain</a><a target="_blank" href="http://blog.csdn.net/Baykermr/article/details/5359913#" title="copy" style="text-decoration:none; color:rgb(62,115,160)">copy</a> <div></div> </wbr> </div> </div> <ol start="1" style="margin:0px 0px 1px 45px!important; padding:0px; border:none; list-style:none; background-color:rgb(255,255,255); color:rgb(92,92,92)"> <li style="margin:0px!important; padding:0px 3px 0px 10px!important; border-width:0px 0px 0px 3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit"> <span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:rgb(0,102,153); background-color:inherit"><strong>public</strong></span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; background-color:inherit"><wbr></wbr></span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:rgb(0,102,153); background-color:inherit"><strong>void</strong></span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; background-color:inherit"><wbr>update(){<wbr><wbr></wbr></wbr></wbr></span></span> </li> <li style="margin:0px!important; padding:0px 3px 0px 10px!important; border-width:0px 0px 0px 3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; background-color:rgb(248,248,248)"> <span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:black; background-color:inherit"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>Session<wbr>session<wbr>=<wbr><wbr>HibernateUitl.getSessionFactory().getCurrentSession();<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span> </li> <li style="margin:0px!important; padding:0px 3px 0px 10px!important; border-width:0px 0px 0px 3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit"> <span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:black; background-color:inherit"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>session.beginTransaction();<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span> </li> <li style="margin:0px!important; padding:0px 3px 0px 10px!important; border-width:0px 0px 0px 3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; background-color:rgb(248,248,248)"> <span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:black; background-color:inherit"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>Query<wbr>query<wbr>=<wbr>session.createQuery(<span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"update<wbr>Teacher<wbr>t<wbr>set<wbr>t.name<wbr>=<wbr>'yangtianb'<wbr>where<wbr>id<wbr>=<wbr>3"</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; background-color:inherit">);<wbr><wbr></wbr></wbr></span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span> </li> <li style="margin:0px!important; padding:0px 3px 0px 10px!important; border-width:0px 0px 0px 3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit"> <span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:black; background-color:inherit"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>query.executeUpdate();<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span> </li> <li style="margin:0px!important; padding:0px 3px 0px 10px!important; border-width:0px 0px 0px 3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; background-color:rgb(248,248,248)"> <span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:black; background-color:inherit"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>session.getTransaction().commit();<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span> </li> <li style="margin:0px!important; padding:0px 3px 0px 10px!important; border-width:0px 0px 0px 3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit"> <span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:black; background-color:inherit"><wbr><wbr><wbr><wbr>}<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></span> </li> </ol> </div> <p style="margin-top:0px; margin-bottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:26px; font-size:14.399999618530273px; color:rgb(51,51,51); font-family:Arial"> <wbr></wbr></p> <p style="margin-top:0px; margin-bottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:26px; font-size:14.399999618530273px; color:rgb(51,51,51); font-family:Arial"> Hibernate 执行的SQL语句:</p> <p style="margin-top:0px; margin-bottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:26px; font-size:14.399999618530273px; color:rgb(51,51,51); font-family:Arial"> <wbr></wbr></p> <p style="margin-top:0px; margin-bottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:26px; font-size:14.399999618530273px; color:rgb(51,51,51); font-family:Arial"> <wbr></wbr></p> <div style="color:rgb(70,70,70); line-height:21.600000381469727px; font-family:Consolas,'Courier new',Courier,mono,serif; background-color:rgb(231,229,220); width:687.0499877929688px; overflow:auto; padding-top:1px; margin:18px 0px!important"> <div style="padding-left:45px"> <div style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; background-color:rgb(248,248,248); border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108)"> <strong>[xhtml]</strong><wbr><a target="_blank" href="http://blog.csdn.net/Baykermr/article/details/5359913#" title="view plain" style="text-decoration:none; color:rgb(62,115,160)">view plain</a><a target="_blank" href="http://blog.csdn.net/Baykermr/article/details/5359913#" title="copy" style="text-decoration:none; color:rgb(62,115,160)">copy</a> <div></div> </wbr> </div> </div> <ol start="1" style="margin:0px 0px 1px 45px!important; padding:0px; border:none; list-style:none; background-color:rgb(255,255,255); color:rgb(92,92,92)"> <li style="margin:0px!important; padding:0px 3px 0px 10px!important; border-width:0px 0px 0px 3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit"> <span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:black; background-color:inherit">Hibernate:<wbr><wbr><wbr></wbr></wbr></wbr></span> </li> <li style="margin:0px!important; padding:0px 3px 0px 10px!important; border-width:0px 0px 0px 3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; background-color:rgb(248,248,248)"> <span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:black; background-color:inherit"><wbr><wbr><wbr><wbr>update<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></span> </li> <li style="margin:0px!important; padding:0px 3px 0px 10px!important; border-width:0px 0px 0px 3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit"> <span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:black; background-color:inherit"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>Teacher<wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span> </li> <li style="margin:0px!important; padding:0px 3px 0px 10px!important; border-width:0px 0px 0px 3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; background-color:rgb(248,248,248)"> <span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:black; background-color:inherit"><wbr><wbr><wbr><wbr>set<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></span> </li> <li style="margin:0px!important; padding:0px 3px 0px 10px!important; border-width:0px 0px 0px 3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit"> <span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:black; background-color:inherit"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:red; background-color:inherit">name</span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; background-color:inherit">=</span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:blue; background-color:inherit">'yangtianb'</span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; background-color:inherit"><wbr><wbr><wbr></wbr></wbr></wbr></span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span> </li> <li style="margin:0px!important; padding:0px 3px 0px 10px!important; border-width:0px 0px 0px 3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; background-color:rgb(248,248,248)"> <span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:black; background-color:inherit"><wbr><wbr><wbr><wbr>where<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></span> </li> <li style="margin:0px!important; padding:0px 3px 0px 10px!important; border-width:0px 0px 0px 3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit"> <span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:black; background-color:inherit"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:red; background-color:inherit">id</span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; background-color:inherit">=</span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; color:blue; background-color:inherit">3</span><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; border:none; background-color:inherit"><wbr><wbr></wbr></wbr></span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span> </li> </ol> </div> <p style="margin-top:0px; margin-bottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:26px; font-size:14.399999618530273px; color:rgb(51,51,51); font-family:Arial"> <wbr></wbr></p> <p style="margin-top:0px; margin-bottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:26px; font-size:14.399999618530273px; color:rgb(51,51,51); font-family:Arial"> 这样就只更新了我们更新的字段·····</p> </wbr>

你可能感兴趣的:(Hibernate4)