当前位置: 首页 > 面试题库 >

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

温嘉赐
2023-03-14
问题内容

我正在使用Hibernate 4,并且在JSF页面中有一个过滤器来获取搜索结果。在执行搜索期间,出现以下异常

java.lang.IllegalArgumentException:参数值[568903]与org.hibernate.ejb.AbstractateImpl.java:370处org.hibernate.ejb.AbstractQueryImpl.validateParameterBinding(AbstractQueryImpl.java:370)处的预期类型[java.lang.Long]不匹配(AbstractQueryImpl.java:343)在org.hibernate.ejb.QueryImpl.setParameter(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) {

问题答案:

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

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


 类似资料: