Bonjour à tous.
J'ai un problème de suppression d'enregistrements avec JPA+JSF.
Avant de vous décrire le problème je vous invite à bien regarder l'image suivante :
En effet,mon souci est le suivant : si je tente de supprimer un enregistrement autre que celui qui se trouve à la première position,c'est toujours celui de la première position qui est supprimé,autrement dit,celui ayant le plus petit identifiant car les enregistrements sont ordonnés par ordre croissant d'identifiants.
Par exemple sur l'image ci-dessus,si je supprime "ordinateur" ou "Céréale" de la liste alors c'est "Boisson" qui sera supprimée.
Ci-dessous le code des différentes méthodes chargées de réaliser la suppression :
Pour le backing-bean :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 package Objets_EJBStatelless; import java.util.List; import javax.ejb.Stateless; import javax.faces.bean.ManagedBean; import javax.faces.model.DataModel; import javax.faces.model.ListDataModel; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.TypedQuery; import Objets_EJBEntity.TypeProduit_Entity; @Stateless public class TypeProduit_EJBStatelless { private static final String Liste_DesTypes = "SELECT t FROM TypeProduit_Entity t"; private DataModel<TypeProduit_Entity> liste_EN_DataModel; private List<TypeProduit_Entity> liste; @PersistenceContext(unitName = "Gestion_Stock") private EntityManager interaction_Avec_BDD; public List<TypeProduit_Entity> getListe() { try{ TypedQuery<TypeProduit_Entity> les_TypesProduits = interaction_Avec_BDD.createQuery(Liste_DesTypes, TypeProduit_Entity.class); return les_TypesProduits.getResultList(); }catch(DAOException d){ throw new DAOException(d); } } public DataModel<TypeProduit_Entity> getListe_EN_DataModel() { liste_EN_DataModel= new ListDataModel<TypeProduit_Entity>(); liste_EN_DataModel.setWrappedData(getListe()); return liste_EN_DataModel; } public void supprimerTypeProduit(){ try{ TypeProduit_Entity tpe = getListe_EN_DataModel().getRowData(); interaction_Avec_BDD.remove(interaction_Avec_BDD.merge(tpe)); }catch(DAOException d){ throw new DAOException(d); } } }
Et enfin voilà comment j'ai défini le lien :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 package Gestionnaire_DEntite; import java.io.Serializable; import java.util.List; import javax.ejb.EJB; import javax.faces.application.FacesMessage; import javax.faces.bean.ManagedBean; import javax.faces.context.FacesContext; import Objets_EJBEntity.TypeProduit_Entity; import Objets_EJBStatelless.TypeProduit_EJBStatelless; @ManagedBean public class Enregistrer_TypeProduit implements Serializable {* private TypeProduit_Entity typeProduit; public Enregistrer_TypeProduit(){ typeProduit = new TypeProduit_Entity(); } @EJB private TypeProduit_EJBStatelless typeProduitState; public TypeProduit_Entity getTypeProduit() { return typeProduit; } public void supprimer(){ typeProduitState.supprimerTypeProduit(); } }
Si quelqu'un a une idée je suis à l'écoute.Merci d'avance!!!
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 <h:commandLink action = "#{enregistrer_TypeProduit.supprimer}" immediate = "true"> <h:graphicImage url = "/Image/sup.png" title = "Supprimer"/> </h:commandLink>
Partager