I have the following problem, and I don’t even know if it can be solved in principle. There is an entiti with a many-to-many relationship:
@Entity @Table(name = "cars", schema = "", catalog = "relationship") public class CarsEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id", nullable = false, insertable = true, updatable = true) private int id; @Column(name = "year", nullable = true, insertable = true, updatable = true) private Integer year; @Column(name = "model", nullable = true, insertable = true, updatable = true, length = 50) private String model; @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER) @Fetch(FetchMode.SELECT) @JoinTable(name = "employee_car", joinColumns = @JoinColumn(name = "car_id"), inverseJoinColumns = @JoinColumn(name = "employee_id")) private Set<EmployeeEntity> employeeSet; ... @Entity @Table(name = "employee", schema = "", catalog = "relationship") public class EmployeeEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id", nullable = false, insertable = true, updatable = true) private int id; @Column(name = "firstName", nullable = true, insertable = true, updatable = true, length = 200) private String firstName; @Column(name = "lastName", nullable = true, insertable = true, updatable = true, length = 200) private String lastName; @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER) @Fetch(FetchMode.SELECT) @JoinTable(name = "employee_car", joinColumns = @JoinColumn(name = "employee_id"), inverseJoinColumns = @JoinColumn(name = "car_id")) private Set<CarsEntity> cars; ... and there is a DTO in which I would like this to be able to:
public class EmployeeWithCarsDTO { private int employeeId; private String employeeName; private String employeeLastName; private Set<CarsEntity> cars; Here is this Criteria:
public List <EmployeeEntity> showEmployeesCars2 () { List<EmployeeEntity> list = null; Criteria crit = session.createCriteria(EmployeeEntity.class); list = crit.list(); return list; } returns
Employee{id=16, firstName='Vasya', lastName='Vasyatin', cars={CarsEntity{id=5, year=2005, model='vaz21099'}, CarsEntity{id=6, year=2015, model='calina'} }} Employee{id=17, firstName='Petya', lastName='Petrov', cars={CarsEntity{id=8, year=1989, model='Moskvich'}, CarsEntity{id=7, year=1991, model='Tavria'} }} Employee{id=18, firstName='Ivan', lastName='Ivanov', cars={CarsEntity{id=10, year=2015, model='Lanos'}, CarsEntity{id=9, year=1986, model='gaz66'}}} And this is what I need, but I want to get it in the DTO. With this Criteria
public List <EmployeeWithCarsDTO> showEmployeesCars () { List<EmployeeWithCarsDTO> list = null; Criteria crit = session.createCriteria(EmployeeEntity.class, "empl") .createAlias("empl.cars", "cars") .setProjection(Projections.projectionList() .add(Projections.property("cars"), "cars") .add(Projections.property("empl.id"), "employeeId") .add(Projections.property("empl.firstName"), "employeeName") .add(Projections.property("empl.lastName"), "employeeLastName") ).setResultTransformer(Transformers.aliasToBean(EmployeeWithCarsDTO.class)); list = crit.list(); return list; } returns
EmployeeWithCarsDTO{employeeId=16, employeeName='Vasya', employeeLastName='Vasyatin', cars={}} EmployeeWithCarsDTO{employeeId=16, employeeName='Vasya', employeeLastName='Vasyatin', cars={}} EmployeeWithCarsDTO{employeeId=17, employeeName='Petya', employeeLastName='Petrov', cars={}} EmployeeWithCarsDTO{employeeId=17, employeeName='Petya', employeeLastName='Petrov', cars={}} EmployeeWithCarsDTO{employeeId=18, employeeName='Ivan', employeeLastName='Ivanov', cars={}} EmployeeWithCarsDTO{employeeId=18, employeeName='Ivan', employeeLastName='Ivanov', cars={}} Tell someone, is it even possible to get to DTO, then objects with Set cars. If so, in which direction to dig, otherwise I'm just at a dead end.