gpt4 book ai didi

java - JPA Eclipselink NamedQuery 与查询 COUNT

转载 作者:行者123 更新时间:2023-12-01 14:11:03 24 4
gpt4 key购买 nike

无法从 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 聚合中得到一个实体,而不是长整型或整数。

但是在这里我得到了一个整数。这是完全相同的查询。

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();

有人有想法吗?

最佳答案

简短回答:

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;

`另请参阅:http://www.objectdb.com/java/jpa/query/api

关于java - JPA Eclipselink NamedQuery 与查询 COUNT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18527607/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com