JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Object Relational Mapping » Hibernate  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 多個 or 查詢的語法
onlinemad





發文: 22
積分: 1
於 2010-05-20 01:04 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
在程式中要對資料庫中「三個欄位」進行「like」查詢,查某個 stringQuery 是否存在三個欄位

本來我是這樣寫得

1
2
3
    criteria.add(Restrictions.like("A",  stringQuery, MatchMode.ANYWHERE));
    criteria.add(Restrictions.like("B",  stringQuery, MatchMode.ANYWHERE));
    criteria.add(Restrictions.like("C",  stringQuery, MatchMode.ANYWHERE));


但是 hibernate 生出來的 sql 是用「and」去連接三個「like」

所以我改用這樣的作法

1
2
    criteria.add(Restrictions.or(Restrictions.like("A", stringQuery, MatchMode.ANYWHERE),Restrictions.or(
                Restrictions.like("B", stringQuery, MatchMode.ANYWHERE),Restrictions.like("C", stringQuery, MatchMode.ANYWHERE))));


就是一個 or 包住一個 like 以及一個 or

最後 hibernate 生出來的 sql 是這樣

1
    (this_.a like ? or (this_.b like ? or this_.c like ?)) 


這樣可以達成我的需求

不過我想問的是

除此之外沒有別的方法嗎?


reply to postreply to post
I'm onlinemad
I mad therefore I am

My Blog http://CheYingWu.tw
作者 Re:多個 or 查詢的語法 [Re:onlinemad]
maisky





發文: 85
積分: 0
於 2010-05-20 09:11 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
不知道這樣行不行

1
2
3
4
5
6
7
8
9
10
11
12
Criterion first = Restrictions.like("A",  stringQuery, MatchMode.ANYWHERE);
Criterion second = Restrictions.like("B",  stringQuery, MatchMode.ANYWHERE);
Criterion third = Restrictions.like("C",  stringQuery, MatchMode.ANYWHERE);
 
Disjunction disjunction = Restrictions.disjunction();
disjunction.add(first);
disjunction.add(second);
disjunction.add(third);
 
criteria.add(disjunction);
 


reply to postreply to post
作者 Re:多個 or 查詢的語法 [Re:maisky]
onlinemad





發文: 22
積分: 1
於 2010-05-20 23:04 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
沒錯這就是我想要得~感謝了~~

跑出來的 sql 是長這樣

1
2
3
(this_.a like ? or this_.b like ? or this_.c like ?)
 


onlinemad edited on 2010-05-20 23:35
reply to postreply to post
I'm onlinemad
I mad therefore I am

My Blog http://CheYingWu.tw
» JWorld@TW »  Object Relational Mapping » Hibernate

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8