-->
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: ResultTransformer in Hibernate return null
PostPosted: Tue Jun 21, 2011 7:15 am 
Newbie

Joined: Tue Jun 21, 2011 7:09 am
Posts: 1
Hi guys,
I'm using ResultTransformer to select only particular properties from entity, just i don't need all properties from entity. But the problem i faced is when a property is "one-to-many". Here is a simple example.
Code:
@Entity
@Table(name = "STUDENT")
public class Student
{

private long studentId;
private String studentName;
private List<Phone> studentPhoneNumbers = new ArrayList<Phone>();

@Id
@GeneratedValue
@Column(name = "STUDENT_ID")
public long getStudentId()
{
  return this.studentId;
}
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "STUDENT_PHONE", joinColumns = {@JoinColumn(name = "STUDENT_ID")}, inverseJoinColumns = {@JoinColumn(name = "PHONE_ID")})
public List<Phone> getStudentPhoneNumbers()
{
  return this.studentPhoneNumbers;
}
@Column(name = "STUDENT_NAME", nullable = false, length = 100)
public String getStudentName()
{
  return this.studentName;
}


Here is the class used by ResultTransformer for storing the selected properties.

Code:
public class StudentDTO
{
private long m_studentId;
private List<Phone> m_studentPhoneNumbers = new ArrayList<Phone>();
..
constructors and getters and setters..


And finally the Criteria code

Code:
Criteria criteria = session.createCriteria(Student.class)
    .setProjection(Projections.projectionList()
      .add(Projections.property("studentId"), "m_studentId")
      .add(Projections.property("studentPhoneNumbers"), "m_studentPhoneNumbers"))
    .setResultTransformer(Transformers.aliasToBean(StudentDTO.class));


  List list = criteria.list();
  StudentDTO p = (StudentDTO) list.get(0);


So, after i get StudentDTO object , only studenId is available, studentPhoneNumber is null .. Does it mean ResultTransformer does not work with any relationships ? or my way is wrong Any suggestions?

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.