IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Hibernate Java Discussion :

[hibernate] probleme de mapping un-vers-plusieurs


Sujet :

Hibernate Java

  1. #1
    En attente de confirmation mail
    Profil pro
    Inscrit en
    Novembre 2003
    Messages
    82
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : France

    Informations forums :
    Inscription : Novembre 2003
    Messages : 82
    Points : 78
    Points
    78
    Par défaut [hibernate] probleme de mapping un-vers-plusieurs
    Tout d'abord le contexte :

    La base de données (2 tables):
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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!

  2. #2
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    29
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 29
    Points : 35
    Points
    35
    Par défaut
    il faut que fonction soit de type Fonction et non de type int.

    Jérôme

  3. #3
    En attente de confirmation mail
    Profil pro
    Inscrit en
    Novembre 2003
    Messages
    82
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : France

    Informations forums :
    Inscription : Novembre 2003
    Messages : 82
    Points : 78
    Points
    78
    Par défaut
    Citation Envoyé par jeje900ss
    il faut que fonction soit de type Fonction et non de type int.

    Jérôme
    J'ai modifié ce que tu m'as dit , voici comment est la classe Collaborateur:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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 Fonction fonction;
       private String nom;
       private String prenom;
       private Date dateNaissance;
       private String mail;
       private String telephone;
     
     
       public Collaborateur() {
       }
     
       public Fonction getFonction() {
          return fonction;
       }
     
       public void setFonction(Fonction 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;
       }
    }
    mais j'ai toujours la meme erreur

Discussions similaires

  1. Réponses: 8
    Dernier message: 27/08/2008, 18h36
  2. hibernate problem (classe not mapped)
    Par oughlad dans le forum Hibernate
    Réponses: 11
    Dernier message: 25/06/2007, 19h57
  3. Réponses: 2
    Dernier message: 17/07/2006, 14h45
  4. [Hibernate] Problème de mapping ?
    Par n@n¤u dans le forum Hibernate
    Réponses: 3
    Dernier message: 13/06/2006, 08h45
  5. [Mapping]relation plusieurs-vers-plusieurs
    Par berret dans le forum Hibernate
    Réponses: 2
    Dernier message: 21/01/2005, 18h44

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo