2010-03-18 100 views
0

我想要做這樣的事情:是否有可能蒙上範圍

def results = Item.findAll("from Item c, Tag b, ItemTag a where c = a.item and b = a.tag and (b.tag like :q or c.uri like :q) " + ob,[q:q]) 
def items = (Item) results[0..1][0] 

,但我得到

Cannot cast object '[Ljava.lang.Object;@1e224a5' with class '[Ljava.lang.Object;' to class 'org.maflt.ibidem.Item' 

我能得到什麼,我需要這一點,但它不」牛逼看起來這是最好的解決辦法:

def items = [] as Set 
def cnt = results.size() 
for (i=0;i<cnt-1;i++) { items << results[i][0] } 
items = items as List 

UPDATE

解決方案建議使用

def results = Item.findAll("from Item c, Tag b, ItemTag a where c = a.item and b = a.tag and (b.tag like :q or c.uri like :q) " + ob,[q:q])  
def items = results[0] as List 

不起作用。該查詢實際上會生成正確的SQL:

select 
    item0_.id as id3_0_, 
    tag1_.id as id16_1_, 
    itemtag2_.id as id6_2_, 
    item0_.version as version3_0_, 
    item0_.last_updated as last3_3_0_, 
    item0_.docsize as docsize3_0_, 
    item0_.customer_id as customer5_3_0_, 
    item0_.uri as uri3_0_, 
    item0_.created_by_person_id as created7_3_0_, 
    item0_.updated_by_person_id as updated8_3_0_, 
    item0_.md5 as md9_3_0_, 
    item0_.original_file_name as original10_3_0_, 
    item0_.date_created as date11_3_0_, 
    item0_.doctype as doctype3_0_, 
    item0_.identifier as identifier3_0_, 
    tag1_.version as version16_1_, 
    tag1_.tagtype_id as tagtype3_16_1_, 
    tag1_.tag as tag16_1_, 
    tag1_.last_updated as last5_16_1_, 
    tag1_.customer_id as customer6_16_1_, 
    tag1_.created_by_person_id as created7_16_1_, 
    tag1_.updated_by_person_id as updated8_16_1_, 
    tag1_.date_created as date9_16_1_, 
    itemtag2_.version as version6_2_, 
    itemtag2_.updated_by_person_id as updated3_6_2_, 
    itemtag2_.weight as weight6_2_, 
    itemtag2_.tag_id as tag5_6_2_, 
    itemtag2_.item_id as item6_6_2_, 
    itemtag2_.last_updated as last7_6_2_, 
    itemtag2_.date_created as date8_6_2_, 
    itemtag2_.source_id as source9_6_2_, 
    itemtag2_.created_by_person_id as created10_6_2_ 
from 
    item item0_, 
    tag tag1_, 
    item_tag itemtag2_ 
where 
    item0_.id=itemtag2_.item_id 
    and tag1_.id=itemtag2_.tag_id 
    and (
     lower(tag1_.tag) like '%english%' 
     or lower(item0_.original_file_name) like '%english%' 
    ) 
order by 
    item0_.id 

但不幸的是items = results [0],因爲List不起作用。它只返回3行,只有項目[0]是一個項目。

items.each{println it.class} 

給出:

class org.maflt.ibidem.Item 
class org.maflt.ibidem.Tag 
class org.maflt.ibidem.ItemTag 
+0

爲什麼你需要轉換爲列表?爲什麼不只是做 def items = results [0] 並正確使用? – Blacktiger 2010-03-22 22:26:00

回答

0

當你的HQL選擇幾個實體,你應該更具體。以下應該只返回物品:

def items = Item.executeQuery("select c from Item as c, Tag as b, ItemTag as a where c = a.item and b = a.tag and (b.tag like :q or c.uri like :q) " + ob,[q:q]) 

乾杯!

+1

感謝您的回覆。 當查詢中存在多個表時,如上面的問題,findAll返回一個n維的對象數組,您需要進行投射。 我需要抓取的項目,例如(Item)結果[0] [0]給出第一個項目。所以它似乎(項目)結果[0..results.size() - 1] [0]應該給我所有的項目。但它不起作用。 – 2010-03-19 01:24:38

+0

@Brad:我剛剛拿到了我的「啊哈」!瞬間...我不知道那:)謝謝!我更新了我的答案。 – lunohodov 2010-03-19 07:38:17

+0

這似乎應該工作,但沒有骰子:(請參閱問題的更新。 再次感謝。 – 2010-03-22 21:27:16

相關問題