简体   繁体   中英

What best practices for value return method JPQL

I need to ask for a support, in this code I initially only need two fields, but I'm not sure how best to return the query values ​​to the method below if only the two fields or the entire object.

Thanks in advance

  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;

    }

I used to create a DTO like this:

public class StockxDepositDTO {

        private String deposit;
        private String typeDeposit;

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

So when you will get the data you can use this DTO instead resultset, like below:

"SELECT new com.stackoverflow.repository.StockxDepositDTO(c.bkLocationC, d.bkStockTypeC ) FROM ...."

In result that you will receive a list of StockxDepositDTO with two fields that you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM