I have a query that is used to order messages by the total number of errors. Every message can have multiple instances. In this case I want to order by the total number of instances which have plausibility or integrity errors.
SELECT *
FROM t_message m
LEFT JOIN (
SELECT m.id, COUNT(*) AS count
FROM t_message m
LEFT JOIN t_alteration_instance i ON i.c_message_id = m.id
WHERE i.c_current_step_status = "INTEGRITY_ERROR" OR
i.c_current_step_status= "PLAUSIBILITY_ERROR"
GROUP BY m.id
) AS cnt ON cnt.id = m.id
ORDER BY count DESC
This query works in SQL, but how can I translate this to the criteria API, how can I join two queries or is there any other way?