Hibernate multi-Table query hql

Source: Internet
Author: User

inner join (内连接) left outer join (左外连接) right outer join (右外连接) full join (全连接,并不常用) SQL中的条件用 on 即:left join ... on ... HQL中的条件用with即:left join ... with...
语句inner join, left outer join以及 right outer join可以简写。 fromCat as cat     joincat.mate asmate    left joincat.kittens askitten 通过HQL的with关键字,你可以提供额外的join条件。 fromCat as cat     left joincat.kittens askitten         with kitten.bodyWeight > 10.0 还有,一个"fetch"连接允许仅仅使用一个选择语句就将相关联的对象或一组值的集合随着他们的父对象的初始化而被初始化,这种方法在使用到集合的情况下尤其有用,对于关联和集合来说,它有效的代替了映射文件中的外联接 与延迟声明(lazy declarations). fromCat ascat     inner joinfetch cat.mate    left joinfetch cat.kittens 一个fetch连接通常不需要被指定别名, 因为相关联的对象不应当被用在 where子句 (或其它任何子句)中。同时,相关联的对象 并不在查询的结果中直接返回,但可以通过他们的父对象来访问到他们。 fromCat as cat     inner joinfetch cat.mate    left joinfetch cat.kittens child    left joinfetch child.kittens 假若使用iterate()来调用查询,请注意fetch构造是不能使用的(scroll() 可以使用)。fetch也不应该与setMaxResults() 或setFirstResult()共用,这是因为这些操作是基于结果集的,而在预先抓取集合类时可能包含重复的数据,也就是说无法预先知道精确的行数。fetch还不能与独立的 with条件一起使用。通过在一次查询中fetch多个集合,可以制造出笛卡尔积,因此请多加注意。对bag映射来说,同时join fetch多个集合角色可能在某些情况下给出并非预期的结果,也请小心。最后注意,使用full joinfetch 与 right joinfetch是没有意义的。 如果你使用属性级别的延迟获取(lazy fetching)(这是通过重新编写字节码实现的),可以使用 fetch all properties 来强制Hibernate立即取得那些原本需要延迟加载的属性(在第一个查询中)。fromDocument fetch all properties order byname from Document doc fetch all properties wherelower(doc.name) like ‘‘%cats%‘‘

  

Hibernate multi-Table query hql

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.