Débutant structs HIBERNATE

abdouffff Messages postés 23 Date d'inscription samedi 21 janvier 2012 Statut Membre Dernière intervention 4 mars 2012 - 11 févr. 2012 à 01:07
 sisouf007 - 13 mai 2013 à 02:53
Salut,
je suis debutant avec hibernate ma projet pfe avec structs 2 hibernat ma probleme en hibernate la proget et comme suite
.. test
.web page
.source depackage
.....pachagedefaut
.........hibernate.cfg
.........struts.xml
.........hibernate.reveng
.........exemple.xml
.....controlle
.........produitMnager.java
.....modelle
.........Produit.hbm.xml
.........Produit.java
.....view
.........INFORMATIQUECON.java
.....util
.........hibernateUtil.java
.....................................voila le page produitManager
package controller;



import java.util.List;
import modelle.Produit;
import org.hibernate.HibernateException;
import org.hibernate.classic.Session;
import util.HibernateUtil;
/**
*
* @author Dell
*/
public class produitManager extends HibernateUtil{



public Produit add(Produit produit) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.save(produit);
session.getTransaction().commit();
return produit;
}
public Produit delete(Long idp) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Produit produts = (Produit) session.load(Produit.class, idp);
if(null != produts) {
session.delete(produts);
}
session.getTransaction().commit();
return produts;
}



public List list() {

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List produit = null;
try {

produit = (List)session.createQuery("from Produit").list();

} catch (HibernateException e) {
e.printStackTrace();
session.getTransaction().rollback();
}
session.getTransaction().commit();
return produit;
}
}
...........................................la page produit.java ........................................................

package modelle;
// Generated 10 f�vr. 2012 05:33:46 by Hibernate Tools 3.2.1.GA


import java.util.HashSet;
import java.util.Set;
/**
* Produit generated by hbm2java
*/
public class Produit implements java.io.Serializable {


private Integer idP;
private String nom;
private String desig;
private float prix;
private String img;
private boolean aff;
private String type;
private Set paniers = new HashSet(0);

public Produit() {
}


public Produit(String nom, String desig, float prix, String img, boolean aff, String type) {
this.nom = nom;
this.desig = desig;
this.prix = prix;
this.img = img;
this.aff = aff;
this.type = type;
}
public Produit(String nom, String desig, float prix, String img, boolean aff, String type, Set paniers) {
this.nom = nom;
this.desig = desig;
this.prix = prix;
this.img = img;
this.aff = aff;
this.type = type;
this.paniers = paniers;
}

public Integer getIdP() {
return this.idP;
}

public void setIdP(Integer idP) {
this.idP = idP;
}
public String getNom() {
return this.nom;
}

public void setNom(String nom) {
this.nom = nom;
}
public String getDesig() {
return this.desig;
}

public void setDesig(String desig) {
this.desig = desig;
}
public float getPrix() {
return this.prix;
}

public void setPrix(float prix) {
this.prix = prix;
}
public String getImg() {
return this.img;
}

public void setImg(String img) {
this.img = img;
}
public boolean isAff() {
return this.aff;
}

public void setAff(boolean aff) {
this.aff = aff;
}
public String getType() {
return this.type;
}

public void setType(String type) {
this.type = type;
}
public Set getPaniers() {
return this.paniers;
}

public void setPaniers(Set paniers) {
this.paniers = paniers;
}




}
................le page struts exemle.xml et cava et le resule passe a la page suivant mais l'erreure .........................
java.lang.NullPointerException
veiw.INFORMATIQUECON.execute(INFORMATIQUECON.java:58)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:601)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263)..................................................................................quelqu'un qui m'aide s'il vous plait

4 réponses

cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
11 févr. 2012 à 15:43
Bonjour,

Une nullPointerException signifie que tu appelles une méthode sur un objet qui est null.

Localise la ligne qui lève l'exception, sur cette ligne tu dois avoir un truc du genre : objet.methode();

objet est null. Essayes de comprendre pourquoi et de corriger le problème.

Es tu certain que la session courante n'est pas à null ?

Si tu ne trouves pas, copies moi les lignes qui levent l'exception et marque moi précisément la ligne qui la génére.
0
abdouffff Messages postés 23 Date d'inscription samedi 21 janvier 2012 Statut Membre Dernière intervention 4 mars 2012
11 févr. 2012 à 22:59
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package view;

import com.opensymphony.xwork2.ActionSupport;
import controller.produitManager;
import java.util.List;
import modelle.Produit;








/**
*
* @author Dell
*/
public class INFORMATIQUECON extends ActionSupport {


private static final long serialVersionUID = 9149826260758390091L;

private List produitList;
private Long idp;
private produitManager prodManager;
private Produit produit;

public Long getIdp() {
return idp;
}

public void setIdp(Long idp) {
this.idp = idp;
}


public List getProduitList() {
return produitList;
}

public void setProduitList(List produitList) {
this.produitList = produitList;
}








@Override
public String execute() {
this.produitList = prodManager.list();
System.out.println("execute called");
return SUCCESS;
}




}
0
abdouffff Messages postés 23 Date d'inscription samedi 21 janvier 2012 Statut Membre Dernière intervention 4 mars 2012
11 févr. 2012 à 23:08
merci julien,voila
la page qui'il contien la methode execute
0
public void addRoot(String t1,String t2) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
String hqlUpdate = "update Preselling p set p.root=:root where p.article=:article";
Query query = session.createQuery( hqlUpdate );
query.setString**("article",+t1);**
query.setString**("root",+t2);**
query.executeUpdate();


session.getTransaction().commit();
}

PLZ help me to pass argument et set in database . probleme is here +t1 and +t2

if we don't work by hibernate the query will be String hqlUpdate = "update Preselling p set p.root='"+t1+"' where p.article='"t2"');
0
Rejoignez-nous