0

使用 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>

我将以下单词持久化到一个句子对象中:

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

我想搜索“the cow”,然后返回 0 和 7。搜索“the cow jumped”只会返回 0。

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

4

1 回答 1

0

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

有返回行号的数据库,您可以使用它来构建复杂的查询。我不认为hibernate对此有支持。(我知道 oracle,postgre >= 8.4)很可能性能会比执行两个查询更差。

只要确保您不会遇到无休止的查询,或者查询的数量会随着输入的大小或现有数据的大小线性增长。

于 2009-02-25T14:16:45.073 回答