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
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)
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With