[Hibernate 3] Relation many-to-one
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:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
| 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:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| 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">
<id name="id" column="id_pk" />
<property name="nom" column="nom" not-null="true" />
<many-to-one name="region" column="region_id_fk" not-null="true" class="com.xxx.domain.model.Region" />
</class>
</hibernate-mapping> |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
| 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">
<id name="id" column="id_pk" />
<property name="nom" column="nom" not-null="true" />
</class>
</hibernate-mapping> |
les classes de mapping:
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
|
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());
}
} |
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
| 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:
Code:
1 2 3
| 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.