我试图在JPA Criteria Query中创建以下查询:
SELECT V.* FROM VENDA V
WHERE
( SELECT COUNT(D.ID) FROM VENDADETALHE D WHERE D.ID IN
( SELECT CVD.DETALHES_ID FROM VENDA_VENDADETALHE CVD WHERE CVD.VENDA_ID = V.ID )
AND ( SELECT I.TIPO FROM ITEM I WHERE I.ID=D.IDITEM ) <> 'SERVICO' ) = 0
AND
( SELECT COUNT(D.ID) FROM VENDADETALHE D WHERE D.ID IN
( SELECT CVD.DETALHES_ID FROM VENDA_VENDADETALHE CVD WHERE CVD.VENDA_ID = V.ID )
AND ( SELECT I.TIPO FROM ITEM I WHERE I.ID=D.IDITEM ) = 'SERVICO' ) > 0
我有以下类结构
public class Venda {
/* other class attributes ( id & etc... ) */
@OneToMany( orphanRemoval=true, cascade = CascadeType.ALL, fetch =FetchType.LAZY )
@JoinTable
private List<VendaDetalhe> detalhes;
}
public class VendaDetalhe{
/* other class attributes */
@ManyToOne
@JoinColumn( name = "iditem", referencedColumnName = "id")
private Item item;
}
public class Item{
/* other class attributes */
@Enumerated( EnumType.STRING )
private ETipo tipo;
}
public enum ETipo{
PRODUTO,
SERVICO;
}
以下代码查询:
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Venda> qry = builder.createQuery( Venda.class );
Root<Venda> root = qry.from( Venda.class );
Join<Venda,VendaDetalhe> join = root.join( "detalhes", JoinType.INNER );
List<Predicate> p = new ArrayList<>();
p.add( builder.greatherThan( builder.count( builder.equal( join.get( "item" ).get("tipo"), ETipo.SERVICO ) ), 0L ) );
p.add( builder.equal( builder.count( builder.equal( join.get("item" ).get("tipo"),ETipo.PRODUTO) ), 0L) );
qry.where( p.toArray( new Predicate[ p.size() ] );
em.createQuery( qry ).getResultList();
但这会生成QuerySyntaxException
,表示预计会CLOSE
,但会找到=
。
我的CriteriaQuery语法是否正确?我在互联网上搜索了在WHERE子句中使用count
,但找不到任何内容。
异常消息如下:
org.hibernate.hql.internal.ast.QuerySyntaxException: expecting CLOSE, found '=' near line 1,
column 249
[select generatedAlias0 from
br.com.koinonia.habil.model.user.movimentacoes.compraevenda.Venda
as generatedAlias0 inner join generatedAlias0.detalhes as generatedAlias1 where
( generatedAlias0.empresa=:param0 ) and ( count(generatedAlias1.item.tipo=:param1)>0L )
and ( count(generatedAlias1.item.tipo=:param2)=0L )]
答案 0 :(得分:3)
Don't run a count query when you could run an exists query。即使您保持在SQL语言(或任何转换为SQL的语言,如JPQL)中,您的优化器也可能无法将COUNT(*) == 0
转换为NOT EXISTS()
并且COUNT(*) > 0
到EXISTS()
。想象一下,其中一项计数结果为100万。您真的需要数据库来确定完全计数值吗?或者,只要数据库知道是否存在(或没有)给定的行,数据库是否可以停止?
您的原始查询可以重写为:
SELECT V.*
FROM VENDA V
WHERE NOT EXISTS (
SELECT 1
FROM VENDADETALHE D
WHERE D.ID IN (
SELECT CVD.DETALHES_ID
FROM VENDA_VENDADETALHE CVD
WHERE CVD.VENDA_ID = V.ID
)
AND (
SELECT I.TIPO FROM ITEM I WHERE I.ID=D.IDITEM
) <> 'SERVICO'
)
AND EXISTS (
SELECT 1
FROM VENDADETALHE D
WHERE D.ID IN (
SELECT CVD.DETALHES_ID
FROM VENDA_VENDADETALHE CVD
WHERE CVD.VENDA_ID = V.ID
)
AND (
SELECT I.TIPO FROM ITEM I WHERE I.ID=D.IDITEM
) = 'SERVICO'
)
当然,与'SERVICO'
进行比较的相关子查询可以进一步转换为内部联接,但我不确定这是否会造成麻烦。
也许,您现在可以更轻松地使用JPQL编写,但为什么不通过EntityManager.createNativeQuery(String, Class)
运行SQL查询。由于您正在预测一个实体(V.*
),这样就可以了。
答案 1 :(得分:0)
count
QL对函数内部的内容显然非常挑剔,不能接受嵌套查询。所以这一行:
builder.count( builder.equal( join.get( "item" ).get("tipo"), ETipo.SERVICO ) )
需要稍微分开,以便equal
比较在其他地方,count
只在一个查询中调用。