java org.hibernate.QueryParameterException: 找不到命名参数 [templateId]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37915920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:56:55  来源:igfitidea点击:

org.hibernate.QueryParameterException: could not locate named parameter [templateId]

javahibernate

提问by Sakthi Draggerz

Please find the code which I have used. Below HQL query fails saying that:

请找到我使用过的代码。下面的 HQL 查询失败说:

could not locate named parameter [templateId]

找不到命名参数 [templateId]

But templateId exist in my model class.

但是 templateId 存在于我的模型类中。

Please help to resolve the issue or possible reason to get this type of error:

请帮助解决出现此类错误的问题或可能的原因:

session = sessionFactory.openSession();         
                Transaction tx = session.beginTransaction();
                String hql ="from FieldTemplate where templateId= :id";
                Query query = session.createQuery(hql);
                query.setParameter("templateId", id);
                List file=query.list();
                tx.commit();
                return (FieldTemplate) file.get(0);

Model file

模型文件

@Entity
@Table(name="EDW_FIELDS")
public class FieldTemplate {
    @Id
    @Column(name="ID")
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int Id;

    public int getId() {
        return Id;
    }

    public void setId(int id) {
        Id = id;
    }

    @Column(name="TEMPLATE_ID")
    private int templateId;

    public int getTemplateId() {
        return templateId;
    }

    public void setTemplateId(int templateId) {
        this.templateId = templateId;
    }

    @Column(name="FIELD_NAME")
    private String fieldName;

    public String getFieldName() {
        return fieldName;
    }

    public void setFieldName(String fieldName) {
        this.fieldName = fieldName;
    }

    @Column(name="DISPLAY_ORDER")
    private int displayOrder;

    public int getDisplayOrder() {
        return displayOrder;
    }

    public void setDisplayOrder(int displayOrder) {
        this.displayOrder = displayOrder;
    }
}

Please help to resolve my problem

请帮助解决我的问题

回答by Jens

Your parameter name is idnot templateId. You have to Change to:

您的参数名称id不是templateId. 您必须更改为:

String hql ="from FieldTemplate where templateId= :id";
Query query = session.createQuery(hql);
query.setParameter("id", id);

The Name after :is the parameter name and must match the first parameter of setParameter()

后面的 Name:是参数名,必须匹配第一个参数setParameter()

回答by Abhishek

If it helps somebody, also remove any ; if your :param is at the very end of the query.

如果它对某人有帮助,也删除任何 ; 如果您的 :param 位于查询的最后。

So for example, your query must be:

例如,您的查询必须是:

Select * from blah where param = :param

and NOT

并不是

Select * from blah where param = :param; 

(note the ; at the end)

(注意 ; 在末尾)

回答by Sanjeev Saha

Could you change following line:

您能否更改以下行:

String hql ="from FieldTemplate where templateId= :id";

String hql ="from FieldTemplate where templateId= :id";

with following line:

与以下行:

String hql ="from FieldTemplate where templateId= :templateId";

String hql ="from FieldTemplate where templateId= :templateId";

and see the result?

并看到结果?