HQL以预定义的顺序查找对象的起始索引?

时间:2009-02-25 13:55:04

标签: hibernate jpa 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>

我将以下单词保留在Sentence对象中:

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。

我当前的方法是迭代搜索 - 查询第一个单词的索引,然后使用该索引查看以下单词的索引是否是我想要的。这是一个很好的方法,还是有办法所有在一个查询中?

1 个答案:

答案 0 :(得分:0)

这种方法没有错。迭代查询有时是不可避免的。

有些数据库会返回行号,您可以使用它来构建复杂的查询。我不认为hibernate支持这一点。 (我知道oracle,postgre&gt; = 8.4)很可能性能会比做两次查询更糟糕。

确保您不会遇到无休止的查询,或者查询数量会随着输入的大小或现有数据的大小而变化。