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 :

Problème de mapping avec table d'association


Sujet :

JPA Java

  1. #1
    Membre émérite Avatar de Madfrix
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    2 326
    Détails du profil
    Informations personnelles :
    Localisation : France, Gironde (Aquitaine)

    Informations forums :
    Inscription : Juin 2007
    Messages : 2 326
    Points : 2 566
    Points
    2 566
    Par défaut Problème de mapping avec table d'association
    bonjour,

    étant néophyte avec JPA, j'essaie de créer des exemples de tests afin de me perfectionner dessus.
    Dans ce but, j'ai crée 3 tables : livres, abonnes et emprunts. Chaque abonne peut emprunter plusieurs livres et chaque livre peut être emprunté plusieurs fois. Ma table d'association est donc emprunts.

    Voilà mes entités :

    Code java : 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
     
    package com.entities;
     
    import java.io.Serializable;
     
    import java.util.ArrayList;
    import java.util.List;
     
    import javax.persistence.CascadeType;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.OneToMany;
    import javax.persistence.Table;
     
    /**
     * The persistent class for the abonnes database table.
     * 
     */
    @Entity
    @Table(name="abonnes")
    public class Abonne implements Serializable {
     
    	private static final long serialVersionUID = 1L;
     
    	@Id
    	@GeneratedValue(strategy=GenerationType.AUTO)
    	private int id;
    	private String nom;
    	private String prenom;
     
    	@OneToMany(cascade=CascadeType.ALL, mappedBy="pk.abonne_id")
    	private List<Emprunt> emprunts = new ArrayList<Emprunt>();
     
        public Abonne() {
        }
     
    	public int getId() {
    		return this.id;
    	}
     
    	public void setId(int id) {
    		this.id = id;
    	}
     
    	public String getNom() {
    		return this.nom;
    	}
     
    	public void setNom(String nom) {
    		this.nom = nom;
    	}
     
    	public String getPrenom() {
    		return this.prenom;
    	}
     
    	public void setPrenom(String prenom) {
    		this.prenom = prenom;
    	}
     
    	public List<Emprunt> getEmprunts() {
    		return this.emprunts;
    	}
     
    	public void setEmprunts(List<Emprunt> emprunts) {
    		this.emprunts = emprunts;
    	}

    Code java : 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
     
    package com.entities;
     
    import java.io.Serializable;
     
    import java.util.ArrayList;
    import java.util.List;
     
    import javax.persistence.CascadeType;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.OneToMany;
    import javax.persistence.Table;
     
    /**
     * The persistent class for the livres database table.
     * 
     */
    @Entity
    @Table(name="livres")
    public class Livre implements Serializable {
     
    	private static final long serialVersionUID = 1L;
     
    	@Id
    	@GeneratedValue(strategy=GenerationType.AUTO)
    	private int id;
    	private String libelle;
     
    	@OneToMany(cascade=CascadeType.ALL, mappedBy="pk.livre_id")
    	private List<Emprunt> emprunts = new ArrayList<Emprunt>();
     
        public Livre() {
        }
     
        public Livre(String libelle){
        	this.libelle = libelle;
        }
     
    	public int getId() {
    		return this.id;
    	}
     
    	public void setId(int id) {
    		this.id = id;
    	}
     
    	public String getLibelle() {
    		return this.libelle;
    	}
     
    	public void setLibelle(String libelle) {
    		this.libelle = libelle;
    	}
     
    	public List<Emprunt> getEmprunt() {
    		return emprunts;
    	}
     
    	public void setEmprunt(List<Emprunt> emprunts) {
    		this.emprunts = emprunts;
    	}
     
     
     
    }

    Code java : 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
     
    package com.entities;
     
    import java.io.Serializable;
     
    import javax.persistence.EmbeddedId;
    import javax.persistence.Entity;
     
     
    /**
     * Entity implementation class for Entity: Emprunt
     *
     */
    @Entity
    public class Emprunt implements Serializable {
     
    	private static final long serialVersionUID = 1L;
     
    	@EmbeddedId
    	private EmpruntId pk;	
     
    	public Emprunt() {
    		super();
    	}
     
    	public EmpruntId getPk() {
    		return pk;
    	}
     
    	public void setPk(EmpruntId pk) {
    		this.pk = pk;
    	}
     
    }

    Code java : 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
     
    package com.entities;
     
    import java.io.Serializable;
     
    import javax.persistence.Embeddable;
    import javax.persistence.FetchType;
    import javax.persistence.JoinColumn;
    import javax.persistence.ManyToOne;
     
    @Embeddable
    public class EmpruntId implements Serializable{
     
    	private static final long serialVersionUID = 1L;
     
    	@ManyToOne(fetch=FetchType.EAGER, optional=false)
    	@JoinColumn(name="abonne_id", referencedColumnName="id")
    	private Abonne abonne_id;
     
    	@ManyToOne(fetch=FetchType.EAGER, optional=false)
    	@JoinColumn(name="livre_id", referencedColumnName="id")
    	private Livre livre_id;
     
    	public Abonne getAbonne_id() {
    		return abonne_id;
    	}
     
    	public void setAbonne_id(Abonne abonne_id) {
    		this.abonne_id = abonne_id;
    	}
     
    	public Livre getLivre_id() {
    		return livre_id;
    	}
     
    	public void setLivre_id(Livre livre_id) {
    		this.livre_id = livre_id;
    	}
     
     
    }


    J'ai 1 erreur dans chaque entité Livre et Abonne :

    In attribute 'emprunts', the "mapped by" value 'pk.abonne_id' cannot be resolved to an attribute on the target entity.
    In attribute 'emprunts', the "mapped by" value 'pk.livre_id' cannot be resolved to an attribute on the target entity.
    Je n'arrive pas à faire le pont entre mes entité et l'id correspondant à la clé composite de mes 2 id d'entité...Comment faire cela ?

    Merci beaucoup de votre aide

  2. #2
    Membre émérite Avatar de Madfrix
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    2 326
    Détails du profil
    Informations personnelles :
    Localisation : France, Gironde (Aquitaine)

    Informations forums :
    Inscription : Juin 2007
    Messages : 2 326
    Points : 2 566
    Points
    2 566
    Par défaut
    Bonjour,

    j'ai oublié de préciser que ma table d'association possède 1 colonne supplémentaire (test) en plus des 2 ids formant la clé composite (cas le plus difficile à ce que j'ai pu lire). Via l'assistant JPA, j'ai finalement pu m'en sortir (je pense) en créant les relation suivantes :

    abonnes -> OneToMany -> emprunts
    livres-> OneToMany -> emprunts
    abonnes -> ManyToMany -> livres

    Pouvez vous me confirmer svp que mes entités sont correctement crées et fonctionnelles ? Les voici :

    Abonne.java
    Code java : 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
     
    package com.entities;
     
    import java.io.Serializable;
    import javax.persistence.*;
    import java.util.List;
     
     
    /**
     * The persistent class for the abonnes database table.
     * 
     */
    @Entity
    @Table(name="abonnes")
    public class Abonne implements Serializable {
    	private static final long serialVersionUID = 1L;
     
    	@Id
    	@GeneratedValue(strategy=GenerationType.AUTO)
    	private int id;
     
    	private String nom;
     
    	private String prenom;
     
    	//bi-directional many-to-one association to Emprunt
    	@OneToMany(mappedBy="abonne")
    	private List<Emprunt> emprunts;
     
    	//bi-directional many-to-many association to Livre
    	@ManyToMany(mappedBy="abonnes")
    	private List<Livre> livres;
     
        public Abonne() {
        }
     
    	public int getId() {
    		return this.id;
    	}
     
    	public void setId(int id) {
    		this.id = id;
    	}
     
    	public String getNom() {
    		return this.nom;
    	}
     
    	public void setNom(String nom) {
    		this.nom = nom;
    	}
     
    	public String getPrenom() {
    		return this.prenom;
    	}
     
    	public void setPrenom(String prenom) {
    		this.prenom = prenom;
    	}
     
    	public List<Emprunt> getEmprunts() {
    		return this.emprunts;
    	}
     
    	public void setEmprunts(List<Emprunt> emprunts) {
    		this.emprunts = emprunts;
    	}
     
    	public List<Livre> getLivres() {
    		return this.livres;
    	}
     
    	public void setLivres(List<Livre> livres) {
    		this.livres = livres;
    	}
     
    }


    Livre.java
    Code java : 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
     
    package com.entities;
     
    import java.io.Serializable;
    import javax.persistence.*;
    import java.util.List;
     
     
    /**
     * The persistent class for the livres database table.
     * 
     */
    @Entity
    @Table(name="livres")
    public class Livre implements Serializable {
    	private static final long serialVersionUID = 1L;
     
    	@Id
    	@GeneratedValue(strategy=GenerationType.AUTO)
    	private int id;
     
    	private String libelle;
     
    	//bi-directional many-to-one association to Emprunt
    	@OneToMany(mappedBy="livre")
    	private List<Emprunt> emprunts;
     
    	//bi-directional many-to-many association to Abonne
        @ManyToMany
    	@JoinTable(
    		name="emprunts"
    		, joinColumns={
    			@JoinColumn(name="livre_id")
    			}
    		, inverseJoinColumns={
    			@JoinColumn(name="abonne_id")
    			}
    		)
    	private List<Abonne> abonnes;
     
        public Livre() {
        }
     
    	public int getId() {
    		return this.id;
    	}
     
    	public void setId(int id) {
    		this.id = id;
    	}
     
    	public String getLibelle() {
    		return this.libelle;
    	}
     
    	public void setLibelle(String libelle) {
    		this.libelle = libelle;
    	}
     
    	public List<Emprunt> getEmprunts() {
    		return this.emprunts;
    	}
     
    	public void setEmprunts(List<Emprunt> emprunts) {
    		this.emprunts = emprunts;
    	}
     
    	public List<Abonne> getAbonnes() {
    		return this.abonnes;
    	}
     
    	public void setAbonnes(List<Abonne> abonnes) {
    		this.abonnes = abonnes;
    	}
     
    }


    Emprunt.java
    Code java : 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
     
    package com.entities;
     
    import java.io.Serializable;
    import javax.persistence.*;
     
     
    /**
     * The persistent class for the emprunts database table.
     * 
     */
    @Entity
    @Table(name="emprunts")
    public class Emprunt implements Serializable {
    	private static final long serialVersionUID = 1L;
     
    	@EmbeddedId
    	private EmpruntPK id;
     
    	private String test;
     
    	//bi-directional many-to-one association to Abonne
        @ManyToOne
    	private Abonne abonne;
     
    	//bi-directional many-to-one association to Livre
        @ManyToOne
    	private Livre livre;
     
        public Emprunt() {
        }
     
    	public EmpruntPK getId() {
    		return this.id;
    	}
     
    	public void setId(EmpruntPK id) {
    		this.id = id;
    	}
     
    	public String getTest() {
    		return this.test;
    	}
     
    	public void setTest(String test) {
    		this.test = test;
    	}
     
    	public Abonne getAbonne() {
    		return this.abonne;
    	}
     
    	public void setAbonne(Abonne abonne) {
    		this.abonne = abonne;
    	}
     
    	public Livre getLivre() {
    		return this.livre;
    	}
     
    	public void setLivre(Livre livre) {
    		this.livre = livre;
    	}
     
    }


    EmpruntPK.java
    Code java : 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
     
    package com.entities;
     
    import java.io.Serializable;
    import javax.persistence.*;
     
    /**
     * The primary key class for the emprunts database table.
     * 
     */
    @Embeddable
    public class EmpruntPK implements Serializable {
    	//default serial version id, required for serializable classes.
    	private static final long serialVersionUID = 1L;
     
    	@Column(name="abonne_id")
    	private int abonneId;
     
    	@Column(name="livre_id")
    	private int livreId;
     
        public EmpruntPK() {
        }
    	public int getAbonneId() {
    		return this.abonneId;
    	}
    	public void setAbonneId(int abonneId) {
    		this.abonneId = abonneId;
    	}
    	public int getLivreId() {
    		return this.livreId;
    	}
    	public void setLivreId(int livreId) {
    		this.livreId = livreId;
    	}
     
    	public boolean equals(Object other) {
    		if (this == other) {
    			return true;
    		}
    		if (!(other instanceof EmpruntPK)) {
    			return false;
    		}
    		EmpruntPK castOther = (EmpruntPK)other;
    		return 
    			(this.abonneId == castOther.abonneId)
    			&& (this.livreId == castOther.livreId);
     
        }
     
    	public int hashCode() {
    		final int prime = 31;
    		int hash = 17;
    		hash = hash * prime + this.abonneId;
    		hash = hash * prime + this.livreId;
     
    		return hash;
        }
    }

    J'essaie maintenant d'insérer dans ma table abonne un...abonne ! Je fais donc ceci :

    Code java : 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
     
    ...
    @PersistenceContext
    EntityManager em;
    EntityManagerFactory emf;
    ...
     
    @PostConstruct
    public void construct(){
     	emf = Persistence.createEntityManagerFactory("JPA_PU");
    	em = emf.createEntityManager();	
    }
     
    @Override
    public String test3() {
     
    	Abonne abonne = new Abonne();
     
    	abonne.setNom("Gates");
    	abonne.setPrenom("Bill");
     
    	em.persist(abonne);
     
    	return "ok";
    }

    Mon EntityManager est à priori bien construit vu que j'arrive à lire des données de la base mais impossible d'en insérer, j'obtiens une DatabaseException :

    ...
    Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.DatabaseException
    Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'jpa.sequence' doesn't exist
    Error Code: 1146
    Call: UPDATE SEQUENCE SET SEQ_COUNT = SEQ_COUNT + ? WHERE SEQ_NAME = ?
    bind => [50, SEQ_GEN]
    Query: DataModifyQuery(sql="UPDATE SEQUENCE SET SEQ_COUNT = SEQ_COUNT + ? WHERE SEQ_NAME = ?")
    at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:324)
    ...
    Quelle peut être la raison de cette exception ?


    Merci beaucoup

  3. #3
    Membre émérite Avatar de Madfrix
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    2 326
    Détails du profil
    Informations personnelles :
    Localisation : France, Gironde (Aquitaine)

    Informations forums :
    Inscription : Juin 2007
    Messages : 2 326
    Points : 2 566
    Points
    2 566
    Par défaut
    En rajoutant ceci dans mon persistence.xml, le problème est résolu

    Code xml : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    <properties>
    	<property name="eclipselink.ddl-generation" value="create-tables" />
    </properties>

  4. #4
    En attente de confirmation mail
    Femme Profil pro
    Étudiant
    Inscrit en
    Février 2012
    Messages
    1
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2012
    Messages : 1
    Points : 1
    Points
    1
    Par défaut
    Citation Envoyé par Madfrix Voir le message
    En rajoutant ceci dans mon persistence.xml, le problème est résolu

    Code xml : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    <properties>
    	<property name="eclipselink.ddl-generation" value="create-tables" />
    </properties>
    Merciiiiiiiiiii c'était ce que je cherchais! Tu m'as sauvé la vie

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

Discussions similaires

  1. Problème de mapping avec AMFPHP
    Par 29kiteman dans le forum Flex
    Réponses: 1
    Dernier message: 16/02/2009, 13h53
  2. Problème de filtration avec tables couples
    Par moilou2 dans le forum VBA Access
    Réponses: 4
    Dernier message: 22/07/2008, 12h11
  3. Problème de mapping avec clés composées
    Par goeland444 dans le forum Hibernate
    Réponses: 1
    Dernier message: 06/04/2008, 19h57
  4. [HIBERNATE] requete avec table d'association
    Par zybay dans le forum Hibernate
    Réponses: 1
    Dernier message: 14/06/2007, 12h59
  5. Problème de map avec paramètre template
    Par bouba dans le forum Langage
    Réponses: 5
    Dernier message: 11/05/2007, 13h19

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