Bonjour,
j'ai 3 tables dans ma bd

groupe et genre et groupegenre

groupegenre regroupe les id de groupe et genre.

dans ma classe Groupe :


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
 
@Entity
public class Groupe implements Serializable {
	@Id
	@GeneratedValue(strategy = GenerationType.SEQUENCE)
	private int idgroupe;
 
	private String nom;
 
	private String photo;
 
	private String email;
 
	@Lob
	private String valide;
 
	@Lob
	private String informations;
 
	private static final long serialVersionUID = 1L;
 
	public Groupe() {
		super();
		groupeGenre = new ArrayList<Genre>();
	}
 
	public int getIdgroupe() {
		return this.idgroupe;
	}
 
	public void setIdgroupe(int idgroupe) {
		this.idgroupe = idgroupe;
	}
 
	public String getNom() {
		return this.nom;
	}
 
	public void setNom(String nom) {
		this.nom = nom;
	}
 
	public String getPhoto() {
		return this.photo;
	}
 
	public void setPhoto(String photo) {
		this.photo = photo;
	}
 
	public String getEmail() {
		return this.email;
	}
 
	public void setEmail(String email) {
		this.email = email;
	}
 
	public String getValide() {
		return this.valide;
	}
 
	public void setValide(String valide) {
		this.valide = valide;
	}
 
	public String getInformations() {
		return this.informations;
	}
 
	public void setInformations(String informations) {
		this.informations = informations;
	}
	@OneToMany(cascade = CascadeType.ALL,
			  fetch = FetchType.EAGER)
 
	  @JoinTable (name="groupegenre",
	      joinColumns={@JoinColumn(name="idgroupe")},
	      inverseJoinColumns=@JoinColumn (name="idgenre"))
	private List<Genre> groupeGenre; 
 
	public List<Genre> getGenre(){
		return this.groupeGenre;
	}
 
	public void setGenre(List<Genre> listGenre){
		this.groupeGenre = listGenre;
	}
 
}
ma classe genre :

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
@Entity
public class Genre implements Serializable {
	@Id
	@GeneratedValue(strategy = GenerationType.SEQUENCE)
	private int idgenre;
 
	private String nom;
 
	private static final long serialVersionUID = 1L;
 
	public Genre() {
		super();
	}
 
	public int getIdgenre() {
		return this.idgenre;
	}
 
	public void setIdgenre(int idgenre) {
		this.idgenre = idgenre;
	}
 
	public String getNom() {
		return this.nom;
	}
 
	public void setNom(String nom) {
		this.nom = nom;
	}
}

code d'insertion

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
 
public Groupe createGroupe(Groupe groupe) {
 
		em.persist(groupe);
		return groupe;
}
Voici mon problème:
J'ai un groupe avec les infos (nom,email....)
et j'ai différents type de genre(rock, jazz .....)

donc ce que je souhaite c'est lorsque je crée un groupe, ajouter dans la table groupegenre l'id du groupe et l'id du genre (j'ai mis une liste car il peut en avoir plusieurs).

l'insertion se passe bien,mais le souci c'est qu'il insère aussi dans la table genre (ce que je ne souhaite pas).

En effet, les genres sont déjà stocké dans la base. Il faut savoir qu'il seront affiché dans une liste déroulante.

Donc ce que j'aimerai faire c'est pouvoir récupérer l'id du genre et lors de la création, ajouter dans la table groupegenre l'id du groupe crée et l'id du genre choisi.

j'espère que mon explication est claire

merci