Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

nested:iterate with hibernate objects  RSS feed

alan wamser
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two hibernate objects (JobApplication, JobApplicationComments). It's a one-to-many relationship. The JobApplication contains the following.

private Set jobApplicationComments = new HashSet(0);.

The JobApplicaitonComments has the following fields...

private Long commentsId;
private JobApplication jobApplication;
private String comments;
private Date createdDate;
private String createdBy;

I retreive a JobApplication from the database and can verify this using log4j. However when I use the nested:iterate I get the following error.

javax.servlet.ServletException: Invalid argument looking up property jobApplicationComments[0].comments of bean jobApplication

Here is the code from the jsp...

<nested resent name="jobApplication">
<nested:root name="jobApplication">
<nested:write property="firstName"/> <nested:write property="lastName" /><br>
<nested:iterate property="jobApplicationComments" >
<nested:write property="comments" />
</nested resent>

Here is the code in my action that I used to verify the correct application and comments.

Iterator myComments = (Iterator)jobApplication.getJobApplicationComments().iterator();

while (myComments.hasNext()) {
JobApplicationComments comments = (JobApplicationComments);
logger.debug("Comments: " + comments.getComments());

What am I missing???
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you're missing is an indexed getter for jobApplicationComments. When you use an indexed property in your JSP such as jobApplicationComments[0].comments , Struts must be able to get a single JobApplicationComment for a given index.

This becomes problematic for you, because you are storing these objects in a HashSet.

Here's a quote from the HashSet javadoc:

This class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time.

Since a HashSet does not have any concept of ordering and objects are not retrieved in any order, I can't think of a way to create an indexed getter for this object.

You may want to consider changing this collection to an implementation of the java.util.List interface, since implementors of this interface have a get(int index) method.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!