0

I am using Play with Excel module 1.2.3. In a controller, I get a list of Students by calling a method defined in the model - Student:

List<Student> students= Student.findStudents();

findStudents() is defined as:

public static List<Student> findStudents() {
    List<Student> list =  Student.find("colA != colB").fetch();

    return list;
}

then I render the excel file by:

renderExcel("student_report");

Inside the excel template, I have used JXLS. For example:

<jx:forEach items="${students}" var="stu">
    ${stu.address.name}                    ${stu.name}
</jx:forEach>

Now, the weird thing happens. stu.name always get displayed fine. However, stu.address.name only get displayed when I have done something like System.out.println(student.address.name) in the code. Otherwise, the cell in the Excel report is blank.

Can anyone explain this?

N.B. Student lazily ref to address

xcoder
  • 1,135
  • 1
  • 14
  • 34

1 Answers1

0

Jxls uses Apache Jexl to process property expressions like stu.address.name. Jexl uses reflection to calculate the object property values. But reflection and lazy loading do not go along because you work not with a real object but with a proxy object.

When you do System.out.println(student.address.name) the real object is instantiated and the reflection works fine.

Possible solution to the issue is described in this answer Converting Hibernate proxy to real object. Or you just should do eager fetching whenever you need to pass the object to Jxls.

Community
  • 1
  • 1
Leonid Vysochyn
  • 1,194
  • 1
  • 6
  • 12