从一侧选择时,JPA内部连接ManyToOne关系

时间:2018-10-06 09:16:15

标签: java jpa orm

我使用的方法是CriteriaBuilder。我有几张彼此相关的表。我有3个表,分别为ABC。假设表B中的表A中有一个外键,表C中的表B中也有另一个外键。所以我有这样的查询:

        CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
        CriteriaQuery<ResultObject> query = criteriaBuilder.createQuery(ResultObject.class);
        Root<A> rootA = query.from(A.class);
        Join<A, B> joinB = root.join(A_.bId , JoinType.INNER);
        Root<C> rootC = query.from(C.class);
        query.where(criteriaBuilder.equal(joinB.get(B_.field) , rootC.get(C_.bId)));

这可以完成工作,但是会在B和C之间创建交叉联接查询。如何将其转换为内部联接?

更新

这是我的总部,一切正常。我需要一个等效的criteria builder之一:

select
    new com.shaheri.financial.ReportParam(function('TRUNC',
    contract.creationDate),
    min(price.currencyCode),
    count(price.id),
    sum(price.totalPrice),
    sum(price.userRound),
    sum(price.userMarkup),
    sum(price.userDiscount),
    sum(price.userCommission),
    sum(price.tax),
    sum(price.surcharge),
    sum(price.basePrice),
    sum(price.agencyRound),
    sum(price.agencyMarkup),
    sum(price.agencyDiscount),
    sum(price.agencyCommission)) 
from
    Price as price  
inner join
    price.proforma as proforma 
inner join
    proforma.contract as contract 
inner join
    Voucher as voucher 
        on voucher.contract = contract 
inner join
    ServiceVoucher as serviceVoucher 
        on serviceVoucher.contract = contract 
inner join
    FlightVoucher as flightVoucher 
        on flightVoucher.contract = contract  
where
    (
        contract.creationDate>=:param0     
    )     
    and (
        contract.creationDate<=:param1     
    )     
    and (
        price.originPrice is not null     
    )     
    and (
        (
            flightVoucher.agencySupplier in (
                36L             
            )         
        )         
        or (
            voucher.agencySupplier in (
                36L             
            )         
        )         
        or (
            serviceVoucher.agencySupplier in (
                36L             
            )         
        )     
    )     
    and (
        contract.user=4L     
    ) 
group by
    function('TRUNC',
    contract.creationDate)

0 个答案:

没有答案