I have a problem with JEE projet, I have one ManyToMany relation but I dont know how to make it work in the form.
I have a Tache class with:
@Entity
public class Tache implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String nom;
@ManyToMany(fetch = FetchType.EAGER)
private List<Developpeur> lesDev;
public int getId() {return id;}
public void setId(int id) {this.id = id;}
public String getNom() {return nom;}
public void setNom(String nom) {this.nom = nom;}
public List<Developpeur> getLesDev() {return lesDev;}
public void setLesDev(List<Developpeur> lesDev) {this.lesDev = lesDev;}
}
A Developpeur class with:
@Entity
public class Developpeur implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String prenom;
private String nom;
@ManyToMany(mappedBy = "lesDev")
private List<Tache> lesTache;
public int getId() {return id;}
public void setId(int id) {this.id = id;}
public String getPrenom() {return prenom;}
public void setPrenom(String prenom) {this.prenom = prenom;}
public String getNom() {return nom;}
public void setNom(String nom) {this.nom = nom;}
public List<Tache> getLesTache() {return lesTache;}
public void setLesTache(List<Tache> lesTache) {this.lesTache = lesTache;}
TacheBean is the controller:
@Named
@RequestScoped
public class TacheBean {
private Tache uneTa = new Tache();
private List<Tache> lesTa;
@PersistenceContext
private EntityManager em;
@Resource
private UserTransaction tx;
public String validerEditer()
{
try{
tx.begin();
em.merge(uneTa);
tx.commit();
FacesMessage message = new FacesMessage("Tache éditée avec succès");
FacesContext.getCurrentInstance().addMessage(null, message);
} catch (Exception ex) {
Logger.getLogger(TacheBean.class.getName()).log(Level.SEVERE, null, ex);
}
return "tache";
}
And the view for the form:
<h:form>
<p:growl id="message_erreur" life="8000"/>
<div class="formulaire">
<h:inputHidden value="#{tacheBean.uneTa.id}"/>
<div class="titreformulaire">Editer/Ajouter tache <span class="alerte">Les champs notés * sont obligatoires</span></div>
<p class="label"><label>Nom *</label></p>
<p><p:inputText label="Nom" id="nom" value="#{tacheBean.uneTa.nom}" required="true" /></p>
<p><label>Developpeur(s) *</label></p>
<p><p:selectManyMenu id="basic" value="#{tacheBean.uneTa.lesDev}">
<f:selectItems value="#{developpeurBean.readAll()}" var="d" itemValue="#{d.id}" itemLabel="#{d}"/>
</p:selectManyMenu>
</p>
<p><p:commandButton value="Valider" action="#{tacheBean.validerEditer()}" styleClass="boutonvalider" ajax="false" update="message_erreur"/></p>
</div>
</h:form>
I am sure that the value of selectManyMenu is bad... I think I need to feed the developpeur list before the merge but I dont see how to do this :/ What should I do in the form and in the controller for it to work?
Aucun commentaire:
Enregistrer un commentaire