Hibernate Parameter value [568903] did not match expected type [java.lang.Long]

95,783

Solution 1

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

Solution 2

projectNo is long type in DAO, so change it to long.

Try this:

q.setParameter("projectNo", new Long(projectNo));

I think you should change:

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

to

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

Solution 3

In your DAO class, you are getting projectNo as a String:

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

However, in your model class, you are defining projectNo as a Long.

When you set parameter in your DAO on this line:

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

You are setting the parameter as a String. Try changing that line as follows (assuming that you've null-checked projectNo):

q.setParameter("projectNo", Long.parseLong(projectNo));

It also probably wouldn't hurt (defensive programming) to be sure that projectNo is numeric prior to calling Long.parseLong. You can do this with Apache Commons StringUtils.isNumeric.

Share:
95,783
Jacob
Author by

Jacob

Downvote, at no time in the past or hitherto; not ever.

Updated on March 17, 2020

Comments

  • Jacob
    Jacob over 4 years

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

    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<String, String> filters) {
    
  • Jacob
    Jacob over 11 years
    The following error java.lang.IllegalArgumentException: Named parameter [projectNo] type mismatch; expecting [java.lang.String], found [java.lang.Long]
  • ninnemannk
    ninnemannk over 11 years
    Yout put <code>ParameterExpression<String> pexp = cb.parameter(String.class, "projectNo");</code> Change this to: ParameterExpression<String> pexp = cb.parameter(Long.class, "projectNo");
  • Jacob
    Jacob over 11 years
    @PSR Sorry I didn't include that. filter is Map<String, String> filters = data.getFilters();
  • Jacob
    Jacob over 11 years
    @PSR I have added that code as Edit 1 in my original question.
  • Jacob
    Jacob over 11 years
    @PSR I have the following method in a class and this class extends LazyDataModel public List<Project> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
  • Jacob
    Jacob over 11 years
  • Jacob
    Jacob over 11 years
    @PSR chat here
  • Jacob
    Jacob over 11 years
    Mikko, Thank you so much, this really has helped to solve the problem. Much appreciated.
  • Philip Tenn
    Philip Tenn over 11 years
    +1 nice job, looks like you were able to piece everything together for a cohesive answer that worked!
  • milosmns
    milosmns over 3 years
    Adding megabytes of Apache libs to your project in order to get one util method isn't defensive, it's actually a bad idea. A simple try/catch works just fine in this case.