在Hibernate HQL更新查询中使用连接

时间:2012-08-20 08:20:25

标签: hibernate nhibernate join hql

我有以下hibernate映射:

 <class name="Domain.Roomreservation, Core" table="Reservationroom">
    <id name="ID" unsaved-value="undefined">
        <generator class="native">
            <!--<param name="sequence">GLOBALSEQUENCE</param>-->
        </generator>
    </id>

    <property name="FromTime" not-null="true" index="IDX_RESRAUM_FromTime" />
    <property name="UntilTime" not-null="true" index="IDX_RESRAUM_UntilTime"/>

    <many-to-one name="Booking" column="Book_ID" index="IDX_RAUMRES_BOOK" lazy="false"
        class="Domain.Booking, Core" not-null="true" />
    </class>

Reservationroom表格如下:

ID         <pk>
Book_ID    <fk>
FromTime
UntilTime
....
....

我的Hibernate查询如下:

String hql = "UPDATE Roomreservation as rr set rr.FromTime= 12:15" +
                     "Inner Join Booking b ON rr.Book_ID= b.ID " +
                     "Where b.ID = 95637";
                        IQuery query = CurrentSession.CreateQuery(hql);
                        int result = query.ExecuteUpdate();
                        Debug.WriteLine("Rows affected: " + result);

但我总是得到Error: NHibernate.Hql.Ast.ANTLR.QuerySyntaxException

有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:22)

您必须使用子查询而不是连接。大致如下:

UPDATE Roomreservation as rr set rr.FromTime= 12:15
WHERE rr.Book_ID IN (
  SELECT b.id 
  FROM Booking b 
  WHERE b.id = 95637);

此外,根据FromTime的类型,它可能会以其他格式呈现。

答案 1 :(得分:0)

rr.FromTime = '12:15'的缺失单引号应该是错误。

String hql = "UPDATE Roomreservation as rr set rr.FromTime= '12:15' " +
"Inner Join Booking b ON rr.Book_ID= b.ID " +
"Where b.ID = 95637";

请你检查一下。