Wednesday, September 18, 2013

Design best practice: How to display in JSP an object with partial collection needed, for application that uses Hibernate?

Design best practice: How to display in JSP an object with partial
collection needed, for application that uses Hibernate?

I have an object Master that has a collection of Student objects. I have a
list of Master objects and I would want to display in the JSP the Master
fields and only the Students that have a score greater than some constant.
I cannot partially load a collection to have only the desired students
because Hibernate does not function like this. I can only filter it in
another collection but I won't have the collection bound to the Master
object for displaying this in the JSP.
My solution for displaying this in the JSP is to use an Wrapper object and
create instances after loading the data from the DB. After that I can use
the object to display it in the JSP:
Class WrapperMaster{
Master master;
Set<Student> filteredStudents;
}
Questions:
Won't this be just a silly and maybe reduntant thing to do?
I will have encapsulated persistent objects contained in another object
(that is unpersistent). Is this a best practice?
An alternative would be to not use the Wrapper object, but filter manually
the collection of students from the Master object in the JSP. I use
Struts2 so I would have to use many <s:if> tags.

No comments:

Post a Comment