错误org.hibernate.exception.SQLGrammarException:无法提取ResultSet
来源:爱站网时间:2021-09-11编辑:网友分享
我有正在执行的hql查询,但收到一个我不太了解原因的错误。这是我的代码:@Override public List getStaffLeaveRequest(String ...
问题描述
我有正在执行的hql查询,但收到一个我不太了解原因的错误。
这是我的代码:
@Override
public List getStaffLeaveRequest(String userID, Date startDate, Date endDate)
{
Session currentSession = sessionFactory.getCurrentSession();
List results =
currentSession.createQuery("select new com.timesheet_Webservice.CustomEntity.StaffRequest(lr.leave_ID, lr.leave_Employee, concat(s.staff_First_Name, ' ', s.staff_Last_Name), "
+ "(lr.leave_Days*8.5), lr.leave_Comments, '1805', concat(pro.project_Pastel_Prefix, ' - ', pro.project_Description), lr.leave_Start, lr.leave_End, lr.leave_IsApproved, "
+ "(select lt.leaveType_Description from LeaveType lt where lt.leaveType_ID = lr.leave_Type)) "
+ "from Staff s, Project pro, Leave lr "
+ "where lr.leave_Employee = s.staff_Code and pro.project_Code = 1805 and lr.leave_Approved = :userID and lr.leave_IsApproved = 0 and s.staff_IsEmployee 0 "
+ "and lr.leave_Start between :startDate and :endDate "
+ "order by concat(s.staff_First_Name, ' ', s.staff_Last_Name)")
.setParameter("userID",userID).setParameter("startDate", startDate).setParameter("endDate", endDate).getResultList();
return results;
}
尝试执行时,我在网页上收到此错误:
org.hibernate.exception.SQLGrammarException:无法提取ResultSet
还有这个控制台错误:
错误:您的SQL语法有错误;检查与您的MariaDB服务器版本对应的手册,以便在第1行的“离开leave2_,其中leave2_.Leave_Employee = staff0_.Staff_Code和project1_.Proj”附近使用正确的语法
它似乎表明where子句有些错误,但我没有看到任何特别错误。
更新:实体类
项目
@Entity
@Table(name="project")
public class Project {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="Project_Code")
public int project_Code;
@Column(name="Project_Customer")
public int project_Customer;
//A lot more attributes...
}
员工
@Entity
@Table(name="staff")
public class Staff
{
@Id
@Column(name="Staff_Code")
public String staff_Code;
...
}
解决方法:
1)当您使用java.util.Date
用于Leave.leave_Start
时,您应该使用适当的@Temporal
值进行注释:
@Temporal(TemporalType.TIMESTAMP)
@Column(name="Leave_Start")
Date leave_Start;
2)设置查询日期参数时,请尝试使用:
.setDate("startDate", startDate)
要么
.setParameter("startDate", startDate, TemporalType.TIMESTAMP)