J'essaye d'utiliser hibernate avec le mécanisme d'annotation.
Voici le code de ma classe
Lorsque je créé une instance de l'objet compteUtilisateur, le champ id est nul.
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package fr.aef.util; import java.io.Serializable; import java.util.GregorianCalendar; import javax.persistence.Column; import javax.persistence.Embedded; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.Table; /** * * @author ratatouille */ @Entity @Table(name="UTILISATEURS") @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS) public class CompteUtilisateur implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "ID") private Long id; @Column(name="NOM") private String nom; @Column(name="PRENOM") private String prenom; @Column(name="EMAIL") private String email; /* @Column(name="DATE_DE_NAISSANCE") private GregorianCalendar dateDeNaissance; /* @Embedded private Adresse adresse; @Embedded private Telephone telephone; */ public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } /* public void setAdresse(Adresse adresse) { this.adresse = adresse; } */ /* public void setDateDeNaissance(GregorianCalendar dateDeNaissance) { this.dateDeNaissance = dateDeNaissance; } */ public void setNom(String nom) { if( nom == null ) throw new IllegalArgumentException(); this.nom = nom; } public void setPrenom(String prenom) { if( prenom == null) throw new IllegalArgumentException(); this.prenom = prenom; } /* public void setTelephone(Telephone telephone) { this.telephone = telephone; } public Adresse getAdresse() { return adresse; } */ /* public GregorianCalendar getDateDeNaissance() { return dateDeNaissance; } */ public String getNom() { return nom; } public String getPrenom() { return prenom; } /* public Telephone getTelephone() { return telephone; } */ public Long getId() { return id; } public void setId(Long id) { this.id = id; } public CompteUtilisateur(String nom, String prenom) { if( nom == null || prenom == null ) throw new IllegalArgumentException(); this.nom = nom; this.prenom = prenom; } public CompteUtilisateur() { } }
lorsque je fais
J'ai l'erreur suivante :
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 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package fr.aef.services; import fr.aef.util.CompteUtilisateur; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.Persistence; /** * * @author ratatouille */ public class CompteManager { public CompteManager() { } public void creerUnCompte( CompteUtilisateur compte ) { if( compte == null ) throw new IllegalArgumentException(); EntityManagerFactory emf; EntityManager em; EntityTransaction tx; emf = Persistence.createEntityManagerFactory("AnnuaireEntrepreneursPU"); em = emf.createEntityManager(); tx = em.getTransaction(); try { tx.begin(); em.persist(compte); tx.commit(); } catch(Exception e ) { System.out.println( e.toString() ); } finally { if (tx.isActive()) { tx.rollback(); } em.close(); } } }
Si vous avez un lien pour l'utilisation de Hibernate avec des annotations, je suis preneur .
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 exporting generated schema to database schema export complete SQL Error: -1, SQLState: 23502 La colonne 'ID' ne peut pas accepter de valeur NULL. javax.persistence.EntityExistsException: org.hibernate.exception.ConstraintViolationException: could not insert: [fr.aef.util.CompteUtilisateur]
Merci
Partager