JPA Criterios de consulta Ruta.obtener left join es posible

Tengo una pregunta acerca de JPA criterios.

Aquí está mi JPA criterios de consulta:

 CriteriaBuilder criteriaBuilder = getEm().getCriteriaBuilder();
 CriteriaQuery<InventoryItemSumReport> query = criteriaBuilder.createQuery(InventoryItemSumReport.class);
 Root<InventoryItemDetail> from = query.from(InventoryItemDetail.class);    

 Join<InventoryItemDetail, InventoryItem> joinItem = from.join(InventoryItemDetail_.inventoryItem); 

 Predicate where = criteriaBuilder.lessThanOrEqualTo(from.get(InventoryItemDetail_.effectiveDate), date);

 query.multiselect(joinItem.get(InventoryItem_.product),joinItem.get(InventoryItem_.facility),joinItem.get(InventoryItem_.customer));
 query.groupBy(joinItem.get(InventoryItem_.product),joinItem.get(InventoryItem_.facility),joinItem.get(InventoryItem_.customer));
 query.where(where); 

 TypedQuery<InventoryItemSumReport> createQuery = getEm().createQuery(query);       
 List<InventoryItemSumReport> resultList = createQuery.getResultList();

Aquí es la consulta resultante producido por el JPA proveedor de:

    select
        inventoryi1_.PRODUCT_ID as col_0_0_,
        inventoryi1_.FACILITY_ID as col_1_0_,
        inventoryi1_.CUSTOMER_ID as col_2_0_ 
    from
        INVENTORY_ITEM_DETAIL inventoryi0_ 
    inner join
        INVENTORY_ITEM inventoryi1_ 
            on inventoryi0_.INVENTORY_ITEM_ID=inventoryi1_.ID 
    inner join
        PRODUCT product2_ 
            on inventoryi1_.PRODUCT_ID=product2_.ID 
    inner join
        FACILITY facility3_ 
            on inventoryi1_.FACILITY_ID=facility3_.ID 
    inner join
        CUSTOMER customer4_ 
            on inventoryi1_.CUSTOMER_ID=customer4_.ID 
    where
        inventoryi0_.EFFECTIVE_DATE<= ? 
    group by
        inventoryi1_.PRODUCT_ID ,
        inventoryi1_.FACILITY_ID ,
        inventoryi1_.CUSTOMER_ID

Pero me gustaría que la siguiente consulta:

    select
        inventoryi1_.PRODUCT_ID as col_0_0_,
        inventoryi1_.FACILITY_ID as col_1_0_,
        inventoryi1_.CUSTOMER_ID as col_2_0_ 
    from
        INVENTORY_ITEM_DETAIL inventoryi0_ 
    inner join
        INVENTORY_ITEM inventoryi1_ 
            on inventoryi0_.INVENTORY_ITEM_ID=inventoryi1_.ID 
    inner join
        PRODUCT product2_ 
            on inventoryi1_.PRODUCT_ID=product2_.ID 
    inner join
        FACILITY facility3_ 
            on inventoryi1_.FACILITY_ID=facility3_.ID 
    left join
        CUSTOMER customer4_ 
            on inventoryi1_.CUSTOMER_ID=customer4_.ID 
    where
        inventoryi0_.EFFECTIVE_DATE<= ?
    group by
        inventoryi1_.PRODUCT_ID ,
        inventoryi1_.FACILITY_ID ,
        inventoryi1_.CUSTOMER_ID

con un left join CLIENTE obtener también resultados donde Customers son nulos.
Customer, Product, Facility son todos entites, mientras que InventoryItemSumReport es un objeto de Valor o DTO.

public class InventoryItemSumReport implements Serializable {

   private static final long serialVersionUID = 1L;

   private Product product;
   private Facility facility;
   private Customer customer;

   public InventoryItemSumReport(Product product, Facility facility, Customer customer) {
       super();
       this.product = product;
       this.facility = facility;
       this.customer = customer;
   }
}

OriginalEl autor landal79 | 2012-04-13

1 Kommentar

  1. 8

    He encontrado de la siguiente manera funciona:

     CriteriaBuilder criteriaBuilder = getEm().getCriteriaBuilder();
     CriteriaQuery<InventoryItemSumReport> query = criteriaBuilder.createQuery(InventoryItemSumReport.class);
     Root<InventoryItemDetail> from = query.from(InventoryItemDetail.class);    
    
     Join<InventoryItemDetail, InventoryItem> joinItem = from.join(InventoryItemDetail_.inventoryItem); 
    
     Predicate where = criteriaBuilder.lessThanOrEqualTo(from.get(InventoryItemDetail_.effectiveDate), date);
    
     Join<InventoryItem, Customer> joinCustomer = joinItem.join(InventoryItem_.customer, JoinType.LEFT);
     query.multiselect(joinItem.get(InventoryItem_.product),joinItem.get(InventoryItem_.facility),joinItem.get(InventoryItem_.customer));
     query.groupBy(joinItem.get(InventoryItem_.product),joinItem.get(InventoryItem_.facility),joinCustomer);
     query.where(where); 
    
     TypedQuery<InventoryItemSumReport> createQuery = getEm().createQuery(query);       
     List<InventoryItemSumReport> resultList = createQuery.getResultList();

    OriginalEl autor

Kommentieren Sie den Artikel

Bitte geben Sie Ihren Kommentar ein!
Bitte geben Sie hier Ihren Namen ein

Pruebas en línea