What is the difference between .getSingleResult()
and LIMIT 1
for example in the below hibernate 6 query:
@Override
public Test customQuery3(Long id) {
return (Test) entityManager.createQuery(
"SELECT NEW com.view.Test(a.id, b) FROM Author a " +
"LEFT JOIN Book b ON b.author.id=a.id " +
"WHERE b.id=:id " +
"GROUP BY a, b ORDER BY b.newestBook DESC LIMIT 1")
.setParameter("id", id)
.getSingleResult();
}
From reading SQL documentation LIMIT 1
restricts the results to one and in the example above the newest.
The documentation on .getSingleResult() appears to do the same albeit with some errors thrown if the id is not found.
So why are both needed?