Many databases are effective at returning the number of records in a table, but if you want to be creative, what about session.createQuery("select 1 from table").setMaxSize(1).list().isEmpty() ?
Or: session.createQuery("select 1 from table").setFetchSize(1).scroll(ScrollMode.FORWARD_ONLY).next() == null
I think this will depend on the database as to which method is the fastest.
source share