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

JPA Java Discussion :

"Unable to configure Entity Manager"


Sujet :

JPA Java

  1. #1
    Membre à l'essai
    Profil pro
    Inscrit en
    Janvier 2010
    Messages
    19
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2010
    Messages : 19
    Points : 14
    Points
    14
    Par défaut "Unable to configure Entity Manager"
    Bonjour à tous,

    Je bosse sur un projet Wicket et j'en suis à la phase de persistance des données. Malheureusement j'ai le gros message d'erreur ci-dessous.
    J'avais pourtant réussi à créer la BDD à partir des entities annotées (pas de xml), puis quand j'ai essayé de mettre les relations entre les tables (@OneToOne, etc.) voici ce que ça me dit :

    ATTENTION: failed wicket.SafeDriving: javax.persistence.PersistenceException: [PersistenceUnit: safeDriving-PU] Unable to configure EntityManagerFactory
    20 mai 2011 12:54:35 org.mortbay.log.Slf4jLog warn
    GRAVE: Failed startup of context org.mortbay.jetty.plugin.Jetty6PluginWebAppContext@2c0cf70f{/SafeDriving,/home/GiZeus/NetBeansProjects/SafeDriving/src/main/webapp}
    javax.persistence.PersistenceException: [PersistenceUnit: safeDriving-PU] Unable to configure EntityManagerFactory
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:265)
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:125)
    at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source)
    at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source)
    at com.safedriving.dao.AgenceJpaController.<init>(AgenceJpaController.java:38)
    at com.safedriving.SafeDrivingApplication.populateDataBase(SafeDrivingApplication.java:54)
    at com.safedriving.SafeDrivingApplication.init(SafeDrivingApplication.java:41)
    at org.apache.wicket.protocol.http.WicketFilter.init(WicketFilter.java:726)
    at org.mortbay.jetty.servlet.FilterHolder.doStart(FilterHolder.java:97)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
    at org.mortbay.jetty.servlet.ServletHandler.initialize(ServletHandler.java:713)
    at org.mortbay.jetty.servlet.Context.startContext(Context.java:140)
    at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1282)
    at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:518)
    at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:499)
    at org.mortbay.jetty.plugin.Jetty6PluginWebAppContext.doStart(Jetty6PluginWebAppContext.java:115)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
    at org.mortbay.jetty.handler.HandlerCollection.doStart(HandlerCollection.java:152)
    at org.mortbay.jetty.handler.ContextHandlerCollection.doStart(ContextHandlerCollection.java:156)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
    at org.mortbay.jetty.handler.HandlerCollection.doStart(HandlerCollection.java:152)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
    at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)
    at org.mortbay.jetty.Server.doStart(Server.java:224)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
    at org.mortbay.jetty.plugin.Jetty6PluginServer.start(Jetty6PluginServer.java:132)
    at org.mortbay.jetty.plugin.AbstractJettyMojo.startJetty(AbstractJettyMojo.java:454)
    at org.mortbay.jetty.plugin.AbstractJettyMojo.execute(AbstractJettyMojo.java:396)
    at org.mortbay.jetty.plugin.AbstractJettyRunMojo.execute(AbstractJettyRunMojo.java:210)
    at org.mortbay.jetty.plugin.Jetty6RunMojo.execute(Jetty6RunMojo.java:184)
    at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:694)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeStandaloneGoal(DefaultLifecycleExecutor.java:569)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:539)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:348)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
    at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
    at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
    at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
    at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
    Caused by: org.hibernate.AnnotationException: Cannot find the expected secondary table: no Client available for com.safedriving.beans.PanierClient
    at org.hibernate.cfg.Ejb3Column.getJoin(Ejb3Column.java:293)
    at org.hibernate.cfg.Ejb3Column.getTable(Ejb3Column.java:272)
    at org.hibernate.cfg.AnnotationBinder.bindManyToOne(AnnotationBinder.java:1939)
    at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1361)
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:754)
    at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:546)
    at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:291)
    at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1115)
    at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1226)
    at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:173)
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:854)
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:191)
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:253)
    ... 48 more
    20 mai 2011 12:54:35 org.mortbay.log.Slf4jLog info

    J'utilise les classes générées par Netbeans pour la persistance, qui avaient bien fonctionné jusque là.

    Dans le message d'erreur, il me dit que le problème viendrait du constructeur de la classe AgenceJPAController (première dao à être générée dans ma fonction populateDB() chargée dans l'init de la classe d'application de wicket).

    Voici son code :

    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
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    public class AgenceJpaController {
     
        public AgenceJpaController() {
            /*
            *   ERREUR REMONTÉE PAR MAVEN
             */
            emf = Persistence.createEntityManagerFactory("safeDriving-PU");
        }
        private EntityManagerFactory emf = null;
     
        public EntityManager getEntityManager() {
            return emf.createEntityManager();
        }
     
        public void create(Agence agence) {
            EntityManager em = null;
            try {
                em = getEntityManager();
                em.getTransaction().begin();
                em.persist(agence);
                em.getTransaction().commit();
            } finally {
                if (em != null) {
                    em.close();
                }
            }
        }
     
        public void edit(Agence agence) throws NonexistentEntityException, Exception {
            EntityManager em = null;
            try {
                em = getEntityManager();
                em.getTransaction().begin();
                agence = em.merge(agence);
                em.getTransaction().commit();
            } catch (Exception ex) {
                String msg = ex.getLocalizedMessage();
                if (msg == null || msg.length() == 0) {
                    int id = agence.getId();
                    if (findAgence(id) == null) {
                        throw new NonexistentEntityException("The agence with id " + id + " no longer exists.");
                    }
                }
                throw ex;
            } finally {
                if (em != null) {
                    em.close();
                }
            }
        }
     
        public void destroy(int id) throws NonexistentEntityException {
            EntityManager em = null;
            try {
                em = getEntityManager();
                em.getTransaction().begin();
                Agence agence;
                try {
                    agence = em.getReference(Agence.class, id);
                    agence.getId();
                } catch (EntityNotFoundException enfe) {
                    throw new NonexistentEntityException("The agence with id " + id + " no longer exists.", enfe);
                }
                em.remove(agence);
                em.getTransaction().commit();
            } finally {
                if (em != null) {
                    em.close();
                }
            }
        }
     
        public List<Agence> findAgenceEntities() {
            return findAgenceEntities(true, -1, -1);
        }
     
        public List<Agence> findAgenceEntities(int maxResults, int firstResult) {
            return findAgenceEntities(false, maxResults, firstResult);
        }
     
        private List<Agence> findAgenceEntities(boolean all, int maxResults, int firstResult) {
            EntityManager em = getEntityManager();
            try {
                CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
                cq.select(cq.from(Agence.class));
                Query q = em.createQuery(cq);
                if (!all) {
                    q.setMaxResults(maxResults);
                    q.setFirstResult(firstResult);
                }
                return q.getResultList();
            } finally {
                em.close();
            }
        }
     
        public Agence findAgence(int id) {
            EntityManager em = getEntityManager();
            try {
                return em.find(Agence.class, id);
            } finally {
                em.close();
            }
        }
     
        public int getAgenceCount() {
            EntityManager em = getEntityManager();
            try {
                CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
                Root<Agence> rt = cq.from(Agence.class);
                cq.select(em.getCriteriaBuilder().count(rt));
                Query q = em.createQuery(cq);
                return ((Long) q.getSingleResult()).intValue();
            } finally {
                em.close();
            }
        }
     
    }

    Après pas mal de recherches, je n'ai pas trouvé ce qui n'allait pas. Je penche pour une mauvaise utilisation des annotation (pourtant le bean de cette classe n'est propriétaire d'aucune relation.

    Quelqu'un a une idée ?


    PS : Au cas où mes relations ManyToOne et autres seraient fausses, est-il possible de spécifier uniquement la table @JoinColumn pour indiquer une clé étrangère sans spécifier de relation ?

  2. #2
    Modérateur
    Avatar de OButterlin
    Homme Profil pro
    Inscrit en
    Novembre 2006
    Messages
    7 310
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Novembre 2006
    Messages : 7 310
    Points : 9 522
    Points
    9 522
    Billets dans le blog
    1
    Par défaut
    Peux-tu montrer le persistence.xml ?
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  3. #3
    Membre régulier
    Homme Profil pro
    Inscrit en
    Mai 2011
    Messages
    61
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mai 2011
    Messages : 61
    Points : 70
    Points
    70
    Par défaut
    Caused by: org.hibernate.AnnotationException: Cannot find the expected secondary table: no Client available for com.safedriving.beans.PanierClient
    Il ne trouve pas votre classe PanierClient.

    Le problème provient souvent d'un mauvais mapping ou de l'utilisation de @Column( table ="matable") sans spécifier le nom sql du champ

    @Column( name ="monchamp", table ="matable")

    EDIT : Pouvez vous nous montrer les deux classes ?

  4. #4
    Membre à l'essai
    Profil pro
    Inscrit en
    Janvier 2010
    Messages
    19
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2010
    Messages : 19
    Points : 14
    Points
    14
    Par défaut
    Merci pour vos réponses. Voici de suite les fichiers demandés :


    La classe PanierClient :

    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
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    @Entity
    public class PanierClient implements Serializable {
        private static final long serialVersionUID = 1L;
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;
     
        @Version
        private int version;
     
        @ManyToOne
        @JoinColumn(table="Client", referencedColumnName="id")
        private Long idClient;
     
        @ManyToOne
        @JoinColumn(table="Offre", referencedColumnName="id")
        private int idOffre;
     
        @Column(length=2, nullable=false)
        private int quantite;
     
        @Column(nullable=false)
        private Timestamp dateSoumission;
     
        @Column(nullable=false)
        private Boolean paye;
     
        public PanierClient(){
        }
     
        public PanierClient(Long idClient, int idOffre, Timestamp dateSoumission, Boolean paye) {
            this.idClient = idClient;
            this.idOffre = idOffre;
            this.dateSoumission = dateSoumission;
            this.paye = paye;
        }
     
     
        public Long getId() {
            return id;
        }
     
        public void setId(Long id) {
            this.id = id;
        }
     
        public Timestamp getDateSoumission() {
            return dateSoumission;
        }
     
        public void setDateSoumission(Timestamp dateSoumission) {
            this.dateSoumission = dateSoumission;
        }
     
        public Long getIdClient() {
            return idClient;
        }
     
        public void setIdClient(Long idClient) {
            this.idClient = idClient;
        }
     
        public int getIdOffre() {
            return idOffre;
        }
     
        public void setIdOffre(int idOffre) {
            this.idOffre = idOffre;
        }
     
        public Boolean getPaye() {
            return paye;
        }
     
        public void setPaye(Boolean paye) {
            this.paye = paye;
        }
     
        public int getQuantite() {
            return quantite;
        }
     
        public void setQuantite(int quantite) {
            this.quantite = quantite;
        }
     
        public int getVersion() {
            return version;
        }
     
        public void setVersion(int version) {
            this.version = version;
        }
     
     
     
        @Override
        public int hashCode() {
            int hash = 0;
            hash += (id != null ? id.hashCode() : 0);
            return hash;
        }
     
        @Override
        public boolean equals(Object object) {
            // TODO: Warning - this method won't work in the case the id fields are not set
            if (!(object instanceof PanierClient)) {
                return false;
            }
            PanierClient other = (PanierClient) object;
            if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
                return false;
            }
            return true;
        }
     
        @Override
        public String toString() {
            return "com.safedriving.beans.PanierClient[id=" + id + "]";
        }
     
    }

    La classe Personne, dont hérite Client :

    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
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    @Entity
    public abstract class Personne implements Serializable {
        private static final long serialVersionUID = 1L;
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        protected Long id;
     
        @Version
        protected Long version;
     
        @Column(length=20)
        protected String login;
     
        @Column(length=50)
        protected String password;
     
        @Column(length=32)
        protected String nom;
     
        @Column(length=32)
        protected String prenom;
     
        @Column(length=1)
        protected char sexe;
     
        @Temporal(javax.persistence.TemporalType.DATE)
        protected Date naissance;
     
        @Column(length=60)
        protected String adresse;
     
        @Column(length=32)
        protected String ville;
     
        @Column(length=15)
        protected String telephone;
     
        @Column(length=50)
        protected String email;
     
     
        public Long getId() {
            return id;
        }
     
        public void setId(Long id) {
            this.id = id;
        }
     
        public String getAdresse() {
            return adresse;
        }
     
        public void setAdresse(String adresse) {
            this.adresse = adresse;
        }
     
        public String getEmail() {
            return email;
        }
     
        public void setEmail(String email) {
            this.email = email;
        }
     
        public String getLogin() {
            return login;
        }
     
        public void setLogin(String login) {
            this.login = login;
        }
     
        public Date getNaissance() {
            return naissance;
        }
     
        public void setNaissance(Date naissance) {
            this.naissance = naissance;
        }
     
        public String getNom() {
            return nom;
        }
     
        public void setNom(String nom) {
            this.nom = nom;
        }
     
        public String getPassword() {
            return password;
        }
     
        public void setPassword(String password) {
            this.password = password;
        }
     
        public String getPrenom() {
            return prenom;
        }
     
        public void setPrenom(String prenom) {
            this.prenom = prenom;
        }
     
        public char getSexe() {
            return sexe;
        }
     
        public void setSexe(char sexe) {
            this.sexe = sexe;
        }
     
        public String getTelephone() {
            return telephone;
        }
     
        public void setTelephone(String telephone) {
            this.telephone = telephone;
        }
     
        public Long getVersion() {
            return version;
        }
     
        public void setVersion(Long version) {
            this.version = version;
        }
     
        public String getVille() {
            return ville;
        }
     
        public void setVille(String ville) {
            this.ville = ville;
        }
     
     
     
     
        @Override
        public int hashCode() {
            int hash = 0;
            hash += (id != null ? id.hashCode() : 0);
            return hash;
        }
     
        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final Personne other = (Personne) obj;
            if (this.version != other.version && (this.version == null || !this.version.equals(other.version))) {
                return false;
            }
            if ((this.login == null) ? (other.login != null) : !this.login.equals(other.login)) {
                return false;
            }
            if ((this.password == null) ? (other.password != null) : !this.password.equals(other.password)) {
                return false;
            }
            if ((this.nom == null) ? (other.nom != null) : !this.nom.equals(other.nom)) {
                return false;
            }
            if ((this.prenom == null) ? (other.prenom != null) : !this.prenom.equals(other.prenom)) {
                return false;
            }
            if (this.sexe != other.sexe) {
                return false;
            }
            if (this.naissance != other.naissance && (this.naissance == null || !this.naissance.equals(other.naissance))) {
                return false;
            }
            if ((this.adresse == null) ? (other.adresse != null) : !this.adresse.equals(other.adresse)) {
                return false;
            }
            if ((this.ville == null) ? (other.ville != null) : !this.ville.equals(other.ville)) {
                return false;
            }
            if ((this.telephone == null) ? (other.telephone != null) : !this.telephone.equals(other.telephone)) {
                return false;
            }
            if ((this.email == null) ? (other.email != null) : !this.email.equals(other.email)) {
                return false;
            }
            return true;
        }
     
     
     
        @Override
        public String toString() {
            return "com.safedriving.beans.personnes.Personne[id=" + id + "]";
        }
     
    }

    La classe Client :

    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
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    @Entity
    public class Client extends Personne{
     
        @ManyToOne
        @JoinColumn(table="Agence", referencedColumnName="id")
        private int idAgence;
     
        @Temporal(javax.persistence.TemporalType.DATE)
        @Column(nullable=false)
        private Date dateInscription;
     
        @Temporal(javax.persistence.TemporalType.DATE)
        @Column(nullable=true)
        private Date dateCodeRoute;
     
        @ManyToOne
        @JoinColumn(table="Commercial", referencedColumnName="id")
        private int idCommercial;
     
        @ManyToMany
        @JoinTable(name="HistoriqueClient", joinColumns=@JoinColumn(name="idClient"),
                    inverseJoinColumns=@JoinColumn(name="idSessionFormation"))
        private Collection<SessionFormation> sessionsFormation;
     
     
     
        public Client(){
        }
     
        public Client(int idAgence, String nom, String prenom, char sexe, Date naissance,
                String adresse, String ville, String telephone, String email, Date dateInscription,
                Date dateCodeRoute, int idCommercial){
     
            this.idAgence=idAgence;
            this.nom=nom;
            this.prenom=prenom;
            this.sexe=sexe;
            this.naissance=naissance;
            this.adresse=adresse;
            this.ville=ville;
            this.telephone=telephone;
            this.email=email;
            this.dateInscription=dateInscription;
            this.dateCodeRoute=dateCodeRoute;
            this.idCommercial = idCommercial;
     
        }
     
     
     
        public Date getDateCodeRoute() {
            return dateCodeRoute;
        }
     
        public void setDateCodeRoute(Date dateCodeRoute) {
            this.dateCodeRoute = dateCodeRoute;
        }
     
        public Date getDateInscription() {
            return dateInscription;
        }
     
        public void setDateInscription(Date dateInscription) {
            this.dateInscription = dateInscription;
        }
     
        public int getIdAgence() {
            return idAgence;
        }
     
        public void setIdAgence(int idAgence) {
            this.idAgence = idAgence;
        }
     
        public int getIdCommercial() {
            return idCommercial;
        }
     
        public void setIdCommercial(int idCommercial) {
            this.idCommercial = idCommercial;
        }
     
        public Collection<SessionFormation> getSessionsFormation() {
            return sessionsFormation;
        }
     
        public void setSessionsFormation(Collection<SessionFormation> sessionsFormation) {
            this.sessionsFormation = sessionsFormation;
        }
     
     
        @Override
        public int hashCode() {
            int hash = 7;
            hash = 73 * hash + this.idAgence;
            hash = 73 * hash + (this.dateInscription != null ? this.dateInscription.hashCode() : 0);
            hash = 73 * hash + (this.dateCodeRoute != null ? this.dateCodeRoute.hashCode() : 0);
            hash = 73 * hash + this.idCommercial;
            return hash;
        }
     
        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final Client other = (Client) obj;
            if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
                return false;
            }
            if ((this.login == null) ? (other.login != null) : !this.login.equals(other.login)) {
                return false;
            }
            if ((this.password == null) ? (other.password != null) : !this.password.equals(other.password)) {
                return false;
            }
            if ((this.nom == null) ? (other.nom != null) : !this.nom.equals(other.nom)) {
                return false;
            }
            if ((this.prenom == null) ? (other.prenom != null) : !this.prenom.equals(other.prenom)) {
                return false;
            }
            if (this.sexe != other.sexe) {
                return false;
            }
            if (this.naissance != other.naissance && (this.naissance == null || !this.naissance.equals(other.naissance))) {
                return false;
            }
            if ((this.adresse == null) ? (other.adresse != null) : !this.adresse.equals(other.adresse)) {
                return false;
            }
            if ((this.ville == null) ? (other.ville != null) : !this.ville.equals(other.ville)) {
                return false;
            }
            if ((this.telephone == null) ? (other.telephone != null) : !this.telephone.equals(other.telephone)) {
                return false;
            }
            if ((this.email == null) ? (other.email != null) : !this.email.equals(other.email)) {
                return false;
            }
            if (this.idAgence != other.idAgence) {
                return false;
            }
            if (this.dateInscription != other.dateInscription && (this.dateInscription == null || !this.dateInscription.equals(other.dateInscription))) {
                return false;
            }
            if (this.dateCodeRoute != other.dateCodeRoute && (this.dateCodeRoute == null || !this.dateCodeRoute.equals(other.dateCodeRoute))) {
                return false;
            }
            if (this.idCommercial != other.idCommercial) {
                return false;
            }
            return true;
        }
     
        @Override
        public String toString() {
            return "nom du client : "+prenom+" "+nom;
            //return "com.safedriving.beans.personnes.Client[id=" + id + "]";
        }
     
    }
    La classe Agence :

    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
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    @Entity
    public class Agence implements Serializable{
        @Id
        @GeneratedValue
        @Column(length=3)
        private int id;
     
        @Version
        private int version;
     
        @Column(length=32)
        private String nom;
        @Column(length=60)
        private String adresse;
        @Column(length=32)
        private String ville;
        @Column(length=15)
        private String telephone;
        @Column(length=50)
        private String email;
        @Column(length=2)
        private int nbPlaces;
     
        public Agence(){
        }
     
        public Agence(String nom, String adresse, String ville, String telephone, String email, int nbPlaces) {
            this.nom = nom;
            this.adresse = adresse;
            this.ville = ville;
            this.telephone = telephone;
            this.email = email;
            this.nbPlaces = nbPlaces;
        }
     
     
     
        public String getAdresse() {
            return adresse;
        }
     
        public void setAdresse(String adresse) {
            this.adresse = adresse;
        }
     
        public int getId() {
            return id;
        }
     
        public void setId(int id) {
            this.id = id;
        }
     
        public int getNbPlaces() {
            return nbPlaces;
        }
     
        public void setNbPlaces(int nbPlaces) {
            this.nbPlaces = nbPlaces;
        }
     
        public String getNom() {
            return nom;
        }
     
        public void setNom(String nom) {
            this.nom = nom;
        }
     
        public String getTelephone() {
            return telephone;
        }
     
        public void setTelephone(String telephone) {
            this.telephone = telephone;
        }
     
        public String getVille() {
            return ville;
        }
     
        public void setVille(String ville) {
            this.ville = ville;
        }
     
        @Override
        public int hashCode() {
            int hash = 7;
            hash = 37 * hash + this.id;
            hash = 37 * hash + this.version;
            hash = 37 * hash + (this.nom != null ? this.nom.hashCode() : 0);
            hash = 37 * hash + (this.adresse != null ? this.adresse.hashCode() : 0);
            hash = 37 * hash + (this.ville != null ? this.ville.hashCode() : 0);
            hash = 37 * hash + (this.telephone != null ? this.telephone.hashCode() : 0);
            hash = 37 * hash + (this.email != null ? this.email.hashCode() : 0);
            hash = 37 * hash + this.nbPlaces;
            return hash;
        }
     
        @Override
        public boolean equals(Object object) {
            // TODO: Warning - this method won't work in the case the id fields are not set
            if (!(object instanceof Agence)) {
                return false;
            }
            Agence other = (Agence) object;
            if (this.id != other.id) {
                return false;
            }
            return true;
        }
     
        @Override
        public String toString() {
            return "Agence{" + "id=" + id + "version=" + version + "nom=" + nom + "adresse=" + adresse + "ville=" + ville + "telephone=" + telephone + "email=" + email + "nbPlaces=" + nbPlaces + '}';
        }
     
     
    }

    Et le persistence.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" encoding="UTF-8"?>
    <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
      <persistence-unit name="safeDriving-PU" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
          <!-- Autodétection des classes à persister -->
          <property name="hibernate.archive.autodetection" value="class, hbm"/>
          <!-- connexion JDBC -->
          <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
          <property name="hibernate.connection.url" value="jdbc:mysql://localhost/safeDriving"/>
          <property name="hibernate.connection.username" value="root"/>
          <property name="hibernate.connection.password" value="root"/>
          <!-- création automatique du schéma -->
          <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
          <!-- dialecte -->
          <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
        </properties>
      </persistence-unit>
    </persistence>

  5. #5
    Expert confirmé
    Homme Profil pro
    Inscrit en
    Septembre 2006
    Messages
    2 936
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Septembre 2006
    Messages : 2 936
    Points : 4 356
    Points
    4 356
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    @ManyToOne
    @JoinColumn(table="Client", referencedColumnName="id")
    private Long idClient;
    Long n'est pas une @Entity.
    c'est Client qu'il faut mettre : pas le type de sa PK.

  6. #6
    Membre à l'essai
    Profil pro
    Inscrit en
    Janvier 2010
    Messages
    19
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2010
    Messages : 19
    Points : 14
    Points
    14
    Par défaut
    J'ai eu le temps de tester quelques petites choses qui se sont révélées plutôt instructives :
    En supprimant les annotations @OneToMany, @ManyToOne, mais en laissant l'annotation @JoinCOlumn, ça semble passer (maven me sort un autre message d'erreur).

    Je sais que je pourrais supprimer ces annotations en cas de besoin (débutant là-dessus, et pas trop le temps de plancher), mais j'aimerais savoir avant tout quelles seraient les conséquences. Je préfèrerais faire quelque chose de propre.

  7. #7
    Membre à l'essai
    Profil pro
    Inscrit en
    Janvier 2010
    Messages
    19
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2010
    Messages : 19
    Points : 14
    Points
    14
    Par défaut
    Citation Envoyé par JeitEmgie Voir le message
    Long n'est pas une @Entity.
    c'est Client qu'il faut mettre : pas le type de sa PK.
    Messages croisés, dsl.

    Je crois avoir compris. J'ai remplacé "private int idClient" par "private Client idClient". Il faut indiquer le type de l'entité que je souhaite voir référencée. Bigre ça ne me facilite pas la tâche pour plus tard, mais merci du coup de pouce

  8. #8
    Membre à l'essai
    Profil pro
    Inscrit en
    Janvier 2010
    Messages
    19
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2010
    Messages : 19
    Points : 14
    Points
    14
    Par défaut
    J'ai toujours cette erreur avec PanierClient, même en ayant modifié certains paramètres :

    Caused by: org.hibernate.AnnotationException: Cannot find the expected secondary table: no Offre available for com.safedriving.beans.PanierClient
    Classe PanierClient :

    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
    @Entity
    public class PanierClient implements Serializable {
        private static final long serialVersionUID = 1L;
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;
     
        @Version
        private int version;
     
        @ManyToOne
        @JoinColumn(name="idAgence", referencedColumnName="id")
        private Client client;
     
        @ManyToOne
        @JoinColumn(table="Offre")
        private Offre offre;
     
        @Column(length=2, nullable=false)
        private int quantite;
     
        @Column(nullable=false)
        private Timestamp dateSoumission;
     
        @Column(nullable=false)
        private Boolean paye;
     
        public PanierClient(){
        }
     
        public PanierClient(Client client, Offre offre, Timestamp dateSoumission, Boolean paye) {
            this.client = client;
            this.offre = offre;
            this.dateSoumission = dateSoumission;
            this.paye = paye;
        }

  9. #9
    Modérateur
    Avatar de OButterlin
    Homme Profil pro
    Inscrit en
    Novembre 2006
    Messages
    7 310
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Novembre 2006
    Messages : 7 310
    Points : 9 522
    Points
    9 522
    Billets dans le blog
    1
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    @ManyToOne     
    @JoinColumn(table="Offre")     
    private Offre offre;
    Tu as correctement mappé Client, pourquoi ne pas faire pareil avec Offre...
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  10. #10
    Membre à l'essai
    Profil pro
    Inscrit en
    Janvier 2010
    Messages
    19
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2010
    Messages : 19
    Points : 14
    Points
    14
    Par défaut
    Merci pour ta réponse, et j'ai maintenant une nouvelle erreur :

    Caused by: org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: com.safedriving.beans.Calendrier.client

    Voici mes classes Calendrier et Client (qui hérite de Personne) :

    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
    @Entity
    public class Client extends Personne{
     
        @ManyToOne
        @JoinColumn(name="Agence", referencedColumnName="id")
        private Agence agence;
     
        @Temporal(javax.persistence.TemporalType.DATE)
        @Column(nullable=false)
        private Date dateInscription;
     
        @Temporal(javax.persistence.TemporalType.DATE)
        @Column(nullable=true)
        private Date dateCodeRoute;
     
        @ManyToOne
        @JoinColumn(name="Commercial", referencedColumnName="id")
        private Commercial commercial;
     
        @ManyToMany
        @JoinTable(name="HistoriqueClient", joinColumns=@JoinColumn(name="idClient"),
                    inverseJoinColumns=@JoinColumn(name="idSessionFormation"))
        private Collection<SessionFormation> sessionsFormation;
     
    //Collection rajoutée après qu'il m'ait généré l'erreur remontée, mais ça n'a rien changé 
        @OneToMany(mappedBy="calendrierClient")
        private Collection<Calendrier> calendriers;

    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
    @Entity
    public class Calendrier implements Serializable {
        private static final long serialVersionUID = 1L;
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;
     
        @Version
        private int version;
     
        @OneToMany
        @JoinColumn(name="calendrierClient", referencedColumnName="id")
        @Column(nullable=true)
        private Client client;
     
        @OneToMany
        @JoinColumn(name="calendrierFormateur", referencedColumnName="id")
        @Column(nullable=true)
        private Formateur formateur;
     
        @OneToMany
        @JoinColumn(name="calendrierVehicule", referencedColumnName="id")
        @Column(nullable=true)
        private Vehicule vehicule;
     
        @Temporal(javax.persistence.TemporalType.DATE)
        @Column(nullable=false)
        private Date jour;
     
        @Column(length=2, nullable=false)
        private int heureDebut;
     
        @Column(length=2, nullable=false)
        private int heureFin;

  11. #11
    Modérateur
    Avatar de OButterlin
    Homme Profil pro
    Inscrit en
    Novembre 2006
    Messages
    7 310
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Novembre 2006
    Messages : 7 310
    Points : 9 522
    Points
    9 522
    Billets dans le blog
    1
    Par défaut
    Tu utilises des @OneToMany là où il faut des @ManyToOne (Client, Formateur, Vehicule)
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  12. #12
    Membre à l'essai
    Profil pro
    Inscrit en
    Janvier 2010
    Messages
    19
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2010
    Messages : 19
    Points : 14
    Points
    14
    Par défaut
    Ma foi c'est bien vrai ! Merci pour le coup d'oeil, les miens ne sont plus trop à leur place à force de zigzaguer entre des Many, des One et des @To

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

Discussions similaires

  1. configuration data manager engine/designer
    Par lammiia dans le forum Cognos
    Réponses: 0
    Dernier message: 06/05/2008, 18h29
  2. Réponses: 4
    Dernier message: 01/12/2007, 22h22
  3. Effectuer du JDBC via l'Entity Manager
    Par Claythest dans le forum JPA
    Réponses: 1
    Dernier message: 06/04/2007, 10h22
  4. [EJB3 Entity] Création d'un entity Manager pour transaction
    Par bizet dans le forum Java EE
    Réponses: 4
    Dernier message: 23/02/2007, 08h58
  5. Configuration Oracle Management Server
    Par Laye dans le forum Oracle
    Réponses: 1
    Dernier message: 16/11/2006, 19h06

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