In my application I want to find the latest duty of each user from 'StaffDuty' table using hibernate query (i.e. HQL). Below is my query.
query = session.createQuery("FROM StaffDuty where deptId.id = :dId and DATE(startDate) <= DATE(:selDate) and DATE(endDate) >= DATE(:selDate) group by staffId.id order by createdOn desc");
But this query doesn't return the latest record of each staff because the 'GROUP BY' clause execute before the 'ORDER BY' clause. How can I retrieve the latest records of all staff using HQL? I am using MySQL in backend.
| id | staffId | deptId | startDate | endDate | duty | createdOn |
|---|---|---|---|---|---|---|
| 1 | s45 | d5 | 2025-07-17 | 2025-07-31 | Cleaning | 2025-06-08 07:21:51 |
| 2 | s35 | d4 | 2025-06-14 | 2025-08-17 | Pantry | 2025-06-10 09:25:25 |
| 3 | s18 | d5 | 2025-06-27 | 2025-07-30 | Office | 2025-06-10 09:43:33 |
| 4 | s35 | d4 | 2025-06-25 | 2025-08-30 | Cleaning | 2025-06-11 09:21:51 |
| 5 | s45 | d5 | 2025-07-07 | 2025-08-11 | Pantry | 2025-07-01 08:15:38 |
| 6 | s35 | d5 | 2025-07-10 | 2025-08-22 | Cleaning | 2025-07-02 10:43:09 |
| 7 | s18 | d5 | 2025-07-17 | 2025-07-31 | Accounts | 2025-07-02 11:21:51 |
| 8 | s35 | d5 | 2025-07-11 | 2025-08-29 | Office | 2025-07-03 09:11:12 |
| 9 | s45 | d4 | 2025-07-18 | 2025-07-30 | Cleaning | 2025-07-05 07:21:51 |
| 10 | s18 | d4 | 2025-07-15 | 2025-08-24 | Accounts | 2025-07-05 09:33:35 |
The expected result when dId = d5 and selDate = 28-07-25 is:
| id | staffId | deptId | startDate | endDate | duty | createdOn |
|---|---|---|---|---|---|---|
| 5 | s45 | d5 | 2025-07-07 | 2025-08-11 | Pantry | 2025-07-01 08:15:38 |
| 7 | s18 | d5 | 2025-07-17 | 2025-07-31 | Accounts | 2025-07-02 11:21:51 |
| 8 | s35 | d5 | 2025-07-11 | 2025-08-29 | Office | 2025-07-03 09:11:12 |
But I got records with id 5,6,7 as result.
StaffDuty class is as below :
@Entity
@Table(name = "staff_duty")
public class StaffDuty implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private long id;
@JoinColumn(name = "staff_id", referencedColumnName = "id", nullable = false)
@OneToOne(optional = false)
private Staff staffId;
@JoinColumn(name = "dept_id", referencedColumnName = "id", nullable = false)
@OneToOne(optional = false)
private Section deptId;
@Column(name = "start_date")
@JsonAdapter(DateJsonSerializer.class)
private Date startDate;
@Column(name = "end_date")
@JsonAdapter(DateJsonSerializer.class)
private Date endDate;
@Column(name = "duty")
private String duty;
@Column(name="created_on")
private Timestamp createdOn;
------------
}