﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-一点一滴，编程人生-随笔分类-Hibernate框架</title><link>http://www.blogjava.net/writegull/category/51496.html</link><description /><language>zh-cn</language><lastBuildDate>Tue, 24 Apr 2012 01:41:36 GMT</lastBuildDate><pubDate>Tue, 24 Apr 2012 01:41:36 GMT</pubDate><ttl>60</ttl><item><title>hibernate多表查询结果处理</title><link>http://www.blogjava.net/writegull/archive/2012/04/24/376437.html</link><dc:creator>writegull</dc:creator><author>writegull</author><pubDate>Tue, 24 Apr 2012 01:34:00 GMT</pubDate><guid>http://www.blogjava.net/writegull/archive/2012/04/24/376437.html</guid><wfw:comment>http://www.blogjava.net/writegull/comments/376437.html</wfw:comment><comments>http://www.blogjava.net/writegull/archive/2012/04/24/376437.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/writegull/comments/commentRss/376437.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/writegull/services/trackbacks/376437.html</trackback:ping><description><![CDATA[<p style="padding-bottom: 0px; border-right-width: 0px; list-style-type: none; margin-top: 0px; margin-right: 0px; margin-left: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px; word-wrap: normal; word-break: normal; color: #494949; font-family: simsun; text-align: left; background-color: #e2ddc7; ">如果我们在Hibernate中需要查询多个表的不同字段，如何来获取结果呢？<br />有两种方式：<br />1、 对各个字段分别转化成对应类型，如下：</p><div style="color: #494949; font-family: simsun; text-align: left; background-color: #e2ddc7; "><div>Java代码</div><ol style="padding-bottom: 0px; border-right-width: 0px; list-style-type: none; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px; "><li style="padding-bottom: 0px; border-right-width: 0px; list-style-type: decimal; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 30px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px; "><span style="word-wrap: normal; word-break: normal; "><span style="word-wrap: normal; word-break: normal; ">Query&nbsp;<wbr>q&nbsp;<wbr>=&nbsp;<wbr>session.createQuery(</span><span style="word-wrap: normal; word-break: normal; ">"&nbsp;<wbr>select&nbsp;<wbr>members,&nbsp;<wbr>classInfo.className&nbsp;<wbr>"</span><span style="word-wrap: normal; word-break: normal; ">&nbsp;<wbr>+&nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;<wbr></span></span></li><li style="padding-bottom: 0px; border-right-width: 0px; list-style-type: decimal; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 30px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px; "><span style="word-wrap: normal; word-break: normal; ">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="word-wrap: normal; word-break: normal; ">"&nbsp;<wbr>from&nbsp;<wbr>Members&nbsp;<wbr>members,&nbsp;<wbr>ClassInfo&nbsp;<wbr>classInfo&nbsp;<wbr>"</span><span style="word-wrap: normal; word-break: normal; ">&nbsp;<wbr>+&nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;<wbr></span></li><li style="padding-bottom: 0px; border-right-width: 0px; list-style-type: decimal; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 30px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px; "><span style="word-wrap: normal; word-break: normal; ">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="word-wrap: normal; word-break: normal; ">"&nbsp;<wbr>where&nbsp;<wbr>members.level&nbsp;<wbr>=&nbsp;<wbr>classInfo.classCode&nbsp;<wbr>"</span><span style="word-wrap: normal; word-break: normal; ">);&nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;<wbr></span></li><li style="padding-bottom: 0px; border-right-width: 0px; list-style-type: decimal; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 30px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px; "><span style="word-wrap: normal; word-break: normal; ">&nbsp;<wbr>&nbsp;<wbr></span></li><li style="padding-bottom: 0px; border-right-width: 0px; list-style-type: decimal; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 30px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px; "><span style="word-wrap: normal; word-break: normal; ">List&nbsp;<wbr>result&nbsp;<wbr>=&nbsp;<wbr>q.list();&nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;<wbr></span></li><li style="padding-bottom: 0px; border-right-width: 0px; list-style-type: decimal; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 30px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px; "><span style="word-wrap: normal; word-break: normal; ">Iterator&nbsp;<wbr>it&nbsp;<wbr>=&nbsp;<wbr>result.iterator();&nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;<wbr></span></li><li style="padding-bottom: 0px; border-right-width: 0px; list-style-type: decimal; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 30px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px; "><span style="word-wrap: normal; word-break: normal; "><strong>while</strong></span><span style="word-wrap: normal; word-break: normal; ">&nbsp;<wbr>(it.hasNext())&nbsp;<wbr>{&nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;<wbr></span></li><li style="padding-bottom: 0px; border-right-width: 0px; list-style-type: decimal; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 30px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px; "><span style="word-wrap: normal; word-break: normal; ">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>Object[]&nbsp;<wbr>tuple&nbsp;<wbr>=&nbsp;<wbr>(Object[])&nbsp;<wbr>it.next();&nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;<wbr></span></li><li style="padding-bottom: 0px; border-right-width: 0px; list-style-type: decimal; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 30px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px; "><span style="word-wrap: normal; word-break: normal; ">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>Members&nbsp;<wbr>members&nbsp;<wbr>=&nbsp;<wbr>(Members)&nbsp;<wbr>tuple[&nbsp;<wbr></span><span style="word-wrap: normal; word-break: normal; ">0</span><span style="word-wrap: normal; word-break: normal; ">&nbsp;<wbr>];&nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;<wbr></span></li><li style="padding-bottom: 0px; border-right-width: 0px; list-style-type: decimal; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 30px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px; "><span style="word-wrap: normal; word-break: normal; ">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>String&nbsp;<wbr>className&nbsp;<wbr>=&nbsp;<wbr>(String)&nbsp;<wbr>tuple[&nbsp;<wbr></span><span style="word-wrap: normal; word-break: normal; ">1</span><span style="word-wrap: normal; word-break: normal; ">&nbsp;<wbr>];&nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;<wbr></span></li><li style="padding-bottom: 0px; border-right-width: 0px; list-style-type: decimal; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 30px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px; "><span style="word-wrap: normal; word-break: normal; ">}&nbsp;<wbr>&nbsp;<wbr></span></li></ol></div>&nbsp;<wbr style="color: #494949; font-family: simsun; text-align: left; background-color: #e2ddc7; "><p style="padding-bottom: 0px; border-right-width: 0px; list-style-type: none; margin-top: 0px; margin-right: 0px; margin-left: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px; word-wrap: normal; word-break: normal; color: #494949; font-family: simsun; text-align: left; background-color: #e2ddc7; ">2、构造自己的复合类型，如下：</p><div style="color: #494949; font-family: simsun; text-align: left; background-color: #e2ddc7; "><div>Java代码</div><ol style="padding-bottom: 0px; border-right-width: 0px; list-style-type: none; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px; "><li style="padding-bottom: 0px; border-right-width: 0px; list-style-type: decimal; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 30px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px; "><span style="word-wrap: normal; word-break: normal; "><span style="word-wrap: normal; word-break: normal; ">Query&nbsp;<wbr>q&nbsp;<wbr>=&nbsp;<wbr>session.createQuery(</span><span style="word-wrap: normal; word-break: normal; ">"&nbsp;<wbr>select&nbsp;<wbr>new&nbsp;<wbr>NewMembers(members,&nbsp;<wbr>classInfo.className)&nbsp;<wbr>"</span><span style="word-wrap: normal; word-break: normal; ">&nbsp;<wbr>+&nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;<wbr></span></span></li><li style="padding-bottom: 0px; border-right-width: 0px; list-style-type: decimal; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 30px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px; "><span style="word-wrap: normal; word-break: normal; ">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="word-wrap: normal; word-break: normal; ">"&nbsp;<wbr>from&nbsp;<wbr>Members&nbsp;<wbr>members,&nbsp;<wbr>ClassInfo&nbsp;<wbr>classInfo&nbsp;<wbr>"</span><span style="word-wrap: normal; word-break: normal; ">&nbsp;<wbr>+&nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;<wbr></span></li><li style="padding-bottom: 0px; border-right-width: 0px; list-style-type: decimal; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 30px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px; "><span style="word-wrap: normal; word-break: normal; ">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="word-wrap: normal; word-break: normal; ">"&nbsp;<wbr>where&nbsp;<wbr>members.level&nbsp;<wbr>=&nbsp;<wbr>classInfo.classCode&nbsp;<wbr>"</span><span style="word-wrap: normal; word-break: normal; ">);&nbsp;<wbr>&nbsp;<wbr></span></li></ol></div><p style="padding-bottom: 0px; border-right-width: 0px; list-style-type: none; margin-top: 0px; margin-right: 0px; margin-left: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px; word-wrap: normal; word-break: normal; color: #494949; font-family: simsun; text-align: left; background-color: #e2ddc7; ">当然我们需要有一个NewMembers类和相应的构造方法。</p><img src ="http://www.blogjava.net/writegull/aggbug/376437.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/writegull/" target="_blank">writegull</a> 2012-04-24 09:34 <a href="http://www.blogjava.net/writegull/archive/2012/04/24/376437.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>