使用criteriabuilder和谓词java的多表联接

时间:2019-05-20 17:44:03

标签: java left-join eclipselink criteria-api

我有这个查询要在Java和eclipselink应用程序中与Predicates和CriteriaBuilder一起执行

SELECT * from exaCar, exaCity, exaGeoLocation,exaMappingLocationCar
where exaCar.id = exaMappingLocationCar.car_id
and exaMappingLocationCar.location_id=exaGeoLocation.id
and exaGeoLocation.parentgeolocation_id=exaCity.location_id
and exaMappingLocationCar.active
and exaCar.active
and exaGeoLocation.active
and exaCity.id=:id_city
and exaCar.status =:Status .....etc

我设法使工作成为查询的第一部分,但我在联接上受阻。

表car / geolocation /城市/ locationCarMapping和地理位置中,我必须选择ID为ID的所有行,父代ID = city geolocation_ID

代码:

 public List<Car> findByFilterAdmin(CarFilterAdmin filter){

    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<Car> cq = cb.createQuery(Car.class);
    Root<Car> e = cq.from(Car.class);

    List<Predicate> predicates=setCarFilterAdminPredicate(cb,e,filter);
    if (!predicates.isEmpty()) {
        cq.where(cb.and(predicates.toArray(new Predicate[0])));
    }

    DatabaseUtil.applySort(cb, cq, e, filter.getSort(), filter.getDirection());

    // set list here, we set it in result data with jsonConverter in controller
    return getTypedQueryWithResultList(getEntityManager().createQuery(cq), filter.getPage(), filter.getNbItem())
            .getResultList();
}

public List<Predicate> setCarFilterAdminPredicate(CriteriaBuilder cb, Root<Car> root,CarFilterAdmin filter){
    List<Predicate> predicates=new ArrayList<>();

    if(filter.getId()!=null) {
        predicates.add(cb.equal(root.get("id"), filter.getId()));
    }
    if(StringUtils.isBlank(filter.getIdentification())) {
        predicates.add(cb.equal(root.get(Car.PROP_IDENTIFICATION), filter.getIdentification()));
    }
    if(filter.getActive()!=null) {
        predicates.add(cb.equal(root.get(Car.PROP_ACTIVE), filter.getActive()));
    }
    if(filter.getCarStatus()!=null) {
        predicates.add(cb.equal(root.get(Car.PROP_CAR_STATUS), filter.getCarStatus()));
    }
    if(filter.getCarCategoryId()!=null) {
        predicates.add(cb.equal(root.get(Car.PROP_CAR_TYPE).get("carCategory").get("id"), filter.getCarCategoryId()));
    }
    if(filter.getCarTypeId()!=null) {
        predicates.add(cb.equal(root.get(Car.PROP_CAR_TYPE).get("id"), filter.getCarTypeId()));
    }

    if(filter.getCitId()!=null) {
        predicates.add(cb.equal(root.join(Car_.).join(),filter.getCitId()));
        //predicates.add(cb.equal(root.join("locationCarMapping",JoinType.LEFT).get("id"),root.get("id")));
        //Join<Car,LocationCarMapping> join1=root.join("locationCarMapping",JoinType.LEFT).get("id"),root.get("id");
        //Join<LocationCarMapping, GeoLocation> associate = join1.join(join1,"geoLocation");
        //join1.on(cb.equal(join1.get("id"), root.get("id")));
        //predicates.add(Join)
    }

    return predicates;
}

此外,实体类对象之间没有双向链接

此locationCarMapping类

public LocationCarMapping() {
    super();
}

public LocationCarMapping(Long car, Long location) {
    this.car = car;
    this.location = location;
}

//@Column(name = "LOCATION_ID")
@JoinColumn(name = "LOCATION_ID", referencedColumnName = "ID")
private Long location;

//@Column(name = "CAR_ID")
@JoinColumn(name = "CAR_ID", referencedColumnName = "ID")
private Long car;

private Date endDate;

在地理位置和汽车中均未引用此类

有什么主意吗?我搜索了所有教程,甚至在oracle上搜索,但没有什么复杂的内容,您是否认为仅用一个谓词查询就可以实现这一目标?

0 个答案:

没有答案