gpt4 book ai didi

java - 忽略 hibernate native 查询中的空列表

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

我有一个疑问

 String query = "Select * 
from brand brd
join product prd on brd.prd_id = prd.id
join price prc on prc.id = prd.prc_id";

if(!bo.brd.isEmpty() || !bo.prd.isEmpty())
query.append(" where " );
if(!bo.brd.isEmpty()) && !bo.prd.isEmpty())
query.append(brd.id in (:brdId) and prd.id in (:prdId));
else if(!bo.brd.isEmpty())
query.append(brd.id in (:brdId)) ;
else if(!bo.prd.isEmpty())
query.append( prd.id in (:prdId));

query.createNativeQuery(query)
.setParameterList("brdId", brdLst)
.setParameterList("prdId", prdLst)
.getResultList();

所有参数都是可选的。我正在使用 hibernate native 查询并使用 setParameterlist 设置所有参数。当列表为空或无法找到参数时,我会收到 brdId not found 或 prdId not found 。如何动态更改查询参数?

最佳答案

整个事情应该像下面这样

String query = "Select * 
from brand brd
join product prd on brd.prd_id = prd.id
join price prc on prc.id = prd.prc_id";
if(!bo.brd.isEmpty() || !bo.prd.isEmpty())
query.append(" where " );
if(!bo.brd.isEmpty()) && !bo.prd.isEmpty())
query.append(brd.id in (:brdId) and prd.id in (:prdId));
else if(!bo.brd.isEmpty())
query.append(brd.id in (:brdId)) ;
else if(!bo.prd.isEmpty())
query.append( prd.id in (:prdId));

if(!bo.brd.isEmpty()) && !bo.prd.isEmpty())
query.createNativeQuery(query).setParameterList("brdId",brdLst).setParameterList("prdId", prdLst);
else if(!bo.brd.isEmpty())
query.createNativeQuery(query).setParameterList("brdId", brdLst)
else if(!bo.prd.isEmpty())
query.createNativeQuery(query).setParameterList("prdId", prdLst)

是的,“查询”是字符串,它不会有 hibernate 方法。

关于java - 忽略 hibernate native 查询中的空列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58848362/

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