当具有相同条件的 HQL 选择有效时,为什么此 HQL 删除会失败?
为什么以下 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 = 7NHibernate.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
语法是
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.
试试这个:
Try this:
从上面 Rafael 提交的链接:
http ://docs.jboss.org/hibernate/stable/core/reference/en/html/batch.html#batch-direct
From the link submitted by Rafael above:
http://docs.jboss.org/hibernate/stable/core/reference/en/html/batch.html#batch-direct