Search code examples
javahibernatehql

Error org.hibernate.exception.SQLGrammarException: could not extract ResultSet


I have the hql query that I am executing but receive an error that I don't really understand the cause of.

This is my code:

@Override
public List<StaffRequest> getStaffLeaveRequest(String userID, Date startDate, Date endDate) 
{
    Session currentSession = sessionFactory.getCurrentSession();        

    List<StaffRequest> 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;
}

I get this error on the webpage when trying to execute it:

org.hibernate.exception.SQLGrammarException: could not extract ResultSet

And also this console error:

ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'Leave leave2_ where leave2_.Leave_Employee=staff0_.Staff_Code and project1_.Proj' at line 1

It seems to indicate some fault at the where clause, but I don't see anything particularly wrong.

UPDATE: Entity classes

Project

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

}

Staff

@Entity
@Table(name="staff")
public class Staff 
{
    @Id
    @Column(name="Staff_Code")
    public String staff_Code;
    ...
}

Solution

  • 1) As you using java.util.Date for Leave.leave_Start, you should annotate with proper @Temporal value:

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="Leave_Start")
    Date leave_Start;
    

    2) When setting your query date parameters, try using:

    .setDate("startDate", startDate)
    

    or

    .setParameter("startDate", startDate, TemporalType.TIMESTAMP)