gpt4 book ai didi

java - JPA 标准 - 依赖 WHERE 子句

转载 作者:行者123 更新时间:2023-12-01 19:56:01 27 4
gpt4 key购买 nike

我正在尝试在 JPA Criteria 查询中创建以下查询:

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 )]

最佳答案

Don't run a count query when you could run an exists query 。即使您停留在 SQL 语言(或任何翻译为 SQL 的语言,例如 JPQL)中,您的优化器也可能无法识别转换 COUNT(*) == 0的可能性。 code> 到 NOT EXISTS()COUNT(*) > 0EXISTS()。想象一下其中一个计数结果为 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.*),因此这可以正常工作。

关于java - JPA 标准 - 依赖 WHERE 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49756918/

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