[hibernate] probleme de mapping un-vers-plusieurs
Tout d'abord le contexte :
La base de données (2 tables):
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
CREATE TABLE `fonction` (
`FN_ID` int(11) NOT NULL auto_increment,
`FN_TITRE` varchar(20) NOT NULL,
PRIMARY KEY (`FN_ID`)
);
CREATE TABLE collaborateur (
`C_ID` int(11) NOT NULL auto_increment,
`C_NOM` varchar(20) default NULL,
`C_PRENOM` varchar(20) default NULL,
`C_DATENAISSANCE` date default NULL,
`C_MAIL` varchar(30) default NULL,
`C_TELEPHONE` varchar(10) default NULL,
`C_FONCTION` int(11) default NULL,
`C_DATEMODIF` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
PRIMARY KEY (`C_ID`),
KEY `FK_collaborateur_fonction` (`C_FONCTION`),
CONSTRAINT `collaborateur_ibfk_1` FOREIGN KEY (`C_FONCTION`) REFERENCES `fonction` (`FN_ID`)
); |
La table Collaborateur a une colonne Fonction qui est une clé etrangere referencant une ligne de la table Fonction.
Mes fichiers de mapping :
Collaborateur.hbm.xml :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="col.Collaborateur" table="collaborateur">
<id name="id" column="C_ID">
<generator class="increment"/>
</id>
<property name="nom" column="C_NOM" type="java.lang.String" length="20"/>
<property name="prenom" column="C_PRENOM" type="java.lang.String" length="20"/>
<property name="dateNaissance" column="C_DATENAISSANCE" type="timestamp"/>
<property name="mail" column="C_MAIL" type="java.lang.String" length="30"/>
<property name="telephone" column="C_TELEPHONE" type="java.lang.String" length="10"/>
<many-to-one name="fonction" class="col.Fonction" column="C_FONCTION"/>
</class>
</hibernate-mapping> |
Fonction.hbm.xml:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="col.Fonction" table="fonction">
<id name="id" column="FN_ID">
<generator class="increment"/>
</id>
<property name="titre" column="FN_TITRE" type="string" length="20"/>
<set name="collaborateurs">
<key column="C_FONCTION"/>
<one-to-many class="col.Collaborateur"/>
</set>
</class>
</hibernate-mapping> |
et enfin les classes :
Collaborateur.java :
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
|
package col;
import java.util.Date;
public class Collaborateur {
private int id;
private int fonction;
private String nom;
private String prenom;
private Date dateNaissance;
private String mail;
private String telephone;
public Collaborateur() {
}
public int getFonction() {
return fonction;
}
public void setFonction(int fonction){
this.fonction = fonction;
}
public int getId(){
return id;
}
public void setId(int id){
this.id = id;
}
public String getNom(){
return nom;
}
public void setNom(String nom){
this.nom = nom;
}
public String getPrenom(){
return prenom;
}
public void setPrenom(String prenom){
this.prenom = prenom;
}
public Date getDateNaissance(){
return dateNaissance;
}
public void setDateNaissance(Date dateNaissance){
this.dateNaissance = dateNaissance;
}
public String getMail(){
return mail;
}
public void setMail(String mail){
this.mail = mail;
}
public String getTelephone(){
return telephone;
}
public void setTelephone(String telephone){
this.telephone = telephone;
}
} |
Fonction.java:
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 col;
import java.util.HashSet;
import java.util.Set;
public class Fonction {
private int id;
private String titre;
private Set collaborateurs = new HashSet();
public Fonction() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitre() {
return titre;
}
public void setTitre(String titre) {
this.titre = titre;
}
public Set getCollaborateurs(){
return collaborateurs;
}
public void setCollaborateurs(Set collaborateurs) {
this.collaborateurs = collaborateurs;
}
} |
Voila, vous avez toutes les infos pour comprendre le probleme.
Quand je teste une insertion (avec SessionFactory et Session), la construction de la SessionFactory echoue en me donnant cette raison :
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
|
[java] Initial SessionFactory creation failed.
[java] org.hibernate.PropertyNotFoundException: Could not find a setter for
property fonction in class col.Collaborateur
[java] Exception in thread "main" java.lang.ExceptionInInitializerError
[java] at util.HibernateUtil.<clinit>(Unknown Source)
[java] at col.CollaborateurManager.createAndStoreEvent(Unknown Source)
[java] at col.CollaborateurManager.main(Unknown Source)
[java] Caused by: org.hibernate.PropertyNotFoundException: Could not find a
setter for property fonction in class col.Collaborateur
[java] at org.hibernate.property.BasicPropertyAccessor.createSetter(Bas
icPropertyAccessor.java:216)
[java] at org.hibernate.property.BasicPropertyAccessor.getSetter(BasicP
ropertyAccessor.java:209)
[java] at org.hibernate.mapping.Property.getSetter(Property.java:265)
[java] at org.hibernate.tuple.PojoEntityTuplizer.buildPropertySetter(Po
joEntityTuplizer.java:259)
[java] at org.hibernate.tuple.AbstractEntityTuplizer.<init>(AbstractEnt
ityTuplizer.java:122)
[java] at org.hibernate.tuple.PojoEntityTuplizer.<init>(PojoEntityTupli
zer.java:55)
[java] at org.hibernate.tuple.TuplizerLookup.create(TuplizerLookup.java
:64)
[java] at org.hibernate.tuple.EntityMetamodel.<init>(EntityMetamodel.ja
va:257)
[java] at org.hibernate.persister.entity.AbstractEntityPersister.<init>
(AbstractEntityPersister.java:412)
[java] at org.hibernate.persister.entity.SingleTableEntityPersister.<in
it>(SingleTableEntityPersister.java:108)
[java] at org.hibernate.persister.PersisterFactory.createClassPersister
(PersisterFactory.java:55)
[java] at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryIm
pl.java:216)
[java] at org.hibernate.cfg.Configuration.buildSessionFactory(Configura
tion.java:1176)
[java] ... 3 more
[java] Java Result: 1 |
En clair, il me dit qu'il ne trouve pas de setter dans la classe Collaborateur pour la propriété fonction.
Or elle y est et me semble bien ecrite...
Ca fait une 1/2 journée que je suis la dessus, et je commence à devenir chevre... je vous precise que c'est la premiere fois que j'utilise Hibernate et cela explique mon manque d experience sur ce genre d'erreurs...
je pense que beaucoup ici ont de l experience et cela doit leur sembler plus evident à corriger... je vous demande alors de l'aide! au secours!