开发中关于Hibernate对多表关联查询
Hibernate 进行多表关联查询Hibernate对多个表进行查询时,查询结果是多个表的笛卡尔积,
或者称为“交叉”连接。 例如:from Student, Book from Student as stu, Book as boo from
Student stu, Book boo注意:让查询中的Student和Book均是表student和book对应的类名,它的
名字一定要和类的名字相同,包括字母的大小写。别名应该服从首字母小写的规则是一个好习惯,
这和Java对局部变量的命名规范是一致的。
例如:
String sTest = "from tBookInfo book, BookSelection sel where book.id = sel.bookId";
  Collection result = new ArrayList();
  Transaction tx = null;
  try {
   Session session = HibernateUtil.currentSession();
   tx = session.beginTransaction();
   Query query = session.createQuery(sql);
   result = query.list();
   tx.commit();
  } catch (Exception e) {
   throw e;
  } finally {
   HibernateUtil.closeSession();
  }
  ArrayList sList = (ArrayList) result;
  Iterator iterator1 = sList.iterator();
  while (iterator1.hasNext()) {
   Object[] o = (Object[]) iterator1.next();
   tBookInfo bookInfo = (tBookInfo) o[0];
   BookSelection bookSelect = (BookSelection) o[1];
   System.out.println(
"BookInfo-Title: " + bookInfo.getTitle());
   System.out.println(
"BookSelection-BookSelectionId: " + bookSelect.getId());
  }

转自:http://develop.csai.cn/java/200805231428261254.htm
lunzi   2008-06-11 10:14:34 评论:0   阅读:464   引用:0

发表评论>>

署名发表(评论可管理,不必输入下面的姓名)

姓名:

主题:

内容: 最少15个,最长1000个字符

验证码: (如不清楚,请刷新)

Copyright@2008 powered by YuLog