JPA 计数 NamedQuery
2022-09-01 10:44:46
如何使用命名查询获取计数,而无需获取所有列表(我认为这将提高性能)。这是不起作用的命名查询:
@NamedQuery(name = "Charakteristika.findAllCount", query = "SELECT COUNT(c) FROM Charakteristika c")
执行此操作:
System.out.println("a");
System.out.println(em.createNamedQuery("Charakteristika.findAllCount", Integer.class).getSingleResult().intValue());
System.out.println("b");
输出:
a
尽管此查询有效:
@NamedQuery(name = "Charakteristika.findAll", query = "SELECT c FROM Charakteristika c")
执行此操作:
System.out.println("a");
System.out.println(em.createNamedQuery("Charakteristika.findAll", Charakteristika.class).getResultList().size());
System.out.println("b");
输出:
a
11111
b