CJSDN开发者社区 - hibernate怎么进行多表查询???

来源:百度文库 编辑:神马文学网 时间:2024/06/13 08:22:29
Topic: hibernate怎么进行多表查询???
  Print this page
1.hibernate怎么进行多表查询???Copy to clipboard
Posted by: worldcreatxr
Posted on: 2004-03-16 15:00
hibernate怎么进行多表查询???
就象sql一样,select a.x, b.y from a, b where ....
hibernate 要对这样的查询再写映射文件 *.hbm.xml 和相应的持久类吗?
请给一个完整的代码,谢谢
2.Re:hibernate怎么进行多表查询??? [Re: worldcreatxr]Copy to clipboard
Posted by: linux_china
Posted on: 2004-03-16 18:41
你的这个例子就是简单的SQL语句,直接执行就可以啦。
session.connection.createStatement.executeQuery(SQLSelect),然后是简单的jdbc操作啦。!
3.Re:hibernate怎么进行多表查询??? [Re: worldcreatxr]Copy to clipboard
Posted by: worldcreatxr
Posted on: 2004-03-16 20:42
你这不是用hibernate的查询
4.Re:hibernate怎么进行多表查询??? [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怎么进行多表查询??? [Re: worldcreatxr]Copy to clipboard
Posted by: Jove
Posted on: 2004-03-16 20:51
请仔细阅读Hibernate自带的文档(hibernate-2.1\doc)
fromHibernate中文手册
10.2. from 子句
可能最简单的Hibernate查询是这样的形式:
from eg.Cat
它简单的返回所有eg.Cat类的实例。
大部分情况下,你需要赋予它一个别名(alias),因为你在查询的其他地方也会引用这个Cat。
from eg.Cat as cat
上面的语句为Cat赋予了一个别名cat 。所以后面的查询可以用这个简单的别名了。as关键字是可以省略的,我们也可以写成这样:
from eg.Cat cat
可以出现多个类,结果是它们的笛卡尔积,或者称为“交叉”连接。
from Formula, Parameter
from Formula as form, Parameter as param
让查询中的别名服从首字母小写的规则,我们认为这是一个好习惯。这和Java对局部变量的命名规范是一致的。(比如,domesticCat).
6.Re:hibernate怎么进行多表查询??? [Re: worldcreatxr]Copy to clipboard
Posted by: NoLimited
Posted on: 2004-03-18 09:39
好像大家讨论的有点偏差,楼主好像没说明白,你这个多表查询select a.x, b.y from a, b where ....既然完全可以在sql中写明,那就完全可以像单表一样操作,即:
StringBuffer hql = new StringBuffer (“from a,b where Name=’”);
......
7.Re:hibernate怎么进行多表查询??? [Re: worldcreatxr]Copy to clipboard
Posted by: worldcreatxr
Posted on: 2004-03-18 11:39
我的问题是当查询结果为多个表的结果时
(可以做到吗?比如一个SQL查询为select 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怎么进行多表查询??? [Re: worldcreatxr]Copy to clipboard
Posted by: Jove
Posted on: 2004-03-18 11:47
似乎Iterator.next()返回的是Object[]
看文档啦,老兄..
9.Re:hibernate怎么进行多表查询??? [Re: Jove]Copy to clipboard
Posted by: worldcreatxr
Posted on: 2004-03-18 12:34
Jove wrote:
似乎Iterator.next()返回的是Object[]
看文档啦,老兄..
是啊,我又没说不是
我的问题是当查询结果为多个表的结果时
(可以做到吗?比如一个SQL查询为select 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();//要是查询多表的话,在这里该怎么做?这个查询结果是个单表结果,可以用(MemberTable)...这样来强制转化。要是查询结果是多个表的结果怎么办?
hibernate要对每一个查询集做一个映射吗(分别写一个对应的*.hbm.xml及*.java)文件???只能这么做?
System.out.println (memberObj.getPassword);
}
} catch (Exception e) {
System.err.println ;
} finally {
try { dbSession.close(); } catch (Exception e) {}
}
10.Re:hibernate怎么进行多表查询??? [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怎么进行多表查询??? [Re: worldcreatxr]Copy to clipboard
Posted by: worldcreatxr
Posted on: 2004-03-18 13:37
这里用上了 hibernate 了吗?
12.Re:hibernate怎么进行多表查询??? [Re: Jove]Copy to clipboard
Posted by: worldcreatxr
Posted on: 2004-03-18 13:40
linux_china wrote:
你的这个例子就是简单的SQL语句,直接执行就可以啦。
session.connection.createStatement.executeQuery(SQLSelect),然后是简单的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怎么进行多表查询??? [Re: worldcreatxr]Copy to clipboard
Posted by: Jove
Posted on: 2004-03-18 13:45
这是HQL,如假包换,童叟无欺
14.Re:hibernate怎么进行多表查询??? [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