Bonjour,
Je debute un peu en hibernate 3 et je suis actuellement face un petit probleme. Je constate que lorsque je sauve un objet contenant un attribut de type java.util.Date ds ma db mysql. Je recois apres avoir rechargé l'objet une date dont l'implementation est différente java.sql.Timestamp (jusque la pas de problème) mais qui n'a pas la mm valeur ! Il y a une perte de precision ...
2009-02-27 23:19:20 DEBUG [ProduitDAOTest] saveProduit.getDate() : (class java.sql.Timestamp)1235773160000
2009-02-27 23:19:20 DEBUG [ProduitDAOTest] defaultTestProduit.getDate() : (class java.util.Date)1235773160812
et du coup mon test unitaire ne passe pas ...
voici la partie du hbm concernée:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4 <property name="date" type="timestamp"> <column length="19" name="date" not-null="true"/> </property>
et voici ma table mysl :
et voici ma classe java:
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 CREATE TABLE IF NOT EXISTS `produit` ( `reference` varchar(32) NOT NULL DEFAULT '', `nom` varchar(255) NOT NULL DEFAULT '', `id_designer` int(11) DEFAULT NULL, `nouveaute` tinyint(1) NOT NULL, `lieuCreation` varchar(255) DEFAULT NULL, `description` text, `mesure` text, `visible` tinyint(1) NOT NULL, `id` int(11) NOT NULL AUTO_INCREMENT, `date` datetime NOT NULL, `type` enum('lighting','tables','casePieces','seating','accessories','miscellaneous') DEFAULT NULL, `photo_principale_id` varchar(32) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `reference` (`reference`), UNIQUE KEY `photo_principale_id` (`photo_principale_id`), KEY `id_designer` (`id_designer`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=4916 ;
J'ai un peu essayé les différents type de date ds mysql sans succés ... Quelqu'un aurait une idée pour moi ?
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 package be.vincentcolet.domaine; // Generated 07-f�vr.-2009 0:00:37 by Hibernate Tools 3.2.1.GA import be.vincentcolet.domaine.type.TypeProduit; import java.util.Date; import java.util.HashSet; import java.util.Set; /** * Produit generated by hbm2java */ public class Produit implements java.io.Serializable { private Integer id; private Photo photo; private Designer designer; private String reference; private String nom; private boolean nouveaute; private String lieuCreation; private String description; private String mesure; private boolean visible; private Date date; private String type; private Set<Photo> photos = new HashSet<Photo>(0); public Produit() { } @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Produit other = (Produit) obj; if ((this.reference == null) ? (other.reference != null) : !this.reference.equals(other.reference)) { return false; } return true; } @Override public int hashCode() { int hash = 5; hash = 47 * hash + (this.reference != null ? this.reference.hashCode() : 0); return hash; } public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public Photo getPhoto() { return this.photo; } public void setPhoto(Photo photo) { this.photo = photo; } public Designer getDesigner() { return this.designer; } public void setDesigner(Designer designer) { this.designer = designer; } public String getReference() { return this.reference; } public void setReference(String reference) { this.reference = reference; } public String getNom() { return this.nom; } public void setNom(String nom) { this.nom = nom; } public boolean getNouveaute() { return this.nouveaute; } public void setNouveaute(boolean nouveaute) { this.nouveaute = nouveaute; } public String getLieuCreation() { return this.lieuCreation; } public void setLieuCreation(String lieuCreation) { this.lieuCreation = lieuCreation; } public String getDescription() { return this.description; } public void setDescription(String description) { this.description = description; } public String getMesure() { return this.mesure; } public void setMesure(String mesure) { this.mesure = mesure; } public boolean getVisible() { return this.visible; } public void setVisible(boolean visible) { this.visible = visible; } public Date getDate() { return this.date; } public void setDate(Date date) { this.date = date; } @Deprecated public String getType() { return this.type; } public TypeProduit getTypeProduit(){ return TypeProduit.valueOfEnum(type); } @Deprecated public void setType(String type) { this.type = type; } public void setTypeProduit(TypeProduit typeProduit){ this.type=typeProduit.getCodeTable(); } public Set<Photo> getPhotos() { return this.photos; } public void setPhotos(Set<Photo> photos) { this.photos = photos; } }
J'ai vraiment besoin de recupérer quelque chose d'identique car pour certain objet de mon domaine je devrai utiliser la date de creation de l'objet ds equals() et hashcode() car j'ai pas de "clé métier" et hibernate deconseille d'utiliser les id db (probleme avec les set ...).
Partager