Bonjour,
Voilà je travaille sur un projet hibernate, jsf 2.0 le tout sous netbeans 6.8.
J'aimerais supprimer un enregistrement de ma table et mon code ne fonctionne pas et je ne comprends pas pourquoi.
Les enregistrements de ma table sont stockées dans une data table dans ma page jsf.
Voici ce que j'ai pu faire pour supprimer :
la classe Authentification.java :
-----------------------------------------------------------------------
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
51
52
53
54 public class Authentification implements java.io.Serializable { private int idauth; private String loginauth; private String passauth; public Authentification() { } public Authentification(int idauth) { this.idauth = idauth; } public Authentification(int idauth, String loginauth, String passauth) { this.idauth = idauth; this.loginauth = loginauth; this.passauth = passauth; } /*@Id @GeneratedValue*/ public int getIdauth() { return this.idauth; } public void setIdauth(int idauth) { this.idauth = idauth; } public String getLoginauth() { return this.loginauth; } public void setLoginauth(String loginauth) { this.loginauth = loginauth; } public String getPassauth() { return this.passauth; } public void setPassauth(String passauth) { this.passauth = passauth; } public String clear(){ idauth=0; loginauth=""; passauth=""; return "clear"; } }
Ma fonction de supression dans ma classe AuthHelper :
--------------------------------------------------------------------------
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 public void delete(){ Authentification authentif = new Authentification(); if (authentif != null){ int id = authentif.getIdauth(); org.hibernate.Transaction tx =session.beginTransaction(); Query query = session.createQuery ("delete from Authentification as a where"+"a.idauth=:id"); query.setInteger("id", id); int rowCount = query.executeUpdate(); tx.commit(); System.out.println("Rows affected :"+rowCount); authentif.clear(); getAuthentication(); // La fonction qui affiche les enregistrements de la table } }
mon managed bean : AuthBean
-------------------------------------------------------------------------
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 package Authentif; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import java.util.List; public class AuthBean { AuthHelper helper= new AuthHelper();; private Authentification auth= new Authentification(); List<Authentification> authList; public AuthBean() { } public Authentification getAuth(){return auth;} public void setAuth(Authentification auth){this.auth=auth;} public String deleteAuth() { authentication.getRowData(); helper.delete(); return "index"; }
Ma page jsf :
-------------------------------------------------------------------------
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 <h:body> <h:form> <h:dataTable value="#{AuthBean.getAuthen()}" var="item" border="0" cellpadding="2" cellspacing="0" rowClasses="jsfcrud_odd_row,jsfcrud_even_row" rules="all" style="border:solid 1px"> <h:column> <f:facet name="header"> <h:outputText value="idauth"/> </f:facet> <h:outputText value="#{item.idauth}"/> </h:column> <h:column> <f:facet name="header"> <h:outputText value="login"/> </f:facet> <h:outputText value="#{item.loginauth}"/> </h:column> <h:column> <f:facet name="header"> <h:outputText value="mot de passe"/> </f:facet> <h:outputText value="#{item.passauth}"/> </h:column> <h:column> <f:facet name="header"> <h:outputText value=" "/> </f:facet> <h:commandButton action="#{AuthBean.UpdateAuth}" value="Modifer"/> </h:column> <h:column> <f:facet name="header"> <h:outputText value=" "/> </f:facet> <h:commandButton action="#{AuthBean.deleteAuth}" value="Supprimer"/> </h:column> </h:dataTable> <br/> <h:commandLink action="#{AuthBean.NextPage}" value="next"/> </h:form> </h:body>
Je vous serait très reconnaissante de toute l'aide apportée.
Partager