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