java long hibernate,Hibernate参数值[568903]与预期类型不匹配[java.lang.Long]

I am using Hibernate 4 and I have a filter in JSF page to get search results. During execution of search I am getting the following exception

java.lang.IllegalArgumentException: Parameter value [568903] did not match

expected type [java.lang.Long]

at org.hibernate.ejb.AbstractQueryImpl.validateParameterBinding(AbstractQueryImpl.java:370)

at org.hibernate.ejb.AbstractQueryImpl.registerParameterBinding(AbstractQueryImpl.java:343)

at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:370)

at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:323)

Below is my code snippet, how can I fix this issue?

private Long projectNo;

public Long getProjectNo() {

return projectNo;

}

public void setProjectNo(Long projectNo) {

this.projectNo = projectNo;

}

And in DAO class I have the following

String projectNo = filters.get("projectNo");

List criteria = new ArrayList();

if (projectNo!= null) {

ParameterExpression pexp = cb.parameter(String.class, "projectNo");

Predicate predicate = cb.equal(emp.get(Project_.projectNo), pexp);

criteria.add(predicate);

}

TypedQuery q = entityManager.createQuery(c);

TypedQuery countquery = entityManager.createQuery(countQ);

q.setParameter("projectNo", projectNo); // error in this line

countquery.setParameter("projectNo", projectNo);

Edit 1

public void getProjects(ProjectQueryData data) {

and in ProjectQueryData class, I have the following as constructor

public ProjectQueryData (int start, int end, String field,

QuerySortOrder order, Map filters) {

解决方案

Because type of persistent attribute projectNo is Long, type argument when creating ParameterExpression should be Long. And consequently, because type of the ParameterExpression is Long, type of the parameter's value should be Long as well:

//because this persistent Attribute is Long:

private Long projectNo;

//we use Long here as well

ParameterExpression pexp = cb.parameter(Long.class, "projectNo");

...

//and finally set parameter. Long again, because that is the type

// type of ParameterExpression:

query.setParameter("projectNo", Long.valueOf(projectNo));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值