当具有相同条件的 HQL 选择有效时,为什么此 HQL 删除会失败?

发布于 2024-09-25 19:49:47 字数 1415 浏览 0 评论 0原文

为什么以下 HQL 查询失败?

string hql = @"delete MyLog log
               where
                    log.UtcTimestamp < :threshold and
                    log.Configuration.Application = :application";

session.CreateQuery(hql)
       .SetDateTime("threshold", threshold)
       .SetEnum("application", this.application)
       .ExecuteUpdate();

在选择中使用相同形式的查询时:

string hql = @"from MyLog log
               where
                    log.UtcTimestamp < :threshold and
                    log.Configuration.Application = :application";
IList<MyLog> log = session.CreateQuery(hql)
    .SetDateTime("threshold", threshold)
    .SetEnum("application", this.application)
    .List<MyLog>();

MyLog 的映射包含:

References(x => x.Configuration)
     .Columns("CONFIGURATION_ID")
     .ReadOnly();      

Configuration 的映射包含:

Map(x => x.Application, "APPLICATION_ID");

我得到的错误是:

从我的日志、配置中删除 countercon1_ 其中 UTC_TIMESTAMP<:p0 和 APPLICATION_ID=:p1; :p0 = 2010/04/10 17:15:52,:p1 = 7

NHibernate.Exceptions.GenericADOException: 无法执行更新查询 [SQL:

从我的日志、配置中删除 countercon1_ 其中 UTC_TIMESTAMP< ?和 APPLICATION_ID=?

] ---> Oracle.DataAccess.Client.OracleException: ORA-00933: SQL 命令不正确 已结束

Why does the following HQL query fail?

string hql = @"delete MyLog log
               where
                    log.UtcTimestamp < :threshold and
                    log.Configuration.Application = :application";

session.CreateQuery(hql)
       .SetDateTime("threshold", threshold)
       .SetEnum("application", this.application)
       .ExecuteUpdate();

The same form of query works when used in a select:

string hql = @"from MyLog log
               where
                    log.UtcTimestamp < :threshold and
                    log.Configuration.Application = :application";
IList<MyLog> log = session.CreateQuery(hql)
    .SetDateTime("threshold", threshold)
    .SetEnum("application", this.application)
    .List<MyLog>();

The mapping for MyLog contains:

References(x => x.Configuration)
     .Columns("CONFIGURATION_ID")
     .ReadOnly();      

The mapping for Configuration contains:

Map(x => x.Application, "APPLICATION_ID");

The error I get is:

delete from MYLOG, CONFIGURATION
countercon1_ where UTC_TIMESTAMP<:p0
and APPLICATION_ID=:p1; :p0 =
04/10/2010 17:15:52, :p1 = 7

NHibernate.Exceptions.GenericADOException:
could not execute update query [SQL:

delete from MYLOG, CONFIGURATION
countercon1_ where UTC_TIMESTAMP< ? and
APPLICATION_ID= ?

] --->
Oracle.DataAccess.Client.OracleException:
ORA-00933: SQL command not properly
ended

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拍不死你 2024-10-02 19:49:48

语法是 DELETE FROM MyLog ....

请记住,HQL 删除不支持使用 (n)hibernate 映射定义的级联。

所以你可以选择所有实体并一一删除。

The syntax is DELETE FROM MyLog ....

Have in mind that HQL delete does not honour cascades defined with (n)hibernate mappings.

So you can select all the entities and delete them one by one.

绮烟 2024-10-02 19:49:47

试试这个:

delete MyLog log
where log.id in
          (select l.id
           from MyLog l
           where l.UtcTimestamp < :threshold and
           and.Configuration.Application = :application)

Try this:

delete MyLog log
where log.id in
          (select l.id
           from MyLog l
           where l.UtcTimestamp < :threshold and
           and.Configuration.Application = :application)
爱人如己 2024-10-02 19:49:47

从上面 Rafael 提交的链接:

http ://docs.jboss.org/hibernate/stable/core/reference/en/html/batch.html#batch-direct

没有连接,无论是隐式的还是显式的,
可以在批量 HQL 查询中指定。
子查询可以用在
where 子句,where 子查询
它们本身可能包含连接

From the link submitted by Rafael above:

http://docs.jboss.org/hibernate/stable/core/reference/en/html/batch.html#batch-direct

No joins, either implicit or explicit,
can be specified in a bulk HQL query.
Sub-queries can be used in the
where-clause, where the subqueries
themselves may contain joins

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文