[Débutante] Valeur par énumération
Bonjour,
Je débute complètement dans les mappings Hibernate et j'ai une question qui me vient à l'esprit.
Imaginons que j'ai un utilisateur représenté par un identifiant et un nom.
Et j'ai des profils, ceux-ci sont en nombre limité (par exemple : Lecteur, Ecrivain et Administrateur).
Je voudrais donc mettre les profils dans une énumération Java telle que celle-ci :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public enum Profil {
LECTEUR("Lecteur"),
ECRIVAIN("Ecrivain"),
ADMIN("Administrateur");
private String value;
private Profil(String value) {
this.value = value;
}
public String getValue() {
return this.value;
}
} |
Un utilisateur peut avoir plusieurs profils. J'aimerais donc un objet Utilisateur comme ceci :
Code:
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
| import java.util.Set;
public class Utilisateur {
private Long id;
private String nom;
private Set<Profil> profils;
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getNom() {
return this.nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public Set<Profil> getProfils() {
return this.profils;
}
public void setProfils(Set<Profil> profils) {
this.profils = profils;
}
} |
Comment puis-je mapper l'utilisateur pour avoir dans ma base le modèle suivant ?
Table UTILISATEUR
Code:
1 2 3 4 5
| id nom
--------------
1 Toto
2 Titi
3 Tata |
Table UTILISATEUR_PROFIL
Code:
1 2 3 4 5 6
| id_utilisateur profil
--------------
1 LECTEUR
1 ECRIVAIN
2 ADMIN
3 LECTEUR |