1

I'm trying to write a Hibernate Criteria API equivalent of the following SQL query:

select c.NAME         as carName,
   cc.COLOR_CODE      as colorCode,
   cc.COLOR           as color,
   c.DESCRIPTION      as desc,
   c.MANUFACTURE_YEAR as year
from CAR c
     LEFT JOIN CAR_COLOR CC on c.COLOR_CODE_ID = CC.ID
WHERE CC.COLOR_CODE = ?

This is my Car entity

public class Car extends BaseEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;

    @Basic
    @Column(name = "MANUFACTURE_YEAR")
    private String year;

    @Basic
    @Column(name = "DESCRIPTION")
    private String desc;
    private Long colorCodeId;
    private String manufacturer;
}

and this is my CarColor entity

public class CarColor extends BaseEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String color;
    private String colorCode;
}

Car.colorCodeId is a foreign key which references CarColor.id. Although there is no apparent entity-level relationship and I cannot add any relationships.

I want to do a left join between these two tables/entities and map the result to CarSearch? How would I do this? Much appreciated.

public class CarSearch {
    private String carName;
    private String colorCode;
    private String color;
    private String desc;
    private String year;
}
6
  • Why you can not add any relationships to your domain model? Usually to be efficient, it should be designed as close as possible to you database tables structure.
    – SternK
    Mar 24, 2020 at 9:01
  • @SternK I suggested them to add relationships, they don't want to! :( Mar 24, 2020 at 9:03
  • Using criteria api as well as jpql/hql assume that you have all necessary relationship in your domain model. If you don't have them and you can not add them the only way is using a native query and try to map the output result to the dto classes.
    – SternK
    Mar 24, 2020 at 9:10
  • I have a native query and it works fine. However, they specifically want the Criteria API equivalent. Mar 24, 2020 at 9:12
  • They actually want to incrementally build the query - depending on if the where clause expression values are null or otherwise. For example, building a dynamic query based on fields that a user fills at runtime in a form that contains many optional fields. Mar 24, 2020 at 9:16

1 Answer 1

5

Cartesian product can be used

CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Object[]> query = cb.createQuery(Object[].class);
Root<Car> car = query.from(Car.class);
Root<CarColor> carColor = query.from(CarColor.class);

Predicate joinPredicate = cb.equal(car.get("colorCodeId"), carColor.get("id"));

query.multiselect(
    car.get("name"),
    carColor.get("colorCode"),
    carColor.get("color"),
    car.get("desc"),
    car.get("year")
).where(
    joinPredicate, 
    cb.equal(carColor.get("colorCode"), "yourColorCode")
);

To avoid Object[] in query result you should create QueryResultDto class with appropriate constructor and use this way

CriteriaQuery<QueryResultDto> query = cb.createQuery(QueryResultDto.class);
//...
query.select(cb.construct(QueryResultDto.class, 
    car.get("name"),
    carColor.get("colorCode"),
    carColor.get("color"),
    car.get("desc"),
    car.get("year")
));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.