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

后端 未结 3 476
轻奢々
轻奢々 2020-12-16 15:14

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.la

相关标签:
3条回答
  • 2020-12-16 15:29

    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));
    
    0 讨论(0)
  • 2020-12-16 15:45

    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"); 
    
    0 讨论(0)
  • 2020-12-16 15:51

    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.

    0 讨论(0)
提交回复
热议问题