-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: hql to map a result list to a List in a dto
PostPosted: Mon Apr 19, 2010 3:00 am 
Newbie

Joined: Mon Apr 19, 2010 1:44 am
Posts: 2
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.