1.hibernate怎么進(jìn)行多表查詢??? |
Copy to clipboard |
Posted by: worldcreatxr Posted on: 2004-03-16 15:00
hibernate怎么進(jìn)行多表查詢??? 就象sql一樣,select a.x, b.y from a, b where .... hibernate 要對(duì)這樣的查詢?cè)賹懹成湮募?*.hbm.xml 和相應(yīng)的持久類嗎?
請(qǐng)給一個(gè)完整的代碼,謝謝 |
2.Re:hibernate怎么進(jìn)行多表查詢??? [Re: worldcreatxr] |
Copy to clipboard |
Posted by: linux_china Posted on: 2004-03-16 18:41
你的這個(gè)例子就是簡(jiǎn)單的SQL語句,直接執(zhí)行就可以啦。 session.connection.createStatement.executeQuery(SQLSelect),然后是簡(jiǎn)單的jdbc操作啦。! |
3.Re:hibernate怎么進(jìn)行多表查詢??? [Re: worldcreatxr] |
Copy to clipboard |
Posted by: worldcreatxr Posted on: 2004-03-16 20:42
你這不是用hibernate的查詢 |
4.Re:hibernate怎么進(jìn)行多表查詢??? [Re: worldcreatxr] |
Copy to clipboard |
Posted by: worldcreatxr Posted on: 2004-03-16 20:45
這是用hibernate查詢單表的例子:
Session dbSession = null; try { SessionFactory sessionFactory = new Configuration().config().buildSessionFactory(); dbSession = sessionFactory.openSession (); Transaction transaction = dbSession.beginTransaction(); StringBuffer hql = new StringBuffer (“from MemberTable where Name=’”); hql.append (“userName”); hql.append(“’”); Query query = dbSession.createQuery (hql.toString()); query.setFirstResult(0); query.setMaxResults(1); transaction.commit();
Iterator memberIterator = query.iterate(); if (memberIterator.hasNext()) { MemberTable memberObj = (MemberTable) memberIterator.next();//要是查詢多表的話,在這里該怎么做? System.out.println (memberObj.getPassword); } } catch (Exception e) { System.err.println ; } finally { try { dbSession.close(); } catch (Exception e) {} } |
5.Re:hibernate怎么進(jìn)行多表查詢??? [Re: worldcreatxr] |
Copy to clipboard |
Posted by: Jove Posted on: 2004-03-16 20:51
請(qǐng)仔細(xì)閱讀Hibernate自帶的文檔(hibernate-2.1\doc)
from Hibernate中文手冊(cè)
10.2. from 子句 可能最簡(jiǎn)單的Hibernate查詢是這樣的形式:
from eg.Cat
它簡(jiǎn)單的返回所有eg.Cat類的實(shí)例。
大部分情況下,你需要賦予它一個(gè)別名(alias),因?yàn)槟阍诓樵兊钠渌胤揭矔?huì)引用這個(gè)Cat。
from eg.Cat as cat
上面的語句為Cat賦予了一個(gè)別名cat 。所以后面的查詢可以用這個(gè)簡(jiǎn)單的別名了。as關(guān)鍵字是可以省略的,我們也可以寫成這樣:
from eg.Cat cat
可以出現(xiàn)多個(gè)類,結(jié)果是它們的笛卡爾積,或者稱為“交叉”連接。
from Formula, Parameter from Formula as form, Parameter as param
讓查詢中的別名服從首字母小寫的規(guī)則,我們認(rèn)為這是一個(gè)好習(xí)慣。這和Java對(duì)局部變量的命名規(guī)范是一致的。(比如,domesticCat).
|
6.Re:hibernate怎么進(jìn)行多表查詢??? [Re: worldcreatxr] |
Copy to clipboard |
Posted by: NoLimited Posted on: 2004-03-18 09:39
好像大家討論的有點(diǎn)偏差,樓主好像沒說明白,你這個(gè)多表查詢select a.x, b.y from a, b where ....既然完全可以在sql中寫明,那就完全可以像單表一樣操作,即:
StringBuffer hql = new StringBuffer (“from a,b where Name=’”); ...... |
7.Re:hibernate怎么進(jìn)行多表查詢??? [Re: worldcreatxr] |
Copy to clipboard |
Posted by: worldcreatxr Posted on: 2004-03-18 11:39
我的問題是當(dāng)查詢結(jié)果為多個(gè)表的結(jié)果時(shí) (可以做到嗎?比如一個(gè)SQL查詢?yōu)閟elect a.x,b.y from a,b where a.c=b.c)
那么HQL怎么寫?以及怎么讀出來??
Iterator memberIterator = query.iterate(); if (memberIterator.hasNext()) { MemberTable memberObj = (MemberTable) memberIterator.next();//要是查詢多表的話,在這里該怎么做? System.out.println (memberObj.getPassword); } } catch (Exception e) { System.err.println ; } finally { try { dbSession.close(); } catch (Exception e) {} } |
8.Re:hibernate怎么進(jìn)行多表查詢??? [Re: worldcreatxr] |
Copy to clipboard |
Posted by: Jove Posted on: 2004-03-18 11:47
似乎Iterator.next()返回的是Object[] 看文檔啦,老兄.. |
9.Re:hibernate怎么進(jìn)行多表查詢??? [Re: Jove] |
Copy to clipboard |
Posted by: worldcreatxr Posted on: 2004-03-18 12:34
Jove wrote: 似乎Iterator.next()返回的是Object[] 看文檔啦,老兄..
是啊,我又沒說不是
我的問題是當(dāng)查詢結(jié)果為多個(gè)表的結(jié)果時(shí) (可以做到嗎?比如一個(gè)SQL查詢?yōu)閟elect a.x,b.y from a,b where a.c=b.c)
那么HQL怎么寫?以及怎么讀出來??
Iterator memberIterator = query.iterate(); if (memberIterator.hasNext()) { MemberTable memberObj = (MemberTable) memberIterator.next();//要是查詢多表的話,在這里該怎么做?這個(gè)查詢結(jié)果是個(gè)單表結(jié)果,可以用(MemberTable)...這樣來強(qiáng)制轉(zhuǎn)化。要是查詢結(jié)果是多個(gè)表的結(jié)果怎么辦?
hibernate要對(duì)每一個(gè)查詢集做一個(gè)映射嗎(分別寫一個(gè)對(duì)應(yīng)的*.hbm.xml及*.java)文件???只能這么做?
System.out.println (memberObj.getPassword); } } catch (Exception e) { System.err.println ; } finally { try { dbSession.close(); } catch (Exception e) {} } |
10.Re:hibernate怎么進(jìn)行多表查詢??? [Re: worldcreatxr] |
Copy to clipboard |
Posted by: Jove Posted on: 2004-03-18 13:34
sample..
001 try { 002 System.out.println("test.."); 003 List list = 004 s 005 .createQuery("select user.id,relation.id from User user,Relationship relation") 006 .list(); 007 for (Iterator iter = list.iterator(); iter.hasNext();) { 008 Object[] record = (Object[]) iter.next(); 009 System.out.print(record[0] + "---" + record[1]); 010 } 011 s.close(); 012 } catch (HibernateException e) { 013 System.err.println(e.getMessage()); 014 } |
11.Re:hibernate怎么進(jìn)行多表查詢??? [Re: worldcreatxr] |
Copy to clipboard |
Posted by: worldcreatxr Posted on: 2004-03-18 13:37
這里用上了 hibernate 了嗎? |
12.Re:hibernate怎么進(jìn)行多表查詢??? [Re: Jove] |
Copy to clipboard |
Posted by: worldcreatxr Posted on: 2004-03-18 13:40
linux_china wrote: 你的這個(gè)例子就是簡(jiǎn)單的SQL語句,直接執(zhí)行就可以啦。 session.connection.createStatement.executeQuery(SQLSelect),然后是簡(jiǎn)單的jdbc操作啦。!
Jove wrote: sample..
001 try { 002 System.out.println("test.."); 003 List list = 004 s 005 .createQuery("select user.id,relation.id from User user,Relationship relation") 006 .list(); 007 for (Iterator iter = list.iterator(); iter.hasNext() { 008 Object[] record = (Object[]) iter.next(); 009 System.out.print(record[0] + "---" + record[1]); 010 } 011 s.close(); 012 } catch (HibernateException e) { 013 System.err.println(e.getMessage()); 014 }
你寫的是不是直接使用了session.connection.createStatement.executeQuery(SQLSelect)這樣的操作啊。。。 |
13.Re:hibernate怎么進(jìn)行多表查詢??? [Re: worldcreatxr] |
Copy to clipboard |
Posted by: Jove Posted on: 2004-03-18 13:45
這是HQL,如假包換,童叟無欺 |
14.Re:hibernate怎么進(jìn)行多表查詢??? [Re: Jove] |
Copy to clipboard |
Posted by: javait Posted on: 2004-03-19 11:22
worldcreatxr兄,
try { 002 System.out.println("test.."); 003 List list = 004 s 005 .createQuery("select user.id,relation.id from User user,Relationship relation") 006 .list(); 007 for (Iterator iter = list.iterator(); iter.hasNext() { 008 Object[] record = (Object[]) iter.next(); 009 System.out.print(record[0] + "---" + record[1]); 010 } 011 s.close(); 012 } catch (HibernateException e) { 013 System.err.println(e.getMessage()); 014 }
中的s 不就是hibernate session 嗎?hibernate 支持的是HQL。
謝謝! -Javait |
|