Je n'arrive pas à insérer une clé primaire composée dans Hibernate
Bonjour,
J'espère vraiment que vous m'aidiez sur ce point là parce que ça fait plus d'une semaine que je cherche la solution mais en vain.
Bon, j'ai deux tables; affectation et article et ils sont reliez par une association affecter qui a un attribut de plus : quantite.
J'ai généré le mapping et les classe POJO automatiquement depuis netBeans. J'ai obtenu alors deux 4 classes : affectation, article, Affecter et AffecterId. Les deux dernières classes ont un seul fichier de mapping : Affecter.hbm.xml
Le problème est quand je veux insérer dans Affecter j'obtiens toujours une erreur : java.lang.classCastException.
addAffectation.xhtml
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<head>
<title>TODO supply a title</title>
<script>
function confirmer() {
return confirm("Voulez-vous vraiment supprimer cette ligne?");
}
</script>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<body>
<!-- CHOISIR LA DIVISION :-->
<h:form id="division">
<table border="1px solid black">
<tr>
<td><h:outputLabel value="Choisir la division : " for="divis"></h:outputLabel></td>
<td><h:selectOneMenu value="#{affectationBean.entite}" id="divis" style="width: 100%">
<f:selectItem itemValue="" itemLabel=""></f:selectItem>
<f:selectItems var='o' itemValue="#{o.idEntiter}" itemLabel="#{o.service}" value="#{affectationBean.listDivision}"></f:selectItems>
</h:selectOneMenu>
</td>
</tr>
<tr>
<td><h:outputLabel value="Date : " for="date"></h:outputLabel></td>
<td><h:inputText value="#{affectationBean.date}" id='date'></h:inputText></td>
</tr>
<tr>
<td colspan="2"><center><h:commandButton value="Choisir" actionListener="#{affectationBean.addAffectation}" ></h:commandButton></center></td>
</tr>
</table>
</h:form>
<!-- CHOISIR LES ARTICLES : -->
<h:form id="article">
<table border="1px solid black">
<tr>
<td><h:outputLabel value="Sélectionner un article : "></h:outputLabel></td>
<td>
<h:selectOneMenu value="#{affectationBean.article}" id="art" converter="articleConverter" style="width: 100%" >
<f:selectItem itemValue="" itemLabel=""></f:selectItem>
<f:selectItems var='o' itemValue="#{o}" itemLabel="#{o.designation}" value="#{affectationBean.articles}" ></f:selectItems>
</h:selectOneMenu>
</td>
</tr>
<tr>
<td><h:outputLabel value="Quantité : " for="qte"></h:outputLabel></td>
<td><h:inputText value="#{affectationBean.quantite}" id="qte"></h:inputText></td>
</tr>
<h:commandButton value="Ajouter dans la liste " actionListener="#{affectationBean.addInList}" ></h:commandButton>
</table>
</h:form>
<!-- LISTES DES ARTICLES ET QUANTITE SELECTIONNER -->
<br/><br/><br/><br/>
<h1>Liste des articles à affecter :</h1>
<h:form >
<h:dataTable value="#{affectationBean.itemses}" var="o" border="1" style="border: 1px solid black;">
<h:column >
<f:facet name="header">Article </f:facet>
<h:outputText value="#{o.article.designation}"></h:outputText>
</h:column>
<h:column >
<f:facet name="header">Quantité dans le stock </f:facet>
<h:outputText value="#{o.article.quantitestock}"></h:outputText>
</h:column>
<h:column >
<f:facet name="header">Quantité demandé</f:facet>
<h:outputText value="#{o.quantite}"></h:outputText>
</h:column>
<h:column>
<f:facet name="header">Supprimer</f:facet>
<h:commandLink onclick="return confirmer()" actionListener="#{affectationBean.deleteArticle}">
<h:outputText value="Supprimer"></h:outputText>
<f:param name="id" value="#{o.article.idArticle}"></f:param>
</h:commandLink>
</h:column>
</h:dataTable>
<h:commandButton value="Affecter " actionListener="#{affectationBean.validerAffectation}" ></h:commandButton>
</h:form>
</body>
</html> |
affectationBean.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
@ManagedBean
@SessionScoped
public class AffectationBean {
private Integer idAffectation;
private String entite;
private String date;
private String quantite;
//METIER ENTITE ==== DIVISION :
EntiteM entiteM = new EntiteM();
private List<Entite> listDivision;
//Metier AFFECTATION :
AffectationM affectationM = new AffectationM();
//LISTE DES ARTICLES:
private Article article;
private List<Article> articles;
ArticleM articleM = new ArticleM();
//AJOUTER LES ARTICLES SELECTIONNES DANS UNE LISTE
private List<SelectedItems> itemses;
@PostConstruct
public void initBean() {
listDivision = entiteM.findAll();
articles = articleM.findAll();
}
public String getEntite() {
return entite;
}
public void setEntite(String entite) {
this.entite = entite;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public List<Entite> getListDivision() {
return listDivision;
}
public void setListDivision(List<Entite> listDivision) {
this.listDivision = listDivision;
}
public Article getArticle() {
return article;
}
public void setArticle(Article article) {
this.article = article;
}
public List<Article> getArticles() {
return articles;
}
public void setArticles(List<Article> articles) {
this.articles = articles;
}
public String getQuantite() {
return quantite;
}
public void setQuantite(String quantite) {
this.quantite = quantite;
}
public List<SelectedItems> getItemses() {
return itemses;
}
public void setItemses(List<SelectedItems> itemses) {
this.itemses = itemses;
}
public void addAffectation(ActionEvent e) {
String idType = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("division:divis");
Entite e1 = entiteM.findById(Integer.valueOf(idType));
Affectation affectation = new Affectation();
affectation.setDate(date);
affectation.setEntite(e1);
affectationM.Add(affectation);
}
public void addInList(ActionEvent e) {
SelectedItems items = new SelectedItems();
String idArt = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("article:art");
items.setArticle(articleM.findById(Integer.valueOf(idArt)));
items.setQuantite(Integer.valueOf(quantite));
itemses.add(items);
}
public void deleteArticle(ActionEvent e) {
Integer idArticle = Integer.valueOf(getParam("id"));
System.out.println("id article à supprimer : "+idArticle);
for(SelectedItems a1: itemses) {
if(idArticle == a1.getArticle().getIdArticle())
itemses.remove(a1);
System.out.println("Succès");
}
}
public String getParam(String name) {
FacesContext fc = FacesContext.getCurrentInstance();
Map<String , String> param = fc.getExternalContext().getRequestParameterMap();
return param.get(name);
}
} |