Problème assez particulier avec Set
Bonjour à tous ! Voila je post ici parce que j'ai eu beau chercher, je n'ai toujours pas trouvé de solution.
Voila le contexte : imaginez un annuaire des anciens étudiants. Il comporte des Promotions, qui elles-mêmes contiennent des Etudiants.
Donc je créé les fichiers de mapping via le plugin HibernateSynchronizer d'Eclipse :
fichier TPromotions.hbm.xml qui représente une promotion :
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
| <hibernate-mapping package="ultimate.beans">
<class
name="Promotion"
table="TPromotions"
>
<meta attribute="sync-DAO">false</meta>
<id
name="id"
type="integer"
column="id"
>
<generator class="native"/>
</id>
<property
name="titre"
column="titre"
type="string"
not-null="true"
length="255"
/>
<property
name="description"
column="description"
type="string"
not-null="false"
/>
<property
name="annee"
column="annee"
type="integer"
not-null="true"
length="11"
/>
<property
name="specialite"
column="specialite"
type="string"
not-null="false"
length="255"
/>
<property
name="options"
column="options"
type="string"
not-null="false"
length="255"
/>
<property
name="location"
column="location"
type="string"
not-null="false"
length="255"
/>
<set name="etudiants" inverse="true" lazy="false">
<key column="id"/>
<one-to-many class="Etudiant"/>
</set>
</class> |
fichier TEtudiants.hbm.xml qui représente un étudiant :
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
| <hibernate-mapping package="ultimate.beans">
<class
name="Etudiant"
table="TEtudiants"
>
<meta attribute="sync-DAO">false</meta>
<id
name="id"
type="integer"
column="id"
>
<generator class="native"/>
</id>
<property
name="nom"
column="nom"
type="string"
not-null="true"
length="255"
/>
<property
name="prenom"
column="prenom"
type="string"
not-null="false"
length="255"
/>
<property
name="mail"
column="mail"
type="string"
not-null="false"
length="255"
/>
<property
name="dateNaissance"
column="dateNaissance"
type="date"
not-null="false"
length="10"
/>
<property
name="societe"
column="societe"
type="string"
not-null="false"
length="255"
/>
<property
name="societeWeb"
column="societeWeb"
type="string"
not-null="false"
/>
<property
name="web"
column="web"
type="string"
not-null="false"
/>
<property
name="dateMAJ"
column="dateMAJ"
type="date"
not-null="false"
length="10"
/>
<many-to-one
name="promotion"
column="promotion"
class="Promotion"
not-null="true"
>
</many-to-one>
</class>
</hibernate-mapping> |
création des tables SQL :
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
| CREATE TABLE `TEtudiants` (
`nom` varchar(255) NOT NULL,
`prenom` varchar(255) default NULL,
`mail` varchar(255) default NULL,
`promotion` int(11) NOT NULL,
`dateNaissance` date default NULL,
`id` int(11) NOT NULL auto_increment,
`societe` varchar(255) default NULL,
`societeWeb` text,
`web` text,
`dateMAJ` date default NULL,
PRIMARY KEY (`id`),
KEY `fk_promo` (`promotion`),
CONSTRAINT `fk_promo` FOREIGN KEY (`promotion`) REFERENCES `TPromotions` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=latin1
CREATE TABLE `TPromotions` (
`id` int(11) NOT NULL auto_increment,
`titre` varchar(255) NOT NULL,
`description` text,
`annee` int(11) NOT NULL default '1970',
`specialite` varchar(255) default NULL,
`options` varchar(255) default NULL,
`location` varchar(255) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1 |
Le problème :
Il est très particulier. Lors du listage des promotions (qui sont en Lazy=false), le seul étudiant à se charger dans le Set de la promotion est l'étudiant qui a le même ID que la promotion.
Exemple :
Imaginons que j'ai un Etudiant ALAIN d'identifiant 2 et un étudiant ALEX d'identifiant 3, tout deux faisant partie de la promotion d'identifiant 3, lors du chargement de la promotion 3, seul ALEX (id=3) se chargera dans le Set.
Avez-vous déjà vu ce problème ? Avez vous une solution ?
Pensez-vous que cela vienne du
Code:
1 2 3 4
| <set name="etudiants" inverse="true" lazy="false">
<key column="id"/>
<one-to-many class="Etudiant"/>
</set> |
sachant que ET les promotions ET les étudiants ont pour clé primaire une colonne de même nom ?
Je suis perdu :D. Merci d'avance de votre aide :)