Bonjour à tous,
malgré mes recherches, je dois me résigner à vous appeler au secours.
J'ai une base de donnée simple avec une table produit (id_prodit,name_produit) en relation many-to-many avec une table image (id_image, name_image).
Lorsque j'utilise Hibernate tools sous éclipe, je prend soin de cocher la case "Detect many-to-many tables mais il me génère 4 beans.
Ais-je fais une erreur quelque part? Dois-je (et alors, comment puis-je) configurer hibernate pour le forcer à détecter la relation ? Merci de votre aide !
BDD :
Table image :
- id_image (auto_increment, primaire)
- name_image
Table produit :
- id_produit (auto_increment, primaire)
- name_produit
Table produit_image :
- id_produit (clé étrangère, relation à produit.id_produit)
- id_image (clé étrangère, relation à image.id_image)
Beans générés :
Image :
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 public class Image implements java.io.Serializable { private Integer idImage; private String nameImage; private Set<ImageProduit> imageProduits = new HashSet<ImageProduit>(0); public Image() { } public Image(String nameImage) { this.nameImage = nameImage; } public Image(String nameImage, Set<ImageProduit> imageProduits) { this.nameImage = nameImage; this.imageProduits = imageProduits; } @Id @GeneratedValue(strategy = IDENTITY) @Column(name = "id_image", unique = true, nullable = false) public Integer getIdImage() { return this.idImage; } public void setIdImage(Integer idImage) { this.idImage = idImage; } @Column(name = "name_image", nullable = false, length = 25) public String getNameImage() { return this.nameImage; } public void setNameImage(String nameImage) { this.nameImage = nameImage; } @OneToMany(fetch = FetchType.LAZY, mappedBy = "image") public Set<ImageProduit> getImageProduits() { return this.imageProduits; } public void setImageProduits(Set<ImageProduit> imageProduits) { this.imageProduits = imageProduits; } }ImageProduit :
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 @Entity @Table(name = "produit", catalog = "test1") public class Produit implements java.io.Serializable { private Integer idProduit; private String nameProduit; private Set<ImageProduit> imageProduits = new HashSet<ImageProduit>(0); public Produit() { } public Produit(String nameProduit) { this.nameProduit = nameProduit; } public Produit(String nameProduit, Set<ImageProduit> imageProduits) { this.nameProduit = nameProduit; this.imageProduits = imageProduits; } @Id @GeneratedValue(strategy = IDENTITY) @Column(name = "id_produit", unique = true, nullable = false) public Integer getIdProduit() { return this.idProduit; } public void setIdProduit(Integer idProduit) { this.idProduit = idProduit; } @Column(name = "name_produit", nullable = false, length = 25) public String getNameProduit() { return this.nameProduit; } public void setNameProduit(String nameProduit) { this.nameProduit = nameProduit; } @OneToMany(fetch = FetchType.LAZY, mappedBy = "produit") public Set<ImageProduit> getImageProduits() { return this.imageProduits; } public void setImageProduits(Set<ImageProduit> imageProduits) { this.imageProduits = imageProduits; } }
ImageProduitID :
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 @Entity @Table(name = "image_produit", catalog = "test1") public class ImageProduit implements java.io.Serializable { private ImageProduitId id; private Image image; private Produit produit; public ImageProduit() { } public ImageProduit(ImageProduitId id, Image image, Produit produit) { this.id = id; this.image = image; this.produit = produit; } @EmbeddedId @AttributeOverrides({ @AttributeOverride(name = "idProduit", column = @Column(name = "id_produit", nullable = false)), @AttributeOverride(name = "idImage", column = @Column(name = "id_image", nullable = false)) }) public ImageProduitId getId() { return this.id; } public void setId(ImageProduitId id) { this.id = id; } @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "id_image", nullable = false, insertable = false, updatable = false) public Image getImage() { return this.image; } public void setImage(Image image) { this.image = image; } @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "id_produit", nullable = false, insertable = false, updatable = false) public Produit getProduit() { return this.produit; } public void setProduit(Produit produit) { this.produit = produit; } }
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 @Embeddable public class ImageProduitId implements java.io.Serializable { private int idProduit; private int idImage; public ImageProduitId() { } public ImageProduitId(int idProduit, int idImage) { this.idProduit = idProduit; this.idImage = idImage; } @Column(name = "id_produit", nullable = false) public int getIdProduit() { return this.idProduit; } public void setIdProduit(int idProduit) { this.idProduit = idProduit; } @Column(name = "id_image", nullable = false) public int getIdImage() { return this.idImage; } public void setIdImage(int idImage) { this.idImage = idImage; } public boolean equals(Object other) { if ((this == other)) return true; if ((other == null)) return false; if (!(other instanceof ImageProduitId)) return false; ImageProduitId castOther = (ImageProduitId) other; return (this.getIdProduit() == castOther.getIdProduit()) && (this.getIdImage() == castOther.getIdImage()); } public int hashCode() { int result = 17; result = 37 * result + this.getIdProduit(); result = 37 * result + this.getIdImage(); return result; } }
Partager