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:
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
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; } }
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
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; } }
En base de donnees mes entity sont representés de la sorte:
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; }
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?
Partager