使用hibernate检查数据库表中某些记录的最快方法

时间:2012-04-26 16:08:44

标签: java hibernate query-optimization

我有使用hibernate orm与db一起工作的java应用程序?那么什么是在db

中检查记录(映射到hibernate对象)的presense的最快方法

1 个答案:

答案 0 :(得分:5)

使用entityManager.find

docs的示例:

long catId = 1234L;
em.find( Cat.class, new Long(catId) );

或者,计算记录:

Integer count = (Integer) session.createQuery("select count(*) from Cats c where c.id = :catId")
                          .setLong("catId", 1234L)
                          .uniqueResult();
boolean exists = count > 0;