Hibernate - Association many-to-one

cs_LordBob Messages postés 2865 Date d'inscription samedi 2 novembre 2002 Statut Membre Dernière intervention 11 mai 2009 - 4 nov. 2008 à 23:13
jiidou Messages postés 48 Date d'inscription vendredi 31 août 2007 Statut Membre Dernière intervention 12 février 2009 - 5 nov. 2008 à 19:26
Bonsoir a tous,

voila je me décide à poster un message car j'ai un problème avec Hibernate et je n'arrive pas à trouver ce qui ne va pas malgrès mes nombreuses recherche sur Internet et mes parcours de Forum (bien que j'ai trouvé des infos).

En fait j'aimerais que vous m'aidier à trouver ce qui ne va pas. En fait tout simplement j'ai deux tables, une table REGION et une table DEPARTEMENT qui se compose comme ceci:
CREATE TABLE tr_region(
  id_pk SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
  nom VARCHAR(50) NOT NULL,
  PRIMARY KEY(id_pk)
)ENGINE= InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE tr_departement(
  id_pk VARCHAR(3) NOT NULL,
  nom VARCHAR(50) NOT NULL,
  region_id_fk SMALLINT UNSIGNED NOT NULL,
  PRIMARY KEY(id_pk),
  FOREIGN KEY(region_id_fk) REFERENCES tr_region(id_pk)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;


J'ai ainsi créé les fichiers de mapping suivant:
 departement.hbm.xml 
<?xml version ="1.0" encoding= "utf-8"?>
<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="com.xxx.domain.model.Departement" table="tr_departement" lazy="false">
        
        
        
        <many-to-one name="region" column="region_id_fk" not-null="true" class="com.xxx.domain.model.Region" />
    </class>
</hibernate-mapping>


 region.hbm.xml: 
<?xml version ="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="com.xxx.domain.model.Region" table="tr_region" lazy="false">
        
        
        
    </class>
</hibernate-mapping>


les classes de mapping:
public class Departement implements Serializable, Comparable<Departement>
{
    private static final long serialVersionUID = 562737487953277633L;
    private String id;
    private String nom;
    private Region region;

    public Departement()
    {
        id = null;
        nom = null;
        region = null;
    }
    
    public Departement(String id, String nom, Region region)
    {
        this.id = id;
        this.nom = nom;
        this.region = region;
    }

    public String getId()
    {
        return id;
    }

    public void setId(String id)
    {
        this.id = id;
    }

    public String getNom()
    {
        return nom;
    }

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

    public Region getRegion()
    {
        return region;
    }

    public void setRegion(Region region)
    {
        this.region = region;
    }
    
    @Override
    public boolean equals(Object o)
    {
        if(this == o)
            return true;
        
        if(!(o instanceof Departement))
            return false;
        
        final Departement dept = (Departement)o;
        if(!id.equals(dept.id))
            return false;
        
        return true;
    }
    
    @Override
    public int hashCode()
    {
        return id.hashCode();
    }
    
    public int compareTo(Departement that)
    {
        return this.getId().compareTo(that.getId());
    }
}

public class Region implements Serializable, Comparable<Region>
{
    private static final long serialVersionUID = -1420115862805756707L;
    private Long id;
    private String nom;

    public Region()
    {
        id = null;
        nom = null;
    }
    
    public Region(Long id, String nom)
    {
        this.id = id;
        this.nom = nom;
    }

    public Long getId()
    {
        return id;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    public String getNom()
    {
        return nom;
    }

    public void setNom(String nom)
    {
        this.nom = nom;
    }
    
    @Override
    public boolean equals(Object o)

    {
        if(this == o)
            return true;
        
        if(!(o instanceof Region))
            return false;
        
        final Region region = (Region)o;
        if(!id.equals(region.id))
            return false;
        
        return true;
    }
    
    @Override
    public int hashCode()
    {
        return id.hashCode();
    }
    
    public int compareTo(Region that)
    {
        return this.getId().compareTo(that.getId());
    }
}


et voila la requête que j'utilise:
departements = session.createCriteria(Departement.class)
                            .addOrder(Order.asc("nom"))
                            .list();


mais en fait cette requête ne me retourne aucun résultat bien que ma table contienne des données et je n'arrive vraiment pas à trouver d'où vient mon erreur!

Pourriez-vous m'aider svp ?

Merci par avance.
Bob...
"Vaut mieux se taire et passer pour un con, que de l'ouvrir et ne laisser aucun doute sur le sujet..."

1 réponse

jiidou Messages postés 48 Date d'inscription vendredi 31 août 2007 Statut Membre Dernière intervention 12 février 2009
5 nov. 2008 à 19:26
Bonsoir,
c'est pas evident de donner un coup de main pour un tel code.il vaut mieux d'utiliser des System.out.print..
comme ça tu rattrappas l'erreur.

Bon courage
0
Rejoignez-nous