Note that there are some explanatory texts on larger screens.

plurals
  1. POResultTransformer in Hibernate return null
    text
    copied!<p>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.</p> <pre><code>@Entity @Table(name = "STUDENT") public class Student { private long studentId; private String studentName; private List&lt;Phone&gt; studentPhoneNumbers = new ArrayList&lt;Phone&gt;(); @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&lt;Phone&gt; getStudentPhoneNumbers() { return this.studentPhoneNumbers; } @Column(name = "STUDENT_NAME", nullable = false, length = 100) public String getStudentName() { return this.studentName; } </code></pre> <p>Here is the class used by ResultTransformer for storing the selected properties.</p> <pre><code>public class StudentDTO { private long m_studentId; private List&lt;Phone&gt; m_studentPhoneNumbers = new ArrayList&lt;Phone&gt;(); .. constructors and getters and setters.. </code></pre> <p>And finally the Criteria code</p> <pre><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); </code></pre> <p>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?</p> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload