当前位置: 首页 > 知识库问答 >
问题:

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

虞展
2023-03-14

我正在使用Hibernate4,我在JSF页面中有一个过滤器来获取搜索结果。在执行搜索期间,我得到以下异常

java.lang.IllegalArgumentException:参数值[568903]与org.hibernate.ejb.AbstractQueryImpl.validateParameterBinding(AbstractQueryImpl.java:370)org.hibernate.ejb.AbstractQueryImpl.registerParameterBinding(AbstractQueryImpl.java:343)org.hibernate.ejb.QueryImpl.setParameter上的预期类型[java.lang.lang.Long]不匹配(QueryImpl.java:370)位于org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:323)

下面是我的代码片段,如何解决此问题?

private Long projectNo; 

public Long getProjectNo() {
    return projectNo;
}

public void setProjectNo(Long projectNo) {
    this.projectNo = projectNo;
}

在DAO类中,我有以下几点

String projectNo = filters.get("projectNo");
List<Predicate> criteria = new ArrayList<Predicate>();
    if (projectNo!= null) {
    ParameterExpression<String> pexp = cb.parameter(String.class, "projectNo");             
    Predicate predicate = cb.equal(emp.get(Project_.projectNo), pexp);
    criteria.add(predicate);
}
TypedQuery<Project> q = entityManager.createQuery(c);
TypedQuery<Long> countquery = entityManager.createQuery(countQ);
q.setParameter("projectNo", projectNo); // error in this line
countquery.setParameter("projectNo", projectNo);

编辑1

public void getProjects(ProjectQueryData data) { 

ProjectQueryData类中,我将以下内容作为构造函数

public ProjectQueryData (int start, int end, String field,
            QuerySortOrder order, Map<String, String> filters) {

共有1个答案

邵修诚
2023-03-14

因为持久属性projectNo的类型是Long,所以创建ParameterExpression时的类型参数应该是Long。因此,由于ParameterExpression的类型是长的,所以参数值的类型也应该是长的:

//because this persistent Attribute is Long:
private Long projectNo; 

//we use Long here as well
ParameterExpression<Long> 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));
 类似资料: