Bonjour,
j'ai fais pour tester JPA (implémentation EclipseLink) un mapping n-n avec une table d'association (avec clé composite + champ supplémentaire).
Voici le mapping :
table abonnes :
id
nom
prenom
table livres:
id
libelle
table emprunts:
abonne_id
livre_id
test
EclipseLink m'a généré les entités suivantes :
Entité Abonne
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.*; 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; 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; } }
Entité Livre
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 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; 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; } }
Entité Emprunt
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; } }
EmbeddedId
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 d'insérer des valeurs dans ces 3 tables en procédant comme suit :
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 Abonne abonne = new Abonne(); Livre livre = new Livre(); Emprunt emprunt = new Emprunt(); EmpruntPK empruntPK = new EmpruntPK(); abonne.setNom("Gates"); abonne.setPrenom("Billouz"); livre.setLibelle("l'info pour les nuls"); em.persist(abonne); em.persist(livre); em.flush(); empruntPK.setAbonneId(abonne.getId()); empruntPK.setLivreId(livre.getId()); emprunt.setId(empruntPK); emprunt.setTest("un test"); em.persist(emprunt); em.flush();
J'obtiens une exception MySQLSyntaxErrorException :
Dans les logs, j'ai également ceci :Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Column 'abonne_id' specified twice
Effectivement, abonne_id et livre_id apparaissent 2 fois dans l'insert généré. J'ai essayé beaucoup de méthodes d'insertions sans pour autant que l'insert généré change (seulement le binding change). Je ne sais pas si c'est ma méthode d'insertion qui est mauvaise ou bien si c'est mes entités qui sont mal construites...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: Column 'abonne_id' specified twice
Error Code: 1110
Call: INSERT INTO emprunts (TEST, abonne_id, livre_id, LIVRE_ID, ABONNE_ID) VALUES (?, ?, ?, ?, ?)
bind => [un test, 1251, 1252, null, null]
Voyez vous l'erreur ?
Merci![]()
Partager