How to save many objects in a spring <form:form>
Asked Answered
A

1

6
@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]'

Amann answered 13/2, 2015 at 11:6 Comment(2)
Please could you post your Controller Code?Lemmueu
You original question has been answered. You should raise a new question for any subsequent issue which is caused by a totally separate issue.Vladimir
V
13

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>
Vladimir answered 13/2, 2015 at 17:49 Comment(5)
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 adviceAmann
Are you sure the Set you pass to the ArrayList constructor is not null?Vladimir
I edited this line of code private Set<VoceMenu> voceMenuList = new HashSet <VoceMenu>(); 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 voceMenuAsListAmann
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.Vladimir
All, May I know how to include setter for this approach? I see the values are not being added.Lean

© 2022 - 2024 — McMap. All rights reserved.