Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

hibernate

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

could not locate named parameter [templateId]

But templateId exist in my model class.

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

like image 893
Sakthi Draggerz Avatar asked Jun 20 '16 06:06

Sakthi Draggerz


2 Answers

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

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)

like image 105
Abhishek Avatar answered Nov 14 '22 21:11

Abhishek


Your parameter name is id not templateId. You have to Change to:

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

like image 45
Jens Avatar answered Nov 14 '22 20:11

Jens