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;
}