Bonjour,
Dans le cas d'une refonte d'une application web, je repars uniquement de la base de données (Oracle).
Comment exploiter les classes générées par Hibernate Tools ?
Pour une table Profil par exemple :
Le plugin hibernate Tools me génére deux classes : une pour la primary key et une autre pour les colonnes
Classe Profil.java : qui ne mappe pas correctement les colonnes de ma table.
Une classe ProfilId:
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 package com.xxx.hibernateDB.database.entities; // Generated 27 janv. 2015 16:25:25 by Hibernate Tools 4.0.0 import javax.persistence.AttributeOverride; import javax.persistence.AttributeOverrides; import javax.persistence.Column; import javax.persistence.EmbeddedId; import javax.persistence.Entity; import javax.persistence.Table; /** * Profil generated by hbm2java */ @Entity @Table(name = "PROFIL", schema = "USR_ROUTAGE") public class Profil implements java.io.Serializable { private ProfilId id; public Profil() { } public Profil(ProfilId id) { this.id = id; } @EmbeddedId @AttributeOverrides({ @AttributeOverride(name = "idProfil", column = @Column(name = "ID_PROFIL", nullable = false, precision = 22, scale = 0)), @AttributeOverride(name = "idPointEntree", column = @Column(name = "ID_POINT_ENTREE", length = 32)), @AttributeOverride(name = "proIdProfil", column = @Column(name = "PRO_ID_PROFIL", precision = 22, scale = 0)), @AttributeOverride(name = "idSegment", column = @Column(name = "ID_SEGMENT", length = 100)), @AttributeOverride(name = "idCible", column = @Column(name = "ID_CIBLE", precision = 22, scale = 0)), @AttributeOverride(name = "poiIdPointEntree", column = @Column(name = "POI_ID_POINT_ENTREE", length = 32)), @AttributeOverride(name = "nomProfil", column = @Column(name = "NOM_PROFIL", nullable = false)), @AttributeOverride(name = "typeProfil", column = @Column(name = "TYPE_PROFIL", nullable = false, length = 100)), @AttributeOverride(name = "descriptionProfil", column = @Column(name = "DESCRIPTION_PROFIL", length = 10)) }) public ProfilId getId() { return this.id; } public void setId(ProfilId id) { this.id = id; } }
Ma question est comment faire une rêquete pour avoir une liste de tous les profils ?
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213 package com.xxx.hibernateDB.database.entities; // Generated 27 janv. 2015 16:25:25 by Hibernate Tools 4.0.0 import java.math.BigDecimal; import javax.persistence.Column; import javax.persistence.Embeddable; /** * ProfilId generated by hbm2java */ @Embeddable public class ProfilId implements java.io.Serializable { private BigDecimal idProfil; private String idPointEntree; private BigDecimal proIdProfil; private String idSegment; private BigDecimal idCible; private String poiIdPointEntree; private String nomProfil; private String typeProfil; private String descriptionProfil; public ProfilId() { } public ProfilId(BigDecimal idProfil, String nomProfil, String typeProfil) { this.idProfil = idProfil; this.nomProfil = nomProfil; this.typeProfil = typeProfil; } public ProfilId(BigDecimal idProfil, String idPointEntree, BigDecimal proIdProfil, String idSegment, BigDecimal idCible, String poiIdPointEntree, String nomProfil, String typeProfil, String descriptionProfil) { this.idProfil = idProfil; this.idPointEntree = idPointEntree; this.proIdProfil = proIdProfil; this.idSegment = idSegment; this.idCible = idCible; this.poiIdPointEntree = poiIdPointEntree; this.nomProfil = nomProfil; this.typeProfil = typeProfil; this.descriptionProfil = descriptionProfil; } @Column(name = "ID_PROFIL", nullable = false, precision = 22, scale = 0) public BigDecimal getIdProfil() { return this.idProfil; } public void setIdProfil(BigDecimal idProfil) { this.idProfil = idProfil; } @Column(name = "ID_POINT_ENTREE", length = 32) public String getIdPointEntree() { return this.idPointEntree; } public void setIdPointEntree(String idPointEntree) { this.idPointEntree = idPointEntree; } @Column(name = "PRO_ID_PROFIL", precision = 22, scale = 0) public BigDecimal getProIdProfil() { return this.proIdProfil; } public void setProIdProfil(BigDecimal proIdProfil) { this.proIdProfil = proIdProfil; } @Column(name = "ID_SEGMENT", length = 100) public String getIdSegment() { return this.idSegment; } public void setIdSegment(String idSegment) { this.idSegment = idSegment; } @Column(name = "ID_CIBLE", precision = 22, scale = 0) public BigDecimal getIdCible() { return this.idCible; } public void setIdCible(BigDecimal idCible) { this.idCible = idCible; } @Column(name = "POI_ID_POINT_ENTREE", length = 32) public String getPoiIdPointEntree() { return this.poiIdPointEntree; } public void setPoiIdPointEntree(String poiIdPointEntree) { this.poiIdPointEntree = poiIdPointEntree; } @Column(name = "NOM_PROFIL", nullable = false) public String getNomProfil() { return this.nomProfil; } public void setNomProfil(String nomProfil) { this.nomProfil = nomProfil; } @Column(name = "TYPE_PROFIL", nullable = false, length = 100) public String getTypeProfil() { return this.typeProfil; } public void setTypeProfil(String typeProfil) { this.typeProfil = typeProfil; } @Column(name = "DESCRIPTION_PROFIL", length = 10) public String getDescriptionProfil() { return this.descriptionProfil; } public void setDescriptionProfil(String descriptionProfil) { this.descriptionProfil = descriptionProfil; } public boolean equals(Object other) { if ((this == other)) return true; if ((other == null)) return false; if (!(other instanceof ProfilId)) return false; ProfilId castOther = (ProfilId) other; return ((this.getIdProfil() == castOther.getIdProfil()) || (this .getIdProfil() != null && castOther.getIdProfil() != null && this .getIdProfil().equals(castOther.getIdProfil()))) && ((this.getIdPointEntree() == castOther.getIdPointEntree()) || (this .getIdPointEntree() != null && castOther.getIdPointEntree() != null && this .getIdPointEntree() .equals(castOther.getIdPointEntree()))) && ((this.getProIdProfil() == castOther.getProIdProfil()) || (this .getProIdProfil() != null && castOther.getProIdProfil() != null && this .getProIdProfil().equals(castOther.getProIdProfil()))) && ((this.getIdSegment() == castOther.getIdSegment()) || (this .getIdSegment() != null && castOther.getIdSegment() != null && this .getIdSegment().equals(castOther.getIdSegment()))) && ((this.getIdCible() == castOther.getIdCible()) || (this .getIdCible() != null && castOther.getIdCible() != null && this .getIdCible().equals(castOther.getIdCible()))) && ((this.getPoiIdPointEntree() == castOther .getPoiIdPointEntree()) || (this.getPoiIdPointEntree() != null && castOther.getPoiIdPointEntree() != null && this .getPoiIdPointEntree().equals( castOther.getPoiIdPointEntree()))) && ((this.getNomProfil() == castOther.getNomProfil()) || (this .getNomProfil() != null && castOther.getNomProfil() != null && this .getNomProfil().equals(castOther.getNomProfil()))) && ((this.getTypeProfil() == castOther.getTypeProfil()) || (this .getTypeProfil() != null && castOther.getTypeProfil() != null && this .getTypeProfil().equals(castOther.getTypeProfil()))) && ((this.getDescriptionProfil() == castOther .getDescriptionProfil()) || (this .getDescriptionProfil() != null && castOther.getDescriptionProfil() != null && this .getDescriptionProfil().equals( castOther.getDescriptionProfil()))); } public int hashCode() { int result = 17; result = 37 * result + (getIdProfil() == null ? 0 : this.getIdProfil().hashCode()); result = 37 * result + (getIdPointEntree() == null ? 0 : this.getIdPointEntree() .hashCode()); result = 37 * result + (getProIdProfil() == null ? 0 : this.getProIdProfil() .hashCode()); result = 37 * result + (getIdSegment() == null ? 0 : this.getIdSegment().hashCode()); result = 37 * result + (getIdCible() == null ? 0 : this.getIdCible().hashCode()); result = 37 * result + (getPoiIdPointEntree() == null ? 0 : this .getPoiIdPointEntree().hashCode()); result = 37 * result + (getNomProfil() == null ? 0 : this.getNomProfil().hashCode()); result = 37 * result + (getTypeProfil() == null ? 0 : this.getTypeProfil() .hashCode()); result = 37 * result + (getDescriptionProfil() == null ? 0 : this .getDescriptionProfil().hashCode()); return result; } }
Voici ma fonction qui récupère tous les profils :
Je ne peux pas utiliser le nom Profil dans ma rêquete puisqu'il ne correspond pas a la configuration de ma table .
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 package com.xxx.hibernateDB.database.dao.impl; import java.util.Collection; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.dao.DataAccessException; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.xxx.hibernateDB.database.dao.ProfilDao; import com.xxx.hibernateDB.database.entities.PointEntree; import com.xxx.hibernateDB.database.entities.Profil; /** * ProfilPE DAO * @author * */ @Repository("profilDaoImpl") @Transactional(propagation = Propagation.REQUIRED) public class ProfilDaoImpl implements ProfilDao { private static final Logger LOGGER = LoggerFactory.getLogger(ProfilDaoImpl.class); private EntityManager entityManager; private static final String SELECT_QUERY = "select ID_PROFIL from Profil p"; @PersistenceContext public void setEntityManager(EntityManager entityManager) { this.entityManager = entityManager; } public EntityManager getEntityManager() { return entityManager; } /** * Insert Profil * @param transientInstance */ public void persist(Profil transientInstance) { LOGGER.debug("persisting Profil instance"); try { entityManager.persist(transientInstance); LOGGER.debug("persist successful"); } catch (RuntimeException re) { LOGGER.error("persist failed", re); throw re; } } public Collection<Profil> selectAll() { Query query = entityManager.createQuery(SELECT_QUERY); Collection<Profil> persons = (Collection<Profil>) query.getResultList(); return persons; } public Collection<Profil> getListeProfilPE() { return null; } }
Partager