views:

52

answers:

1

I need to limit number of entities returned by a query to some certain value inside a JPA query (through JPQL). Particularly:

select m from Manual m //constraint

e.g. in sql (mysql syntax) I would do it like:

select * from Manual limit 1

The only solution that comes up is simply to get all entities and then choose first one, which is out of the issue. Any ideas?

A: 

To do that I may limit Query instance, as follows:

em.createQuery("select m from Manual m").setMaxResults(1).getSingleResult()

which makes constraints like "order by" unnecessary.

den-javamaniac
Your answer is correct, but the part 'which makes constraints like "order by" unnecessary' is irrelevant.
Bytecode Ninja
Yeah, obviously irrelevant in current scope.
den-javamaniac