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 :

Probleme de mis a jour ManyToMany


Sujet :

JPA Java

  1. #1
    Membre actif Avatar de kore62
    Profil pro
    Inscrit en
    Août 2007
    Messages
    222
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : France

    Informations forums :
    Inscription : Août 2007
    Messages : 222
    Points : 205
    Points
    205
    Par défaut Probleme de mis a jour ManyToMany
    En fait j'ai un objet contact qui possede une collection d'objets livre...
    Je creer mon objet contact je lui met deux livres dans sa collection et je le met en base avec la methode persist().
    Jusque là tous va bien...

    Le truc c'est que j'ai créé une fonction qui me permet d'ajouter un livre à un contact existent à sa collection de livre..

    Ces deux objets ont une relation manyToMany avec le cascadeType.ALL

    Mais lorsque je veux met ma collection a jour dans l'objet contact et que je fait em.merge(objetContact); il ne met pas à jour ma liste de livre dans ma table de jointure... Comment puis je faire???

    Voici les codes de mes entitybean: (ce sont dfes entity à titre d'exemple afin de tester les relations oneToone, oneTomany, manyTomany..)

    Entity Contact:
    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
     
    package com.labosun.cj.ejb3.entity;
     
     
     
    import java.util.Collection;
    import javax.persistence.CascadeType;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.ManyToMany;
    import javax.persistence.OneToMany;
    import javax.persistence.OneToOne;
     
    /**
    * Entity Bean Contact
    * @author Cyril
    *
    */
    @Entity // Annotation indiquant que la classe est un entity bean
    public class Contact {
     
    private int id;
     
    private String firstname;
     
    private String lastname;
     
    private String address;
     
    private int zipCode;
     
    private String city;
     
    private String phone;
     
    private String mobile;
     
    private CarteCredit carteContact;
     
    private Collection<Voiture> voitures;
     
    private Collection<Livre> livres;
     
    public Contact() {
    }
     
    @Id // tag définissant la clé primaire
    @GeneratedValue(strategy=GenerationType.AUTO) // tag indiquant que la clé est auto générée
    public int getId() {
    return id;
    }
     
    public void setId(int id) {
    this.id = id;
    }
     
    public String getAddress() {
    return address;
    }
     
    public void setAddress(String address) {
    this.address = address;
    }
     
    public String getCity() {
    return city;
    }
     
    public void setCity(String city) {
    this.city = city;
    }
     
    public String getFirstname() {
    return firstname;
    }
     
    public void setFirstname(String firstname) {
    this.firstname = firstname;
    }
     
    public String getLastname() {
    return lastname;
    }
     
    public void setLastname(String lastname) {
    this.lastname = lastname;
    }
     
    public String getMobile() {
    return mobile;
    }
     
    public void setMobile(String mobile) {
    this.mobile = mobile;
    }
     
    public String getPhone() {
    return phone;
    }
     
    public void setPhone(String phone) {
    this.phone = phone;
    }
     
    public int getZipCode() {
    return zipCode;
    }
     
    public void setZipCode(int zipCode) {
    this.zipCode = zipCode;
    }
     
    @OneToOne(cascade={CascadeType.ALL})
    @JoinColumn(name="cartecredit_id", referencedColumnName="id")
    public CarteCredit getCarteContact() {
    	return carteContact;
    }
     
    public void setCarteContact(CarteCredit carteContact) {
    	this.carteContact = carteContact;
    }
     
    @OneToMany(mappedBy = "proprietaire", cascade = CascadeType.ALL)
    public Collection<Voiture> getVoitures() {
    	return voitures;
    }
     
    public void setVoitures(Collection<Voiture> voitures) {
    	this.voitures = voitures;
    }
     
    @ManyToMany(mappedBy = "lecteurs", cascade = {CascadeType.ALL})
    public Collection<Livre> getLivres() {
    	return livres;
    }
     
    public void setLivres(Collection<Livre> livres) {
    	this.livres = livres;
    }
    }
    Entity Livre:
    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
    package com.labosun.cj.ejb3.entity;
     
    import java.util.Collection;
     
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.ManyToMany;
     
    @Entity
    public class Livre {
     
    private int id;	
     
    private Collection<Contact> lecteurs;
     
    private String titre;
     
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    public int getId() {
    	return id;
    }
     
    public void setId(int id) {
    	this.id = id;
    }
     
    @ManyToMany
    public Collection<Contact> getLecteurs() {
    	return lecteurs;
    }
     
    public void setLecteurs(Collection<Contact> lecteurs) {
    	this.lecteurs = lecteurs;
    }
     
    public String getTitre() {
    	return titre;
    }
     
    public void setTitre(String titre) {
    	this.titre = titre;
    }
    }
    Ma fonction D'ajout d'un livre dans l'entityManager:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    public Livre findLivre(int idLivre){
    	 Livre l = em.find(Livre.class, idLivre);
    	 System.out.println("Le titre de livre retrouvé est:" + l.getTitre());
    	 return l;
    }
     
    public Contact ajouterLivreContact(int idLivre, Contact monContact){
    	Livre l = findLivre(3);
    	monContact.getLivres().add(l);
    	Contact monContact2 = em.merge(monContact);
    	return monContact2;
    }
    En base de donnees mes entity sont representés de la sorte:
    table Livre : (id, titre)
    table Contact: (id,nom,etc...)
    table jointure: contact_livre(livres_id, lecteurs_id);

    Quelqu'un peut il me dire comment mettre à jour la table de jointure Contact_livre?

  2. #2
    Membre actif Avatar de kore62
    Profil pro
    Inscrit en
    Août 2007
    Messages
    222
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : France

    Informations forums :
    Inscription : Août 2007
    Messages : 222
    Points : 205
    Points
    205
    Par défaut
    Bon j'ai trouvé l'erreur, c'était tout con mais subtile quand même... lol
    En fait dans mon objet livre j'ai une collection de lecteurs (=collection d'objet contact)
    Et dans mon objet contact j'ai une collection de livre (donc relation manytomany)

    Ce que je faisais:
    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
     
    public Livre findLivre(int idLivre){
    	 Livre l = em.find(Livre.class, idLivre);
    	 return l;
    }
     
    public Contact ajouterLivreContact(int idLivre, Contact monContact){
                 //je cherche le livre d'indice 3
    	Livre l = findLivre(3);
     
                 //Je recupere la collection de livre deja dans l'objet contact
    	Collection<Livre> mesLivres = monContact.getLivres();
     
                 //J'ajoute mon livre au contact
    	mesLivres.add(l);
    	monContact.setLivres(mesLivres);
     
                //Je met à jour le contact
    	Contact monContact2 = em.merge(monContact);
    	return monContact2;
    }
    En fait j'ajoute le livre à ma collection de livre de mon contact mais le probleme est que je n'avait pas mis à jour ma liste de lecteurs de mon livre donc pour l'objet livre (0 lecteur!)
    Donc lorsque je fait le merge avec cascadetype.all je met à jour les relations le problème c'est que le manager ne detectait pas le fait qu'un nouveau lecteur étaient dans mon objet livre (vu qu'il n'avait pas été ajouté!)
    donc pas de mise à jour à faire dans la table de jointure!

    D'où le code solution suivant:

    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
    public Livre findLivre(int idLivre){
    	 Livre l = em.find(Livre.class, idLivre);
    	 System.out.println("Le titre de livre retrouvé est:" + l.getTitre());
    	 return l;
    }
    
    public Contact ajouterLivreContact(int idLivre, Contact monContact){
    	Livre l = findLivre(3);
    	l.getLecteurs().add(monContact);
    	Collection<Livre> mesLivres = monContact.getLivres();
    	mesLivres.add(l);
    	monContact.setLivres(mesLivres);
    	Contact monContact2 = em.merge(monContact);
    	return monContact2;
    }
    Maintenant la solution propre:
    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
     
    public Livre findLivre(int idLivre){
    	return em.find(Livre.class, idLivre);
     
    }
     
    public Contact ajouterLivreContact(int idLivre, Contact monContact){
     
    	//On recupere le livre d'indice 3
    	Livre l = findLivre(3);
     
    	//On ajoute le lecteur au livre
    	l.getLecteurs().add(monContact);
     
    	//On ajoute le livre au lecteur
    	monContact.getLivres().add(l);
     
    	//On met à jour la table de jointure (par le biais de l'objet monContact)
    	monContact = em.merge(monContact);
    	return monContact;
    }

    Voili voilou, j'ai détaillé pour ceux qui aurait le même problème

  3. #3
    Expert confirmé
    Avatar de Valère
    Profil pro
    Inscrit en
    Août 2005
    Messages
    1 334
    Détails du profil
    Informations personnelles :
    Âge : 49
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations forums :
    Inscription : Août 2005
    Messages : 1 334
    Points : 4 740
    Points
    4 740
    Par défaut
    Merci pour ta contribution!

  4. #4
    Membre du Club
    Profil pro
    Inscrit en
    Février 2007
    Messages
    64
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2007
    Messages : 64
    Points : 54
    Points
    54
    Par défaut
    merci beaucoup

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

Discussions similaires

  1. Réponses: 9
    Dernier message: 09/10/2009, 12h58
  2. [EJB3 Entity] Probleme de mis a jour ManyToMany
    Par doblern dans le forum Java EE
    Réponses: 3
    Dernier message: 12/04/2008, 19h26
  3. probleme apres mis a jour
    Par Blo0d4x3 dans le forum SQL Procédural
    Réponses: 1
    Dernier message: 18/08/2006, 21h36
  4. Probleme servlet mis a jours
    Par yuriashford dans le forum Servlets/JSP
    Réponses: 2
    Dernier message: 24/04/2006, 15h03
  5. [] [Install] Problème de mise à jour des dll
    Par pepper dans le forum Installation, Déploiement et Sécurité
    Réponses: 4
    Dernier message: 23/01/2003, 22h34

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