IllegalArgumentException:使用AliasToBeanResultTransformer和MySQL UNION SQLQuery

时间:2018-01-05 07:10:24

标签: java mysql hibernate

我在Hibernate中使用AliasToBeanResultTransformer进行以下查询,正常工作
SQL查询1

//Query 1
final String GET_ASSIGNMENT_STATUS = 
    "select distinct wtd.work_task_detail_id as workTaskDetailId, wtd.assigned_task as assignedTask, "
    + "e.emp_id as employeeId, concat( e.first_name,' ',e.last_name ) as employeeName, "
    + "wtd.deadline_date as deadlineDate, a.code_name as workTaskStatusName, a.code_id as workTaskStatusId, "
    + "wtd.completion_date as completionDate, wtd.priority as priority "
    + "from work_task_detail wtd "
    + "join allcode a on a.code_id = wtd.work_task_status "
    + "join employee e on e.emp_id = wtd.team_member_id  "
    + "where wtd.work_task_detail_id = :assignmentId";

如果我在MySQL union 查询中使用相同的选择条件,则会给我异常 IllegalArgumentException:参数类型不匹配

SQL查询2

//Query 2           
final String GET_ASSIGNMENT_STATUS = 
    "select distinct wtd.work_task_detail_id as workTaskDetailId, wtd.assigned_task as assignedTask, "
    + "e.emp_id as employeeId, concat( e.first_name,' ',e.last_name ) as employeeName, "
    + "wtd.deadline_date as deadlineDate, a.code_name as workTaskStatusName, a.code_id as workTaskStatusId, "
    + "wtd.completion_date as completionDate, wtd.priority as priority "
    + "from work_task_detail wtd "
    + "join logs l on wtd.work_task_detail_id = l.tran_id and l.transaction='ASGNMT' "
    + "join user u on u.user_id = l.user_id "
    + "join employee e on e.emp_id = u.emp_id "
    + "join allcode a on a.code_id = wtd.work_task_status "
    + "where wtd.work_task_detail_id = :assignmentId and l.user_id = :userId "
    + "union "
    + "select distinct wtd.work_task_detail_id as workTaskDetailId, wtd.assigned_task as assignedTask, "
    + "e.emp_id as employeeId, concat( e.first_name,' ',e.last_name ) as employeeName, "
    + "wtd.deadline_date as deadlineDate, a.code_name as workTaskStatusName, a.code_id as workTaskStatusId, "
    + "wtd.completion_date as completionDate, wtd.priority as priority "
    + "from work_task_detail wtd "
    + "join allcode a on a.code_id = wtd.work_task_status "
    + "join employee e on e.emp_id = wtd.team_member_id  "
    + "where wtd.work_task_detail_id = :assignmentId and wtd.team_member_id = ( select emp_id from user uu where uu.user_id = :userId )";

为什么使用 UNION 导致以下异常?
日志

05-01-2018 12:14:57 320 org.hibernate.property.access.spi.SetterMethodImpl.set(SetterMethodImpl.java:97) 
 HHH000123: IllegalArgumentException in class: support.dto.AssignmentStatus, setter method of property: priority 
 05-01-2018 12:14:57 324 org.hibernate.property.access.spi.SetterMethodImpl.set(SetterMethodImpl.java:98) 
 HHH000091: Expected type: boolean, actual value: java.lang.Byte 

另一个

05-01-2018 12:14:57 342 support.DAO.ImplDeveloperDAO.getAssignmentStatus(ImplDeveloperDAO.java:1916) 
 Message     :- IllegalArgumentException occurred while calling setter for property [support.dto.AssignmentStatus.priority (expected type = boolean)]; target = [support.dto.AssignmentStatus@11643c8], property value = [0] setter of support.dto.AssignmentStatus.priority 
 05-01-2018 12:14:57 345 support.DAO.ImplDeveloperDAO.getAssignmentStatus(ImplDeveloperDAO.java:1917) 
 Root Cause  :- java.lang.IllegalArgumentException: argument type mismatch 
 05-01-2018 12:14:57 346 support.DAO.ImplDeveloperDAO.getAssignmentStatus(ImplDeveloperDAO.java:1918) 
              ************************************************************ 

到目前为止我尝试了什么

  1. 我在选择标准中使用 if(wtd.priority = 0,FALSE,TRUE)
  2. 我还尝试了另外两个priority的重载setter,参数为byte,其他BigInteger

1 个答案:

答案 0 :(得分:0)

我提到this answer仍有问题,建议为 Expected type: boolean, actual value: java.lang.String

所以我为我的字段priority acceptiong String重载了一个setter方法作为参数。现在它按预期工作了。

public void setPriority(String priority) {
    this.priority = "true".equals(priority);
}

谢谢。