简体   繁体   English

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

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

I have the following query in Hibernate.I got Hibernate Exception and I don't understand why Hibernate throws this exception. 我在Hibernate中有以下查询。我收到了Hibernate Exception,但我不明白为什么Hibernate抛出此异常。 Could anyone help me? 有人可以帮我吗?

Session session = this.sessionFactory.openSession();

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

Try to add some spaces between the = sign and the bind name :laboratoryId and remove the alias: 尝试在=符号和绑定名称:laboratoryId之间添加一些空格,并删除别名:

Session session = this.sessionFactory.openSession();

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

Are you sure that laboratoryID has value? 您确定laboratorID有价值吗? For my query builder I used something like this: 对于我的查询生成器,我使用了以下内容:

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

also same thing for query 也可以查询

  "delete o from Laboratory o"

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

}

But I used it for String values 但是我将其用于字符串值

Please show code for laboratoryId - is it user input or what? 请显示laboratoryId的代码-是用户输入还是什么?

You can try this one. 你可以试试这个。

DELETE Clause 删除条款

The DELETE clause can be used to delete one or more objects. DELETE子句可用于删除一个或多个对象。 Following is the simple syntax of using DELETE clause: 以下是使用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