J'ai regardé je ni comprend plus rien
Bon alors voila je vais tout dettayer histoire de voir si je fais une boulette en amont.
Donc j'ai un gestionnaire de session qui est le suivant:
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
| package util;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
//Crée la SessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (HibernateException ex) {
throw new RuntimeException("Problème de config:"+ex.getMessage(), ex);
}
}
public static final ThreadLocal<Session> session = new ThreadLocal<Session>();
public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
//Ouvre une nouvelle Session, si ce Thread n'en a aucune
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
}
} |
Puis j'ai deux classes persistantes:
Une qui représente un batiment:
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
|
package unite;
import java.util.HashSet;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import fr.gouv.defense.terre.rhc1.util.HibernateUtil;
/**
*
* @hibernate.class table="unite_batiment"
* @author Christophe
*
*/
public class Batiment {
/**
* identifiant non porteur de données.
*/
private int id_batiment;
private String libelle;
private Set pieces = new HashSet();
public Batiment(){
}
/**
*
* @param _libelle
*/
public Batiment(String _libelle) {
this.setLibelle(_libelle.toLowerCase());
}
/**
* @hibernate.property
* @return String
*/
public String getLibelle() {
return libelle;
}
/**
* @param _idEtage
* @return Etage
* @throws HibernateException
*/
public Etage getEtage(Etage _etage) throws HibernateException {
Etage etage = null;
ArrayList<Etage> arrayEtage = new ArrayList<Etage>(etages);
for(Etage currentEtage : arrayEtage){
if(currentEtage.compareTo(_etage,this)){
etage=_etage;
break;
}
}
return etage;
}
/**
*
* @hibernate.set
* lazy="true"
* cascade="all"
* @hibernate.collection-key
* column="id_etage"
* @hibernate.collection-one-to-many class="fr.gouv.defense.terre.rhc1.unite.Etage"
/*
*hibernate.many-to-one class="fr.gouv.defense.terre.rhc1.unite.Etage"
*
* return Set
*/
public Set getEtages() {
return etages;
}
/**
*
* @param _libelle
*/
public void setLibelle(String _libelle) {
libelle = _libelle;
}
/**
*
* @param _idEtage
* @throws Exception
*/
public void addEtage(String _idEtage) throws Exception{
Etage newEtage = new Etage(_idEtage,this);
System.out.println("Etage a ajouter: "+newEtage);
System.out.println("getEtage(newEtage): "+getEtage(newEtage));
if(getEtage(newEtage)==null){
etages.add(newEtage);
}else{
throw new Exception("L'etage \""+_idEtage+"\" existe déjà. ");
}
}
/**
*
* @param _etage
*/
public void removeEtage(Etage _etage){
// getEtages().remove(getEtage(_etage.getIntitule()));
}
/**
*
*/
public String toString(){
return "\n Libelle batiment:"+this.getLibelle()+
"\n ---------------------------------";
}
public boolean compareTo(Batiment _batiment){
boolean identique = true;
if(this.getLibelle().compareToIgnoreCase(_batiment.getLibelle())!=0){
identique = false;
}
return identique;
}
/**
* @hibernate.id generator-class="native"
* @return int
*/
public int getId_batiment() {
return id_batiment;
}
public void setId_batiment(int id_batiment) {
this.id_batiment = id_batiment;
}
public void setEtages(Set<Etage> etages) {
this.etages = etages;
}
} |
l'autre qui représente un etage:
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
|
package unite;
/**
* @hibernate.class table="unite_etage"
* @author Christophe
*
*/
public class Etage {
/**
* identifiant non porteur de données.
*/
private int id_etage;
private String intitule;
private Batiment batiment;
public Etage(){
}
/**
*
* @param _idEtage
*/
public Etage(String _idEtage, Batiment _batiment){
this.setIdEtage(_idEtage.toLowerCase());
this.setBatiment(_batiment);
}
/**
* @hibernate.property
* @return String
*
*/
public String getIntitule() {
return intitule;
}
/**
*
* @param _idEtage
*/
public void setIdEtage(String _idEtage) {
this.intitule = _idEtage;
}
public String toString(){
return "\n Etage: "+this.getIntitule()+
"\n "+this.getBatiment()+
"\n ---------------------------------";
}
public boolean compareTo(Etage _etage, Batiment _batiment){
boolean identique =true;
if(this.getIntitule().compareToIgnoreCase(_etage.getIntitule())!=0){
identique=false;
}else if(!this.getBatiment().compareTo(_batiment)){
identique=false;
}
return identique;
}
/**
* @hibernate.id
* generator-class="native"
* @return int
*/
public int getId_etage() {
return id_etage;
}
public void setId_etage(int id_etage) {
this.id_etage = id_etage;
}
public void setIntituleEtage(String intituleEtage) {
this.intitule = intituleEtage;
}
public void setIntitule(String intitule) {
this.intitule = intitule;
}
/**
* @hibernate.many-to-one class="fr.gouv.defense.terre.rhc1.unite.Batiment"
* @return batiment
*/
public Batiment getBatiment() {
return batiment;
}
public void setBatiment(Batiment _batiment) {
this.batiment = _batiment;
}
} |
Pour finir j'utilise un gestionnaire que voici:
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
|
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import util.HibernateUtil;
public class GestionnaireUnite {
public static Batiment getBatiment(String _batiment) throws HibernateException{
Batiment batiment = null ;
Session session = null;
session = HibernateUtil.currentSession();
Query q = null;
q = session.createQuery("from fr.gouv.defense.terre.rhc1.unite.Batiment batiment where batiment.libelle = :libelle");
q.setString("libelle", _batiment);
if(!q.list().isEmpty()){
batiment = (Batiment) q.uniqueResult();
}
System.out.println("Batiment :"+ batiment);
return batiment;
}
/**
*
* @param _batiment
* @throws Exception
*/
public static void addBatiment(String _batiment) throws Exception{
Batiment batiment = new Batiment(_batiment);
if(getBatiment(batiment.getLibelle())==null){
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
session.save(batiment);
tx.commit();
HibernateUtil.closeSession();
}else{
throw new Exception("Le batiment \""+batiment.getLibelle()+"\" existe déjà. ");
}
}
/**
*
* @param _batiment
* @throws HibernateException
*/
public static void removeBatiment(Batiment _batiment) throws HibernateException{
Session session = null;
Transaction tx = null;
session = HibernateUtil.currentSession();
tx = session.beginTransaction();
session.delete(getBatiment(_batiment.getLibelle()));
tx.commit();
HibernateUtil.closeSession();
}
} |
Mon code utilise ces classes de cette façon:
Code:
1 2 3 4 5
| GestionnaireUnite.addBatiment("00-50");
GestionnaireUnite.addBatiment("TAkaToUkIté");
GestionnaireUnite.getBatiment("00-50").addEtage("1er EtAgE");
GestionnaireUnite.getBatiment("00-50").addEtage("2eme EtAgE"); |
Apres quoi je regarde en BD, pas de soucis le batiment y est, mais pas les étage...
Please help me!
On s'en approche, mais pourtant:
Alors je viens d'ajouter a mon gestionnaire d'unité un méthode updateBatiment( Batiment _batiment) que voici:
Code:
1 2 3 4 5 6 7 8 9
| public static void updateBatiment(Batiment _batiment) throws HibernateException{
Session session = null;
Transaction tx = null;
session = HibernateUtil.currentSession();
tx = session.beginTransaction();
session.save(_batiment);
tx.commit();
HibernateUtil.closeSession();
} |
et j'ai modifié la méthode addEtage de l'objet Batiment:
Code:
1 2 3 4 5 6 7 8 9 10 11
| public void addEtage(String _idEtage) throws Exception{
Etage newEtage = new Etage(_idEtage,this);
System.out.println("Etage a ajouter: "+newEtage);
System.out.println("getEtage(newEtage): "+getEtage(newEtage));
if(getEtage(newEtage)==null){
etages.add(newEtage);
GestionnaireUnite.updateBatiment(this);
}else{
throw new Exception("L'etage \""+_idEtage+"\" existe déjà. ");
}
} |
cependant quand j'execute le code, voila ce qui apparait dans la console :s
Code:
1 2 3 4 5
|
15:33:19,972 DEBUG SQL:230 - select etages0_.id_etage as id_etage__, etages0_.id_etage as id_etage0_, etages0_.intitule as intitule0_, etages0_.batiment as batiment0_ from unite_etage etages0_ where etages0_.id_etage=?
getEtage(newEtage): null
15:33:19,987 DEBUG SQL:230 - update unite_etage set intitule=?, batiment=? where id_etage=?
15:33:20,003 ERROR SessionImpl:2400 - Could not synchronize database state with session |
Une petit idée magique?
PS1: Mon gestionnaire de session se trouve dans un précèdent message
PS2: Je n'ai pas trop compris le moyens que tu proposes pour les méthodes de comparaison...
Merci bien pour les comparaisons :)
Merci bien pour les comparaisons :)
Pour l'erreur, la voici :
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
| 16:09:40,206 DEBUG SQL:230 - select etages0_.id_etage as id_etage__, etages0_.id_etage as id_etage0_, etages0_.intitule as intitule0_, etages0_.batiment as batiment0_ from unite_etage etages0_ where etages0_.id_etage=?
getEtage(newEtage): null
16:09:40,231 DEBUG SQL:230 - update unite_etage set intitule=?, batiment=? where id_etage=?
16:09:40,236 ERROR SessionImpl:2400 - Could not synchronize database state with session
net.sf.hibernate.HibernateException: Batch update row count wrong: 0
at net.sf.hibernate.impl.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:65)
at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:128)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2438)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2393)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2261)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at fr.gouv.defense.terre.rhc1.unite.Batiment.addEtage(Batiment.java:181)
at test.Controler.init(Controler.java:79)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1161)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:806)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:133)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:216)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:634)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:445)
at java.lang.Thread.run(Unknown Source) |
Bon je viens de trouver un début de piste
Alors ce qui est etrange c'est que dans la console je vois un update. Hors vu que l'etage n'existe pas encore, cela devrait mettre un insert.
Code:
1 2 3 4
| 18:58:19,140 DEBUG SQL:230 - select etages0_.id_etage as id_etage__, etages0_.id_etage as id_etage0_, etages0_.intitule as intitule0_, etages0_.batiment as batiment0_ from unite_etage etages0_ where etages0_.id_etage=?
getEtage(newEtage): null
18:58:19,156 DEBUG SQL:230 - update unite_etage set intitule=?, batiment=? where id_etage=?
18:58:19,160 ERROR SessionImpl:2400 - Could not synchronize database state with session |