如何将带有子查询的SQL语句转换为HQL

时间:2016-06-27 07:23:51

标签: sql database hibernate nhibernate hql

我有一个相对简单的SQL查询:

SELECT TOP 10 * From Occasion 
WHERE 
(Occasion.StateId = @StateId AND Occasion.EndTime < @EndTime) 
OR 
(
   Occasion.StateId = @StateId 
   AND 
   @StartTime < (SELECT TOP 1 Time FROM StartTime where Occasion.OccasionId = StartTime.OccasionId ORDER BY Time DESC)
)

但我不知道如何将其转换为HQL。我已经做了很多尝试,但却无法让它发挥作用。我主要收到编译时错误,这意味着我的语法不正确。任何帮助将不胜感激。

以下是我的映射文件的相关部分:

<id name="Id" column="OccasionId" type="guid">
  <generator class="guid" />
</id>

<bag name="StartTimeCollection" lazy="false" access="nosetter.camelcase-underscore" table="StartTime">
  <key column="OccasionId" />
  <element type="datetime2">
    <column sql-type="datetime2(7)" not-null="true" name="Time" />
  </element>
</bag>

<property name="EndTime" type="datetime2">
  <column name="EndTime" sql-type="datetime2(7)" not-null="false" />
</property>

<many-to-one not-null="true" lazy="false" fetch="join" name="_state" column="StateId" access="field" />

<property name="Status" formula="StateId" access="readonly" insert="false" update="false" />

<id name="Id" column="OccasionStateId">
  <generator class="native" />
</id>

<discriminator column="Name" type="string">
  <column name="Name" sql-type="nvarchar(256)" />
</discriminator>

<subclass name="PendingState" discriminator-value="Pending" />
<subclass name="StartedState" discriminator-value="Started" />
<subclass name="EndedState" discriminator-value="Ended" />

1 个答案:

答案 0 :(得分:0)

我不知道你的班级模特,所以我不得不猜测。

假设StartTime之间存在一个多对多的关系(这只是一个原始时间值列表):

ComboBox

(TOP 10用null完成。)