gpt4 book ai didi

java - 值(value)返回方法JPQL的最佳实践是什么

转载 作者:行者123 更新时间:2023-12-02 03:51:20 28 4
gpt4 key购买 nike

我需要寻求支持,在这段代码中我最初只需要两个字段,但我不确定如果只有两个字段或整个对象,如何最好地将查询值返回到下面的方法。

提前致谢

  public LocalDE localizaPorItemstockdeposit(   final BigInteger ItemId, 
final BigInteger stockId,
final BigInteger depositId){

StringBuilder sql = new StringBuilder();

sql.append(" SELECT ")
.append(" c.bkLocationC, ")
.append(" d.bkStockTypeC ")
.append(" FROM LocalDE c ")
.append(" JOIN a.partsXWarehouseDE b ")
.append(" JOIN a.depositDE c ")
.append(" JOIN a.TypedepositDE d ")
.append(" WHERE ")
.append(" a.ItemId = :ItemId AND")
.append(" a.stockId = :stockId AND")
.append(" a.bkdepositId = :depositId ");

TypedQuery<LocalDE> query = this.entityManager().createQuery(sql.toString(), LocalDE.class);

query.setParameter("ItemId", ItemId);
query.setParameter("stockId", stockId);
query.setParameter("depositId", depositId);

@SuppressWarnings("unchecked")
List lstitemxStockxDeposit = query.getResultList();
java.util.Iterator it = lstitemxStockxDeposit.iterator();

LocalDE itemxStockxDeposit = new LocalDE();
depositDE deposit = new depositDE();
TypedepositDE typeDeposit = new TypedepositDE();

while(it.hasNext()) {
Object[] resultitemxStockxDeposit =(Object[]) it.next();

deposit.setDepositDesc(String.valueOf(resultitemxStockxDeposit[0]));
typeDeposit.setTypeDepositDEsc(String.valueOf(resultitemxStockxDeposit[1]));

itemxStockxDeposit.setDepositDE(deposit);
itemxStockxDeposit.setTypeDepositDE(typeDeposit);
}

return itemxStockxDeposit;

}

最佳答案

我曾经创建这样的 DTO:

public class StockxDepositDTO {

private String deposit;
private String typeDeposit;

//with a constructor
public StockxDepositDTO(String deposit, String typeDeposit){
this.deposit = deposit;
this.typeDeposit = typeDeposit;
}
}

因此,当您获取数据时,可以使用此 DTO 代替结果集,如下所示:

“从...中选择新的 com.stackoverflow.repository.StockxDepositDTO(c.bkLocationC, d.bkStockTypeC )”

结果,您将收到一个 StockxDepositDTO 列表,其中包含您需要的两个字段。

关于java - 值(value)返回方法JPQL的最佳实践是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56778443/

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