bonjour,
supposons que j'ai une classe abstraite comme ceci :
et une autre classe concrète qui l'étends :
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 package Metier.Promotion.EtatsSemestre; import DAO.DB.IDataBase; import Metier.Enseignant.Enseignant; import Metier.Etudes.Module; import Metier.Etudiant.Etudiant; import Metier.Promotion.Semestre; import java.util.List; import javax.persistence.*; /** * * @author bassim */ @Entity @Table(name="Etat_Semestre") abstract public class AbstractEtatSemestre implements IDataBase { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @OneToOne(mappedBy = "etat") protected Semestre semestre; abstract public void mettreEnAttente() ; abstract public void commencer() throws Exception; abstract public void terminer() throws Exception; abstract public void ajouterEtudiants(List<Etudiant> etudiants) throws Exception; abstract public void ajouterEtudiant(Etudiant etudiant) throws Exception; abstract public void enleverEtudiant(Etudiant etudiant) throws Exception; abstract public void affecterEnseignant(Module module,Enseignant ensei) throws Exception; public Long getId() { return id; } public void setId(Long id) { this.id = id; } }
normalement si on persiste une instance de la classe EtatEnAttente, y'aura qu'une seule table crée dans la BDD correspondant à AbstractEtatSemestre
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 package Metier.Promotion.EtatsSemestre; import Metier.Enseignant.Enseignant; import Metier.Etudes.Module; import Metier.Etudiant.Etudiant; import Metier.Promotion.Semestre; import java.util.List; /** * * @author bassim */ public class EtatEnAttente extends AbstractEtatSemestre { static private final long serialVersionUID = 731L; /** Creates a new instance of EtatEnAttente */ public EtatEnAttente(Semestre semestre) { this.semestre = semestre; } public void mettreEnAttente() { } public void commencer() throws Exception { if (semestre.getEtudiants().size() == 0) throw new Exception("Aucun étudiant n'est inscrit " + "à la promotion !"); /* if (semestre.getEnseignants().size() == 0) throw new Exception("Aucun enseignant n'a été affecté à la promotion !");*/ if (semestre.getCours().isEmpty()) throw new Exception("Aucun module n'existe !"); if (semestre.getCours().containsValue(null)) throw new Exception("Vous devez affecter un enseignant " + "pour chaque module !"); semestre.setEtat(new EtatEnCours(semestre)); semestre.notifier(); } public void terminer() throws Exception { throw new Exception("Le semestre n'a pas encore commencé !"); } public void ajouterEtudiants(List<Etudiant> etudiants) throws Exception { // cette instruction doit venir aprés semestre.getEtudiants().addAll(etudiants); semestre.notifier(); } public void ajouterEtudiant(Etudiant etudiant) throws Exception { if (etudiant != null) { if (semestre.getEtudiants().contains(etudiant)) throw new Exception("Cet étudiant appartient déjà à la promotion !"); if (semestre.getNumero() == 1) etudiant.ajouterPromotion(semestre.getSemestre().getPromotion()); // cette instruction doit venir aprés semestre.getEtudiants().add(etudiant); semestre.notifier(); } } public void enleverEtudiant(Etudiant etudiant) throws Exception { if (etudiant != null) if (semestre.getEtudiants().remove(etudiant)) System.out.println("trouvé"); } public void affecterEnseignant(Module module, Enseignant ensei) throws Exception { if (semestre.getCours().containsValue(ensei)) throw new Exception("Cet enseignant est dèjà " + "en charge d'un module !"); else semestre.getCours().put(module,ensei); } public void save() { } public void update() { } public void delete() { } }
maintenant, si on ajoute un attribut persistant à EtatEnAttente, la classe ne corresponds plus vraiment à la classe AbstractEtatSemestre et la table générée dans la BDD.
Comment JPA gère ce mécanisme ?









Répondre avec citation
Partager