如何使用命名查询获得计数,而不获取所有列表(我认为这会提高性能)。这是不起作用的命名查询:
@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