hello, is there a way in hql to map a result list to a List in a dto? here's an example that will show my question more clearly. assume we have a Person entity that has many Car entities.
@Entity public class Person { private long id; private String firstName; private String lastName; private String address; private Set<Car> cars = new HashSet<Car>(); @Id public long getId() { return id; } public void setId(long id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @OneToMany(fetch = FetchType.LAZY, mappedBy = "person") @Cascade(CascadeType.ALL) public Set<Car> getCars() { return cars; } public void setCars(Set<Car> cars) { this.cars = cars; } }
@Entity public class Car {
private long id; private String sn; private String color; private Person person; @Id public long getId() { return id; } public void setId(long id) { this.id = id; } public String getSn() { return sn; } public void setSn(String sn) { this.sn = sn; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } @JoinColumn(name = "person_id") @ManyToOne(fetch = FetchType.LAZY) public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } }
assume we also have the following PersonDto: public class PersonDto {
private String firstName; private List<Car> cars; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public List<Car> getCars() { return cars; } public void setCars(List<Car> cars) { this.cars = cars; } }
is there a way i can call one hql query that fetches all the personDtos in which the person's name starts with 'a' for example?
the following hql query returns 'Subquery returns more than 1 row' select p.firstName as firstName, (select c from Car c where c.person = p) as cars from Person p where p.firstName like 'a%'
I'm using the ResultTransformer to map the result to the dto [query.setResultTransformer(Transformers.aliasToBean(PersonDto.class)).list()]
thanks.
|