2009-02-25 24 views
0

使用HQL,我想搜索排序對象的起始索引。HQL用於按預定義順序查找對象的起始索引?

舉例來說,如果我有以下持久類:

<class name="Word"> 

    <id name="id" type="int"> 
     <meta attribute="scope-set">protected</meta> 
     <generator class="native"/> 
    </id> 

    <many-to-one name="sentence" class="Sentence"/> 

    <property name="word" type="string"/> 

    <property name="num" type="integer"/> 

</class> 

<class name="Sentence"> 

    <id name="id" type="int"> 
     <meta attribute="scope-set">protected</meta> 
     <generator class="native"/> 
    </id> 

    <set name="words" lazy="true"> 
     <one-to-many class="Word"/> 
    </set> 

</class> 

,我堅持禾以下RDS成句對象:

WORD NUM 
---------- 
the 0 
cow 1 
jumped 2 
over 3 
the 4 
moon 5 
but 6 
the 7 
cow 8 
forgot 9 
her 10 
bell 11 

我想搜索「牛」,並取回0和7,「牛跳樓」的搜索只會返回0

我現在方法是一種迭代搜索 - 對第一個單詞的索引進行查詢,然後使用該索引查看下列單詞的索引是否是我想要的索引。這是一個很好的方法或有沒有辦法做到這一點所有在一個查詢?

回答

0

這種方法沒有任何問題。迭代查詢有時是不可避免的。

有數據庫返回行號,你可能會用它來構造一個複雜的查詢。我不認爲休眠支持這一點。 (我知道oracle,postgre> = 8.4)最有可能的性能會比做兩個查詢更差。

只是確保您不會遇到無盡的查詢,或者查詢的數量隨着輸入大小或現有數據大小而線性增長。

相關問題