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

Persistance des données Java Discussion :

Pb de mapping de collection d'objets


Sujet :

Persistance des données Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Inscrit en
    Août 2010
    Messages
    36
    Détails du profil
    Informations forums :
    Inscription : Août 2010
    Messages : 36
    Par défaut
    Bonjour,

    J'ai un bug dans mon fichier de mapping "Personne.hbm.xml", quand j'enlève le bout de code "en commentaire" :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    <set name="voitures" inverse="true" cascade="save-update">
                    <key column="ID_PERSONNE"/>       
                    <one-to-many class="Voiture"/>   
            </set>
    Ca marche et mes objets sont mappés et sauvegardés dans ma BD, mais quand j'enlève le commentaire, l'exécution s'interrompe, et un bug m'apparait en me disant :
    Association references unmapped class : Voiture.
    J'ai un pb dans ce bout de code qui permet le mapping des collections d'objets.

    J'ai tout vérifié : le fichier de configuration, les fichiers de mapping ainsi que les fichiers beans. Je crois que tout est valide.

    Mon fichier "Personne.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
    package domaine;
     
    import java.util.*;
     
    public class Personne {
        private String cin;
        private String nom;
        private String prenom;
        private Set<Voiture> voitures = new HashSet<Voiture>();
        public Personne()
        {
            super();
        }
        public Personne(String cin,String nom ,String prenom)
        {        
            this.setCin(cin);
            this.setNom(nom);
            this.setPrenom(prenom);
        }
        public void setNom(String nom) {
            this.nom = nom;
        }
        public String getNom() {
            return nom;
        }
        public void setPrenom(String prenom) {
            this.prenom = prenom;
        }
        public String getPrenom() {
            return prenom;
        }
        public void setCin(String cin) {
            this.cin = cin;
        }
        public String getCin() {
            return cin;
        } 
        public void setVoitures(Set<Voiture> voitures)
        {
            this.voitures=voitures;
        }
        public void addVoiture(Voiture voiture)
        {
            voiture.setPersonne(this);
            this.voitures.add(voiture);
        }
    }
    Mon fichier "Voiture.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
    package domaine;
     
    public class Voiture {
        private String marque;
        private String matricule;
        private Personne personne;
        public Voiture()
        {
            super();
        }
        public Voiture(String marque,String matricule,Personne personne)
        {    
            this.setMarque(marque);
            this.setMatricule(matricule);
            this.setPersonne(personne);
        }
        public void setMarque(String marque) {
            this.marque = marque;
        }
        public String getMarque() {
            return marque;
        }
        public void setMatricule(String matricule) {
            this.matricule = matricule;
        }
        public String getMatricule() {
            return matricule;
        }
        public void setPersonne(Personne personne) {
            this.personne = personne;
        }
        public Personne getPersonne() {
            return personne;
        }
    }
    Mon mapping file "Personne.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
    20
    21
    22
     
    <?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="domaine.Personne" table="PERSONNE" catalog="test">
     
            <id column="ID_PERSONNE" type="integer">
                <generator class="increment" />
            </id>
            <property name="cin" column="CIN" type="string" length="10" not-null="true"/>
            <property name="nom" column="NOM" type="string" length="10" not-null="true"/>
            <property name="prenom" column="PRENOM" type="string" length="10" not-null="true"/>
            <!-- Le bug existe dans ce bout de code commenté !! -->
            <!-- 
            <set name="voitures" inverse="true" cascade="save-update">
                <key column="ID_PERSONNE"/>        
                <one-to-many class="Voiture"/>    
            </set>
            -->                 
        </class>
    </hibernate-mapping>
    Mon fichier mapping file "Voiture.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
     
    <?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="domaine.Voiture" table="Voiture" catalog="test">
     
            <id column="ID_VOITURE" type="integer">
                <generator class="increment" />
            </id>
            <property name="marque" column="MARQUE" type="string" length="10" not-null="true"/>
            <property name="matricule" column="MATRICULE" type="string" length="10" not-null="true"/>
            <many-to-one name="personne" column="ID_PERSONNE"/>         
        </class>
    </hibernate-mapping>
    Voici les logs que j'obtiens après la compilation/exécution :
    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
    78 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.3.2.GA
    78 [main] INFO org.hibernate.cfg.Environment - hibernate.properties not found
    78 [main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
    94 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
    203 [main] INFO org.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml
    203 [main] INFO org.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml
    328 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource : domaine/Personne.hbm.xml
    437 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: domaine.Personne -> PERSONNE
    453 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource : domaine/Voiture.hbm.xml
    484 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: domaine.Voiture -> Voiture
    578 [main] INFO org.hibernate.cfg.Configuration - Configured SessionFactory: null
    Echec création SessionFactoryorg.hibernate.MappingException: Association references unmapped class: Voiture
    Exception in thread "main" java.lang.ExceptionInInitializerError
        at hibernateUtil.HibernateUtil.<clinit>(HibernateUtil.java:17)
        at domaine.Main.main(Main.java:20)
    Caused by: org.hibernate.MappingException: Association references unmapped class: Voiture
        at org.hibernate.cfg.HbmBinder.bindCollectionSecondPass(HbmBinder.java:2399)
        at org.hibernate.cfg.HbmBinder$CollectionSecondPass.secondPass(HbmBinder.java:2678)
        at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:66)
        at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1177)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1333)
        at hibernateUtil.HibernateUtil.<clinit>(HibernateUtil.java:13)
        ... 1 more
    Voici mon fichier de configuration "hibernate.cfg.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
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
     
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
        <session-factory>
            <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="hibernate.connection.password">root</property>
            <property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
            <property name="hibernate.connection.username">root</property>
            <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
     
               <!-- Enable Hibernate's automatic session context management -->
            <property name="current_session_context_class">thread</property>
     
            <!-- Cache niveau 2 EHCache-->  
            <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
     
            <!-- Utiliser le schéma de base de données existant au démarrage (utile pour tester chargement())
            <property name="hbm2ddl.auto">update</property>
            -->
     
            <!-- Formater le SQL de sortie -->
            <property name="hibernate.format_sql">true</property>
     
            <!-- Supprimer et re-créée le schéma de base de données au démarrage (utile pour tester insertion())-->
            <property name="hbm2ddl.auto">create</property>
     
            <!-- Montrer toutes les réquêtes générées -->
            <property name="show_sql">true</property>
     
            <property name="hibernate.generate_statistics">true</property>
     
            <mapping resource="domaine/Personne.hbm.xml"/>        
            <mapping resource="domaine/Voiture.hbm.xml"/>
        </session-factory>
    </hibernate-configuration>
    Quelqu'un saurait-il me dire d'où vient le problème ?

    Merci d'avance pour votre aide.

  2. #2
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2011
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2011
    Messages : 3
    Par défaut
    Est ce que t'as fait attention aux package des classes que tu références ? Parce qu'il me semble que dans "Personne.hbm.xml" tu dois mettre <one-to-many class="domaine.Voiture"/>

  3. #3
    Membre averti
    Inscrit en
    Août 2010
    Messages
    36
    Détails du profil
    Informations forums :
    Inscription : Août 2010
    Messages : 36
    Par défaut
    Oui tu as raison Ysf.
    Merci pour ta réponse, en effet il fallait que j'indique le package auquel appartient ma classe à mapper même si toutes mes classes sont incluses dans le même package.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 6
    Dernier message: 24/03/2006, 09h22
  2. Problème de gestion d'une collection d'objet
    Par poolky dans le forum VB 6 et antérieur
    Réponses: 3
    Dernier message: 17/02/2006, 21h51
  3. [Struts] <logic:iterate> sur une collection d objets c
    Par trax020 dans le forum Struts 1
    Réponses: 2
    Dernier message: 12/05/2005, 00h11
  4. Probléme collection d'objets
    Par Contrec dans le forum MFC
    Réponses: 1
    Dernier message: 14/04/2005, 10h08
  5. [VB6] Sauvegarder une collection d'objets
    Par Sayagh dans le forum VB 6 et antérieur
    Réponses: 7
    Dernier message: 19/09/2003, 11h58

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