开发者

How do I check for the existance of at least one record matching a given set of filters?

Right now i have a hibernate model on which i set a filter, then load it. I just need to perform different logic if there is at least one record found, vs if there are no records found.

The problem is that loading all matching records can be very time consuming and inefficient. Is t开发者_StackOverflowhere a way to load the first model found only, or do something in hql which will simply just tell me if there is a record matching my filters?


You can use projections and count some indexed column.


From the Hibernate Manual:

You can count the number of query results without returning them:

( (Integer) session.createQuery("select count(*) from ....").iterate().next() ).intValue()

If you have a filter set on the model, it will be applied to this query as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜