-->
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.  [ 9 posts ] 
Author Message
 Post subject: AliasToBeanResultTransformer doesn't create a child?
PostPosted: Wed Nov 29, 2006 7:49 am 
Newbie

Joined: Tue Nov 28, 2006 9:34 am
Posts: 15
Location: Brasil
With some code like this:
Code:
c.createAlias("course", "course", CriteriaSpecification.LEFT_JOIN)
c.setResultTransformer(new AliasToBeanResultTransformer(Student.class));

Then i need a projection like this:
Code:
pl.add(Projections.property("course.description"), "course.description");

It shouldn't create an instance of Course inside Student with the description??

Because is returning:
Could not find setter for course.description on class test.Student


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 29, 2006 8:08 am 
Senior
Senior

Joined: Mon Oct 23, 2006 5:12 am
Posts: 141
Location: Galicia, Spain
If Student.java has and "courseDescription" property you should use:

pl.add(Projections.property("course.description"), "coursDescription");

Look at this example:

List resultWithAliasedBean = s.createCriteria(Enrolment.class)
.createAlias("student", "st")
.createAlias("course", "co")
.setProjection( Projections.projectionList()
.add( Projections.property("co.description"), "courseDescription" )
)
.setResultTransformer( new AliasToBeanResultTransformer(StudentDTO.class) )
.list();

StudentDTO dto = (StudentDTO)resultWithAliasedBean.get(0);

_________________
andresgr (--don't forget to rate)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 29, 2006 8:30 am 
Newbie

Joined: Tue Nov 28, 2006 9:34 am
Posts: 15
Location: Brasil
But no.... Student has an Object, Course, and this Course has a description (and an Id, etc)...
How can I transform it? :/

Code:
public class Student {
   private Integer id;
   private String name;
   private Course course;
}
public class Course {
   private Integer id;
   private String description;
}


Thanks in advance... :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 29, 2006 8:43 am 
Senior
Senior

Joined: Mon Oct 23, 2006 5:12 am
Posts: 141
Location: Galicia, Spain
Maybe what you need, then, is something like

c.createAlias("student", "student")
.createAlias("course", "course")
.setProjection( Projections.projectionList()
.add( Projections.property("course"), "course" )
)
.setResultTransformer( new AliasToBeanResultTransformer(StudentDTO.class) )
.list();

StudentDTO dto = (StudentDTO)resultWithAliasedBean.get(0);

Anyway, please post your POJOs and mappings and the results you want to achieve, your question is not very clear...

_________________
andresgr (--don't forget to rate)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 29, 2006 9:38 am 
Newbie

Joined: Tue Nov 28, 2006 9:34 am
Posts: 15
Location: Brasil
Sorry... i was trying to be short and direct to the point... but... here goes the code...

Student POJO
Code:
public class Student {
   private Integer id;
   private Course course;
   private String name;
}


Course POJO
Code:
public class Course {
   private Integer id;
   private String description;
}


TestDAO
Code:
   Criteria c = SessionManager.getSession().createCriteria(Student.class);
   c.createAlias("course", "course", CriteriaSpecification.LEFT_JOIN);
   ProjectionList pl = Projections.projectionList();
   pl.add(Projections.property("id"), "id");
   pl.add(Projections.property("name"), "name");
   pl.add(Projections.property("course.description"), "course.description");
   c.setProjection(pl);
   c.setResultTransformer(new AliasToBeanResultTransformer(Student.class));


Now... the point is, I want to bring just what I need, because in my real application I have a lot of fields (properties) in the POJOs (both of them)...

Thanks again!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 29, 2006 11:18 am 
Senior
Senior

Joined: Mon Oct 23, 2006 5:12 am
Posts: 141
Location: Galicia, Spain
So... if i understand you what you want is to retrieve

studendt.id
student.name
course.description

only?

Then you need a StudentDTO.java like this:

class StudentDTO{
private Integer id;
private String courseDescription;
private String name;
}
and use the following projections:

Criteria c = SessionManager.getSession().createCriteria(Student.class);
c.createAlias("course", "course", CriteriaSpecification.LEFT_JOIN);
ProjectionList pl = Projections.projectionList();
pl.add(Projections.property("id"), "id");
pl.add(Projections.property("name"), "name");
pl.add(Projections.property("course.description"), "course.description");
c.setProjection(pl);
c.setResultTransformer(new AliasToBeanResultTransformer(StudentDTO.class));

_________________
andresgr (--don't forget to rate)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 29, 2006 1:08 pm 
Newbie

Joined: Tue Nov 28, 2006 9:34 am
Posts: 15
Location: Brasil
Same error:
Could not find setter for course.description on class teste.StudentDTO

But, if i change to this:
pl.add(Projections.property("course.description"), "courseDescription");

It works as i thought...

But I wouldn't want to change my DTOs... Maybe in another time I would like to retrieve another property of Course, and them I would have to add it to StudentDTO...
And this is the subject of my post: Doesn't AliasToBeanResultTransformer create a child?
Meaning... Why this transformer doesn't create a Course inside of Student with the fields that I want to retrieve?

Thank you very very much again!!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 29, 2006 1:30 pm 
Senior
Senior

Joined: Mon Oct 23, 2006 5:12 am
Posts: 141
Location: Galicia, Spain
Quote:
But, if i change to this:
pl.add(Projections.property("course.description"), "courseDescription");


Yes, sorry, i typed it wrong before

Quote:
And this is the subject of my post: Doesn't AliasToBeanResultTransformer create a child?
Meaning... Why this transformer doesn't create a Course inside of Student with the fields that I want to retrieve?


AliasToBeanResultTransformer only works with raw "properties". I mean, he will only call "setXXXXX()'s" one your Bean.
Quote:
Thank you very very much again!!


You can thank me rating a couple of my answers O:)

_________________
andresgr (--don't forget to rate)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 29, 2006 2:24 pm 
Newbie

Joined: Tue Nov 28, 2006 9:34 am
Posts: 15
Location: Brasil
The maximum number of credit allotments for this topic has been reached.

But... thank you again! I'll code myself Transformer then... :D


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

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.