Fix org.hibernate.QueryException illegal attempt to dereference collection exception error

Source: Internet
Author: User
Tags join set set

Today, when doing the project, there are two entities: style, brand relationship is many-to-many relationships, the realization of the function is: through the selection of styles, showing the style of all brands. The HQL statement is as follows:

From Brand as B where b.styles.styleid=?

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

By looking at the data, we found that in the HQL statement above, brand's associated entity styles is a set set, not a style entity. In previous versions of Hibernate3.2.2, Hibernate automatically used an implicit inner join for the associated entity, meaning that using the HQL statement above was no problem.

However, after the Hibernate3.2.2 version, Hibernate changed this strategy. It uses the following policy to correlate entities.

Also for the above HQL statement. If styles is a single associated entity or is a normal attribute, Hibernate automatically uses the implicit inner join. But if Styles is a collection, then I'm sorry, the system will appear org.hibernate.QueryException:illegal attempt to dereference collection exception.
For the solution, either you return the hibernate3.2.2 version or use the following form of the HQL statement:

          From Brand as B inner join fetch b.styles as s where s.styleid=?

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.