Hibernate查詢關聯對象是否為空白的語句

來源:互聯網
上載者:User

一年前發的文章:http://topic.csdn.net/u/20100510/15/284817f8-820d-434d-8e15-1c3099bbcde3.html,都忘記這問題了,今天突然看到訊息有人回複了,看下了,有人給解決了,可開心了,紀念下。

Person類和User類一一對應,映射關係如下

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 person;  
  7. @OneToOne
    ()  
  8. @JoinColumn
    (name=
    "personid"
    )  
  9. public
     Person getPerson() {  
  10.     return
     person;  
  11. }  

我想從person中查詢還未配上User的person列表

view plain
copy to clipboard
print
?
  1. String hql = 
    "from Person as p where p.user is null"
    ;  


可是沒實現我要的結果,myeclipse後台輸出的hibernate語句是的後半部分是 

view plain
copy to clipboard
print
?
  1. from Person person0_ where person0_.id is not 
    null
      

怎麼會是 person0_.id
is not null,怎麼就給變成 person0_.id呢,我要怎麼寫才能實現我要的功能

感謝
MaracMo
的回答:

我今天也碰到了這個問題,研究了下,可以用以下語句查出來:
from Person as p left join p.user as u where u is null.
hibernate預設情況下對關聯屬性的查詢是採用inner join策略,因此
from Person as p where p.user is null 語句是查不到user屬性為空白的person對象的!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.