Hibernate statement used to query whether the correlated object is null

Source: Internet
Author: User

Post posted one year ago: Success.

The person class corresponds to the user class one by one. The ing relationship is as follows:

View plain
Copy to clipboard
Print
?
  1. Private
    User user;
  2. @ Onetoone
    (Cascade = {cascadetype. All}, mappedby =
    "Person"
    )
  3. Public
    User getuser (){
  4. Return
    User;
  5. }
  6. Private
    Person;
  7. @ Onetoone
    ()
  8. @ Joincolumn
    (Name =
    "Personid"
    )
  9. Public
    Person getperson (){
  10. Return
    Person;
  11. }

I want to query the list of persons that have not been matched with a user from person.

View plain
Copy to clipboard
Print
?
  1. String hql =
    "From person as P where p. User is null"
    ;


But I didn't implement the desired result. The second half of the hibernate statement output by myeclipse is

View plain
Copy to clipboard
Print
?
  1. From person person0 _ Where person0 _. ID is not
    Null

How is it person0 _. ID?
Is not null. How can I change it to person0 _. ID? How can I write it to implement the functions I want?

Thanks
Maracmo
Answer:

I also encountered this problem today. After studying it, you can use the following statement to find out:
From person as P left join P. User as u where u is null.
By default, Hibernate uses the inner join policy to query Association attributes. Therefore
From person as P where P. User is null statement cannot find the person object with the user attribute being null!

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.