繁体   English   中英

休眠异常-找不到命名参数[:laboratoryId]

[英]Hibernate Exception - could not locate named parameter [:laboratoryId]

我在Hibernate中有以下查询。我收到了Hibernate Exception,但我不明白为什么Hibernate抛出此异常。 有人可以帮我吗?

Session session = this.sessionFactory.openSession();

session.createQuery("delete from Laboratory l where l.id=:laboratoryId")
        .setParameter("laboratoryId", laboratoryId).executeUpdate();
session.close();

尝试在=符号和绑定名称:laboratoryId之间添加一些空格,并删除别名:

Session session = this.sessionFactory.openSession();

session.createQuery("delete from Laboratory where id = :laboratoryId")
    .setParameter("laboratoryId", laboratoryId)
    .executeUpdate();
session.close();

您确定laboratorID有价值吗? 对于我的查询生成器,我使用了以下内容:

if (!laboratoryId.isEmpty()) { 
        query.setParameter("laboratoryId", laboratoryId());
        }

也可以查询

  "delete o from Laboratory o"

if(!laboratoryId.isEmpty()){
query.append("where o.id = (:laboratoryId)")

}

但是我将其用于字符串值

请显示laboratoryId的代码-是用户输入还是什么?

你可以试试这个。

删除条款

DELETE子句可用于删除一个或多个对象。 以下是使用DELETE子句的简单语法:

String hql = "DELETE FROM Employee "  + 
         "WHERE id = :employee_id";
Query query = session.createQuery(hql);
query.setParameter("employee_id", 10);
int result = query.executeUpdate();
System.out.println("Rows affected: " + result);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM