簡體   English   中英

org.hibernate.QueryParameterException:找不到命名參數[templateId]

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

請找到我使用過的代碼。 HQL下面的查詢失敗說:

無法找到命名參數[templateId]

但是templateId存在於我的模型類中。

請幫助解決出現此類錯誤的問題或可能的原因:

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);

模型文件

@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;
    }
}

請幫忙解決我的問題

您的參數名稱是id而不是templateId 你必須改為:

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

Name after :是參數名稱,必須與setParameter()的第一個參數匹配

你能改變以下幾行嗎?

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

以下行:

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

看到結果?

如果它對某人有幫助,也可以刪除任何人; 如果你的:param位於查詢的最后。

例如,您的查詢必須是:

Select * from blah where param = :param

並不是

Select * from blah where param = :param; 

(注意;最后)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM