I read many posts, but I didn't find a solution.
I have an object called Utente.
Utente has a field called autorizzazioniLista
public class Utente{
...
@OneToMany(mappedBy = "utente", fetch = FetchType.EAGER)
@Cascade(CascadeType.ALL)
private List<Autorizzazioni> autorizzazioniLista;
...
}
Autorizzazioni has 3fields: id, utente and autorizzazione
public class Autorizzazioni {
...
@Id
@GeneratedValue
@Column(name = "id")
private int id;
@ManyToOne(targetEntity = Utente.class, fetch = FetchType.EAGER)
private Utente utente;
@ManyToOne(targetEntity = Autorizzazione.class, fetch = FetchType.EAGER)
private Autorizzazione autorizzazione;
...
}
and this is Autorizzazione
public class Autorizzazione{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id", nullable=false, updatable=false)
private int id;
@Column(name = "descrizione")
private String descrizione;
}
I have a form with Utente as modelAttribute. In this form, the objects Autorizzazioni of the field autorizzazioniLista must be shown as checkbox; here start my problems.
This is the jsp page that shows the checkboxes:
<form:checkboxes
items="${utente.autorizzazioniLista}"
path="autorizzazioniLista"
itemValue="id" delimiter="<br><br>"
itemLabel="autorizzazione.descrizione" />
When I submit the form and I retrieve the object Utente inside the controller, the objects Autorizzazioni inside the field autorizzazioniLista have their relative fields Utente and Autorizzazione set to null, so I'm not able to save them on the DB.
The only field of the objects Autorizzazioni that is not null is id, since inside the tag <form:checkboxes>
i set as itemValue
the id of the objects Autorizzazioni
NOTE: of course I use a class that extends PropertyEditorSupport
, in order to handle the objects Autorizzazioni inside the tag <form:checkboxes>
.
So, my final question is: before the object Utente is retrieved by the controller (the one called after the submit of the form), is there a way to manipulate the the fields of the objects Autorizzazioni?
Inside the controller (the one called after the submit of the form) I used @Valid Utente
, so the fields of the object Utente must have no errors.