无法从NamedQuery获取整数???
@NamedQuery( name = Comm.COUNTLIKEVALUEINHOLDER,
query = "SELECT COUNT(c.id) " +
"FROM Comm c " +
"JOIN c.element e " +
"WHERE c.search1 LIKE :search1 " +
"AND c.search2 = :search2 " +
"AND c.type = :type " +
"AND e.holderelement = :holder"
)
Object q = em.createNamedQuery(Comm.COUNTLIKEVALUEINHOLDER)
.setParameter("search1", "bla%")
.setParameter("search2", "bla2")
.setParameter("type", t)
.setParameter("holder", h)
.getSingleResult();
我从COUNT Aggregat返回实体,而不是Long或Integer。
但是我在这里得到一个整数。这是完全相同的查询。
Object i = em.createQuery("SELECT COUNT(c.id) " +
"FROM Comm c " +
"JOIN c.element e " +
"WHERE c.search1 LIKE :search1 " +
"AND c.search2 = :search2 " +
"AND c.type = :type " +
"AND e.holderelement = :holder")
.setParameter("search1", "bla%")
.setParameter("search2", "bla2")
.setParameter("type", t)
.setParameter("holder", h)
.getSingleResult();
有人有想法吗?
答案 0 :(得分:1)
简短回答:
em.createQuery用于创建动态查询。返回类型由QL语言的返回类型决定(上例中的“SELECT COUNT(c.id)”。)
em.createNamedQuery用于创建静态查询。返回类型可以是类型化的,例如:
Object q = em.createNamedQuery(Comm.COUNTLIKEVALUEINHOLDER)
.setParameter("search1", "bla%")
.setParameter("search2", "bla2")
.setParameter("type", t)
.setParameter("holder", h)
.getSingleResult();
Integer count = (Integer) q;