【问题标题】:HQL for finding starting index of objects in a predefined order?HQL以预定义的顺序查找对象的起始索引?
【发布时间】:2009-02-25 13:55:04
【问题描述】:

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

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

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

【问题讨论】:

    标签: hibernate jpa hql


    【解决方案1】:

    这种方法没有任何问题。迭代查询有时是不可避免的。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-20
      • 2016-03-19
      • 2013-08-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多