5
@Component
@Entity
@Table(name="menu")
@Configurable
public class Menu implements Serializable{      
    ....        
    @OneToMany(mappedBy="menu", fetch=FetchType.EAGER)
    private Set<VoceMenu> voceMenuList; 

    public Set<VoceMenu> getVoceMenuList() {
        return voceMenuList;
    }

    public void setVoceMenuList(Set<VoceMenu> voceMenuList) {
        this.voceMenuList = voceMenuList;
    }
    .....   
}

I print a form to edit the menu, and its relative VoceMenu objects, this way:

<form:form action="editMenu" method="post" commandName="menu"> 
     Menu id<form:input path="id" maxlength="11"/><br/>       
     ...... 
    <c:forEach items="${menu.voceMenuList}" varStatus="counter">            
        <form:input path="voceMenuList[${counter.index}].id" maxlength="11"/>
             .....
    </c:forEach>
    <input type="submit">
</form:form>

But, when I try to save the object Menu, I get this error:

Invalid property 'voceMenuList[0]' of bean class [com.springgestioneerrori.model.Menu]: Cannot get element with index 0 from Set of size 0, accessed using property path 'voceMenuList[0]'

Alan Hay
  • 22,665
  • 4
  • 56
  • 110
MDP
  • 4,177
  • 21
  • 63
  • 119

1 Answers1

12

The elements of a Set cannot be accessed by index. You will need to add methods which return a List wrapping your set.

@Component
@Entity
@Table(name="menu")
@Configurable
public class Menu implements Serializable{      
    ....        
    @OneToMany(mappedBy="menu", fetch=FetchType.EAGER)
    private Set<VoceMenu> voceMenus; 

    public Set<VoceMenu> getVoceMenus() {
        return voceMenus;
    }

    public void setVoceMenus(Set<VoceMenu> voceMenus) {
        this.voceMenus = voceMenus;
    }

    //bind to this
    public List<VoceMenu> getVoceMenusAsList(){
        return new ArrayList<VoceMenu>(voceMenus);
    }
    .....   
}

JSP:

<form:form action="editMenu" method="post" commandName="menu"> 
     Menu id<form:input path="id" maxlength="11"/><br/>       
     ...... 
    <c:forEach items="${menu.voceMenusAsList}" varStatus="counter">            
        <form:input path="voceMenusAsList[${counter.index}].id" maxlength="11"/>
             .....
    </c:forEach>
    <input type="submit">
</form:form>
Alan Hay
  • 22,665
  • 4
  • 56
  • 110
  • I edited my code according to your help. Now I get a different error. I don't know if i didn't follow correctly your advice – MDP Feb 16 '15 at 13:38
  • Are you sure the Set you pass to the ArrayList constructor is not null? – Alan Hay Feb 16 '15 at 13:49
  • I edited this line of code private Set voceMenuList = new HashSet (); Now I don't get any error, but the field voceMenuList of the object Menu has always size 0. I'm wondering if I must add a set method for voceMenuAsList – MDP Feb 16 '15 at 14:02
  • The Set you are expecting to be populated by JPA layer is null which is why it fails. By adding this statement you are preventing it being null. Why it is not being populated from your JPA tier is a separate issue. If, in the get as list method, you manually add some VoceMenu items then that should confirm what you have works as expected and the issue is in JPA tier. If so then your original issue is fixed and you should mark the answer as accepted and raise a new question around the JPA issue. – Alan Hay Feb 16 '15 at 14:13
  • All, May I know how to include setter for this approach? I see the values are not being added. – Under_stand Mar 22 '21 at 11:14