Hibernate HQL One-to-many queries on one side

Source: Internet
Author: User

First say: Is the version of the problem!

In many-to-many or many-to-one, it is easy to find some statements from the one-to-many query

I wrote the HQL for:

From Department as D where d.employees.name= ' Tom ';

An exception occurred at runtime: Org.hibernate.QueryException:illegal attempt to dereference collection

Because: in the HQL statement above, department's associated entity employees is a collection, not directly an employee entity.

In previous versions of Hibernate3.2.2, Hibernate automatically used the implicit inner join for the associated entity,

In other words, the following SQL statement will not have any problem: from Department as D where d.employees.name= ' Tom ';

Since Hibernate3.2.3, hibernate has changed the strategy of this implicit inner join.

For the following statement:

From Department as D where d.employees.name= ' Tom ';

If employees is an ordinary component property, or a single associated entity, Hibernate automatically generates an implicit inner join

If MyEvents is also a collection, then I'm sorry! The system will appear org.hibernate.QueryException:illegal attempt to dereference collection exception.
According to hibernate official saying:
This allows the implicit association to be more deterministic (original: This makes implicit joins more deterministic).

It is recommended to write:

From Department as D INNER join fetch d.employees e where e.name= ' Tom ';

From: http://lishibo613.blog.163.com/blog/static/116100391201010114314573/

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.