Suppression d'une occurence avec HQl
j'ai creer la classe SuppressionBcn afin de supprimer une occurence de la base a l'aide de Hql :
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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
public class SuppressionBcnBean
{
private String Titre;
private String serie;
private int ndeb;
private int nfin;
private int qte;
private int numbcn;
private Date dtbcn;
private int cdart;
private String cddep;
private String logincre;
private Date datecre;
private String loginmod;
private Date datemod;
private String msg;
public SuppressionBcnBean(){}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getLogincre() {
return logincre;
}
public void setLogincre(String logincre) {
this.logincre = logincre;
}
public Date getDatecre() {
return datecre;
}
public void setDatecre(Date datecre) {
this.datecre = datecre;
}
public String getLoginmod() {
return loginmod;
}
public void setLoginmod(String loginmod) {
this.loginmod = loginmod;
}
public Date getDatemod() {
return datemod;
}
public void setDatemod(Date datemod) {
this.datemod = datemod;
}
public int getCdart() {
return cdart;
}
public void setCdart(int cdart) {
this.cdart = cdart;
}
public String getCddep() {
return cddep;
}
public void setCddep(String cddep) {
this.cddep = cddep;
}
public String getTitre() {
return Titre;
}
public void setTitre(String titre) {
Titre = titre;
}
public String getSerie() {
return serie;
}
public void setSerie(String serie) {
this.serie = serie;
}
public int getNdeb() {
return ndeb;
}
public void setNdeb(int ndeb) {
this.ndeb = ndeb;
}
public int getNfin() {
return nfin;
}
public void setNfin(int nfin) {
this.nfin = nfin;
}
public int getQte() {
return qte;
}
public void setQte(int qte) {
this.qte = qte;
}
public int getNumbcn() {
return numbcn;
}
public void setNumbcn(int numbcn) {
this.numbcn = numbcn;
}
public Date getDtbcn() {
return dtbcn;
}
public void setDtbcn(Date dtbcn) {
this.dtbcn = dtbcn;
}
public String supprimer()
{
boolean verif = false;
SessionFactory sessionfactory = new Configuration().configure().buildSessionFactory();
Session session = sessionfactory.openSession();
try
{ Transaction tx = session.beginTransaction() ;
Bcn bb;
Query q= session.createQuery
("from Bcn as b where b.Id.idTitre=:idt and b.Id.serie=:s and b.Id.ndebut=:nd and b.nfin=:nf and b.dtBcn=:dt and b.nbcn=:nb");
q.setString("idt", this.Titre);
q.setString("s",this.serie);
q.setInteger("nd",this.ndeb);
q.setInteger("nf",this.nfin);
q.setDate("dt",this.dtbcn);
q.setInteger("nb",this.numbcn);
Iterator it = q.iterate();
while (it.hasNext())
{
bb = (Bcn)it.next();
this.cdart = bb.getCdart();
this.cddep = bb.getCddep();
this.logincre= bb.getLogincreateur();
this.datecre = bb.getDatecreation();
this.loginmod = bb.getLoginModificateur();
this.datemod = bb.getDateModification();
}
BcnPK pk = new BcnPK (this.serie,this.ndeb,this.Titre);
Bcn bcn = new Bcn(pk,this.nfin,this.dtbcn,this.cdart,this.numbcn,this.cddep,this.logincre,this.datecre,this.loginmod,this.datemod);
session.load(bcn,pk);
session.delete(bcn);
verif = true;
}
finally
{
session.close();
}
sessionfactory.close();
if ( verif = true)
{
this.msg="Suppression effectue";
return "ok";
}
else
{
this.msg = "Suppression non effectué";
return "nok";
}
}
} |
en lui associant la page jsp suivante :
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
|
<h:panelGrid columns="2" border="5" style="background-color:Orange">
<h:outputText value="Titre"></h:outputText>
<h:inputText id="title" value="#{suppressionBcnBean.titre}"></h:inputText>
<h:outputText value="Serie"></h:outputText>
<h:inputText id="serie" value="#{suppressionBcnBean.serie}"></h:inputText>
<h:outputText value="Premier N° B.C.N"></h:outputText>
<h:inputText id="premier" value="#{suppressionBcnBean.ndeb}"></h:inputText>
<h:outputText value="Dernier N° B.C.N"></h:outputText>
<h:inputText id="dernier" value="#{suppressionBcnBean.nfin}"></h:inputText>
<h:outputText value="Quantité"></h:outputText>
<h:inputText id="quantity" value="#{suppressionBcnBean.qte}"></h:inputText>
<h:outputText value="Numero B.C.N"></h:outputText>
<h:inputText id="numbcn" value="#{suppressionBcnBean.numbcn}"></h:inputText>
<h:outputText value="Date B.C.N"></h:outputText>
<h:inputText id="datebcn" value="#{suppressionBcnBean.dtbcn}"><f:convertDateTime pattern="yyyy-MM-dd"/></h:inputText><h:message for="datebcn" id="mess1"></h:message>
</h:panelGrid>
<br/><br/>
<h:panelGrid columns="2">
<center>
<h:commandButton value="Supprimer" type="submit"style="font-size: 18px; background-color: Orange" action="#{suppressionBcnBean.supprimer}"></h:commandButton>
<h:commandButton value="Reinitialiser" type="reset" style=" font-size:18px; background-color: Orange"></h:commandButton>
</center>
</h:panelGrid>
<br/>
<h:panelGrid columns="1">
<h:outputText value="#{suppressionBcnBean.msg}"></h:outputText>
</h:panelGrid>
</h:form>
</center>
</body>
</f:view>
</html> |
les classes BcnPk et Bcn sont generés pae hibernate dont les codes sont les suivants:
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
|
import stt.base.BaseBcn;
public class Bcn extends BaseBcn {
private static final long serialVersionUID = 1L;
/*[CONSTRUCTOR MARKER BEGIN]*/
public Bcn () {
super();
}
/**
* Constructor for primary key
*/
public Bcn (stt.BcnPK id) {
super(id);
}
/**
* Constructor for required fields
*/
public Bcn (
stt.BcnPK id,
java.lang.Integer nfin,
java.util.Date dtBcn,
java.lang.Integer cdart,
java.lang.Integer nbcn,
java.lang.String cddep,
java.lang.String logincreateur,
java.util.Date datecreation,
java.lang.String loginModificateur,
java.util.Date dateModification) {
super (
id,
nfin,
dtBcn,
cdart,
nbcn,
cddep,
logincreateur,
datecreation,
loginModificateur,
dateModification);
}
/*[CONSTRUCTOR MARKER END]*/
}
import stt.base.BaseBcnPK;
public class BcnPK extends BaseBcnPK {
private static final long serialVersionUID = 1L;
/*[CONSTRUCTOR MARKER BEGIN]*/
public BcnPK () {}
public BcnPK (
java.lang.String serie,
java.lang.Integer ndebut,
java.lang.String idTitre) {
super (
serie,
ndebut,
idTitre);
}
/*[CONSTRUCTOR MARKER END]*/
} |
le mapping file concernant la table Bcn :
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
|
<hibernate-mapping package="stt">
<class
name="Bcn"
table="bcn"
>
<meta attribute="sync-DAO">true</meta>
<composite-id name="Id" class="BcnPK">
<key-property
name="serie"
column="serie"
type="string"
/>
<key-property
name="ndebut"
column="n_debut"
type="integer"
/>
<key-property
name="idTitre"
column="id_titre"
type="string"
/>
</composite-id>
<property
name="nfin"
column="n_fin"
type="integer"
not-null="true"
length="10"
/>
<property
name="dtBcn"
column="dt_bcn"
type="date"
not-null="true"
length="10"
/>
<property
name="cdart"
column="cd_art"
type="integer"
not-null="true"
length="10"
/>
<property
name="nbcn"
column="n_bcn"
type="integer"
not-null="true"
length="10"
/>
<property
name="cddep"
column="cd_dep"
type="string"
not-null="true"
length="1"
/>
<property
name="logincreateur"
column="login_createur"
type="string"
not-null="true"
length="10"
/>
<property
name="datecreation"
column="date_creation"
type="date"
not-null="true"
length="10"
/>
<property
name="LoginModificateur"
column="login_modificateur"
type="string"
not-null="true"
length="10"
/>
<property
name="DateModification"
column="date_modification"
type="date"
not-null="true"
length="10"
/>
</class>
</hibernate-mapping> |
a l'execution une exception se declanche voire :
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
|
exception
javax.servlet.ServletException: Error calling action method of component with id j_id_jsp_339585192_1:j_id_jsp_339585192_34
javax.faces.webapp.FacesServlet.service(FacesServlet.java:256)
cause m�re
javax.faces.FacesException: Error calling action method of component with id j_id_jsp_339585192_1:j_id_jsp_339585192_34
org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:72)
javax.faces.component.UICommand.broadcast(UICommand.java:383)
javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:447)
javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:752)
com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:97)
com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:244)
cause m�re
javax.faces.el.EvaluationException: org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [stt.Bcn#stt.BcnPK@33e8cb72]
javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:91)
org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:57)
javax.faces.component.UICommand.broadcast(UICommand.java:383)
javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:447)
javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:752)
com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:97)
com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:244)
cause m�re
org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [stt.Bcn#stt.BcnPK@33e8cb72]
org.hibernate.impl.SessionFactoryImpl$2.handleEntityNotFound(SessionFactoryImpl.java:409)
org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:171)
org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:223)
org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:126)
org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:905)
org.hibernate.impl.SessionImpl.load(SessionImpl.java:811)
bean.SuppressionBcnBean.supprimer(SuppressionBcnBean.java:169)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:585)
org.apache.el.parser.AstValue.invoke(AstValue.java:172)
org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
org.apache.jasper.el.JspMethodExpression.invoke(JspMethodExpression.java:68)
javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:77)
org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:57)
javax.faces.component.UICommand.broadcast(UICommand.java:383)
javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:447)
javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:752)
com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:97)
com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:244) |
a l aide svp et merci