How to add, with Spring form, a new object to a List owned by the modelAttribute

90 views Asked by At

I have a object called Menu. It has a List of object called VoceMenu

public class Menu implements Serializable{          

    @Column(name="titolo")
    private String titolo;

    @OneToMany(mappedBy="menu", fetch=FetchType.EAGER)  
    @Cascade({CascadeType.SAVE_UPDATE}) 
    private List<VoceMenu> voceMenuList;
 }

public class VoceMenu implements Serializable{  
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name="id", nullable=false, updatable=false)
        private Integer id; 

        @Column(name="descrizione", nullable=false)
        private String descrizione; 
    }

This is my Spring form

<form:form modelAttribute="menu">

    <form:input path="titolo" maxlength="50"/>  

       //List of VoceMenu//////////////////////////////////////////////
       <form:hidden path="voceMenuList[${counter.index}].id"/>                      
       <form:input path="voceMenuList[${counter.index}].descrizione" />
       ////////////////////////////////////////////////////////////////

</form:form>

This form it's ok if I want to edit an existing VoceMenu, since any VoceMenu has, already, its own id.

But, this is not ok if I want to add a new VoceMenu, since, of course, voceMenuList[${counter.index}].id would be null.

Since I want to use jquery to multiply the part of the form to add a new VoceMenu, how should I handle this form? What should I write in the path attribute of the field id?

0

There are 0 answers