質問編集履歴

2

試してみたこと

2018/10/17 15:27

投稿

KurisOswad
KurisOswad

スコア15

test CHANGED
File without changes
test CHANGED
@@ -179,3 +179,257 @@
179
179
 
180
180
 
181
181
  ```
182
+
183
+
184
+
185
+ コンバーターと列挙型を作成してみましたがうまくいきませんでした。
186
+
187
+
188
+
189
+ ```コンバータークラス
190
+
191
+ package com.funayama.springboot;
192
+
193
+
194
+
195
+ import javax.persistence.AttributeConverter;
196
+
197
+
198
+
199
+ //フォーム用クラス
200
+
201
+ public class Converter implements AttributeConverter<JenreEnum, String> {
202
+
203
+ //データーベースに登録する値(列挙型使用)
204
+
205
+ @Override
206
+
207
+ public String convertToDatabaseColumn(JenreEnum jenreCode) {
208
+
209
+ if (jenreCode == null) {
210
+
211
+ return null;
212
+
213
+ }
214
+
215
+ switch (jenreCode) {
216
+
217
+ case Clock:
218
+
219
+ return "1";
220
+
221
+
222
+
223
+ case Electron:
224
+
225
+ return "2";
226
+
227
+
228
+
229
+ case Phone:
230
+
231
+ return "3";
232
+
233
+
234
+
235
+ default:
236
+
237
+ return null;
238
+
239
+
240
+
241
+ }
242
+
243
+ }
244
+
245
+
246
+
247
+ //Entityに登録する値(列挙型使用)
248
+
249
+ @Override
250
+
251
+ public JenreEnum convertToEntityAttribute(String dbData) {
252
+
253
+ if (dbData == null) {
254
+
255
+ return null;
256
+
257
+ }
258
+
259
+ switch (dbData) {
260
+
261
+ case "1":
262
+
263
+ return JenreEnum.Clock;
264
+
265
+ case "2":
266
+
267
+ return JenreEnum.Electron;
268
+
269
+ case "3":
270
+
271
+ return JenreEnum.Phone;
272
+
273
+ default:
274
+
275
+ return null;
276
+
277
+ }
278
+
279
+ }
280
+
281
+
282
+
283
+ }
284
+
285
+
286
+
287
+ ```
288
+
289
+
290
+
291
+ ```列挙型クラス
292
+
293
+ ackage com.funayama.springboot;
294
+
295
+
296
+
297
+ import javax.persistence.Convert;
298
+
299
+
300
+
301
+ @Convert(converter = Converter.class)
302
+
303
+ public enum JenreEnum {
304
+
305
+
306
+
307
+ Unspecified("0", "指定なし"),
308
+
309
+
310
+
311
+ Clock("1", "時計"),
312
+
313
+
314
+
315
+ Electron("2", "電子"),
316
+
317
+
318
+
319
+ Phone("3", "携帯");
320
+
321
+
322
+
323
+ //ジャンルID
324
+
325
+ private String genreId;
326
+
327
+
328
+
329
+ //ジャンル名
330
+
331
+ private String genreName;
332
+
333
+
334
+
335
+ //コンストラクタ
336
+
337
+ private JenreEnum(String genreId, String genreName) {
338
+
339
+ this.genreId = genreId;
340
+
341
+ this.genreId = genreName;
342
+
343
+ }
344
+
345
+
346
+
347
+ //商品IDゲッター
348
+
349
+ public String getGenreId() {
350
+
351
+ return genreId;
352
+
353
+ }
354
+
355
+
356
+
357
+ //ジャンル名ゲッター
358
+
359
+ public String getGenreName() {
360
+
361
+ return genreName;
362
+
363
+ }
364
+
365
+
366
+
367
+ }
368
+
369
+
370
+
371
+ ```
372
+
373
+
374
+
375
+ ```Entityクラス
376
+
377
+ @Convert(converter = Converter.class)
378
+
379
+ @Column(name = "product_genre",nullable = false,length = 2)
380
+
381
+ private String genre;
382
+
383
+ ```
384
+
385
+
386
+
387
+ EntityをEnumに設定できないとエラーが発生してしまいました。
388
+
389
+ データーベースの型がcharなのにStringでしてしまっているからなのか
390
+
391
+ HTMLからの取り出し方に問題があるのか
392
+
393
+ なにがおかしいのか見当もつきません。
394
+
395
+ どなたかお力添えをしていただけないでしょうか。。
396
+
397
+ ちなみにHTMLも載せておきます
398
+
399
+
400
+
401
+ ```HTML
402
+
403
+ <form method="post" action="/ProductList" th:action="@{/ProductList}" th:object="${formList}">
404
+
405
+ <div class='form-id'>
406
+
407
+ <!--商品ID-->
408
+
409
+ <label>※必須※ 商品ID</label> <br> <input type="text"
410
+
411
+ name="id">
412
+
413
+ </div>
414
+
415
+ <!--ジャンル-->
416
+
417
+ <div class='form-jenre'>
418
+
419
+ <label>ジャンル</label><br> <select style="width: 160px;"
420
+
421
+ name="genre">
422
+
423
+ <option value='0'></option>
424
+
425
+ <option value='1'>時計</option>
426
+
427
+ <option value='2'>電子</option>
428
+
429
+ <option value='3'>携帯</option>
430
+
431
+ </select>
432
+
433
+ </div>
434
+
435
+ ```

1

追加の情報

2018/10/17 15:27

投稿

KurisOswad
KurisOswad

スコア15

test CHANGED
File without changes
test CHANGED
@@ -17,3 +17,165 @@
17
17
  DBの型がchar型になっていて、enunではStringで作成してます。
18
18
 
19
19
  こちらは変換なども必要なのでしょうか。
20
+
21
+
22
+
23
+ 追加の情報です。
24
+
25
+ A-pzさんのアドバイスに明確に答えられないかもしれません。
26
+
27
+ 以下、私が使っているものです。
28
+
29
+
30
+
31
+ ・エクリプスのSTSでスタータースプリングブート
32
+
33
+ ・MYSQL
34
+
35
+ ・Marven
36
+
37
+ ・トムキャット
38
+
39
+ ・mysql.jdbc.Driver
40
+
41
+ ・ライブラリがよくわかっていないのですが
42
+
43
+ JREシステムライブラリというフォルダがプロジェクトにあります。
44
+
45
+
46
+
47
+
48
+
49
+ ```pom.xml
50
+
51
+ <?xml version="1.0" encoding="UTF-8"?>
52
+
53
+ <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
54
+
55
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
56
+
57
+ <modelVersion>4.0.0</modelVersion>
58
+
59
+
60
+
61
+ <groupId>com.funayama.springboot</groupId>
62
+
63
+ <artifactId>pda_k_funayama</artifactId>
64
+
65
+ <version>0.0.1-SNAPSHOT</version>
66
+
67
+ <packaging>jar</packaging>
68
+
69
+
70
+
71
+ <name>pda_k_funayama</name>
72
+
73
+ <description>sample project for Spring Boot</description>
74
+
75
+
76
+
77
+ <parent>
78
+
79
+ <groupId>org.springframework.boot</groupId>
80
+
81
+ <artifactId>spring-boot-starter-parent</artifactId>
82
+
83
+ <version>2.0.5.RELEASE</version>
84
+
85
+ <relativePath/> <!-- lookup parent from repository -->
86
+
87
+ </parent>
88
+
89
+
90
+
91
+ <properties>
92
+
93
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
94
+
95
+ <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
96
+
97
+ <java.version>1.8</java.version>
98
+
99
+ </properties>
100
+
101
+
102
+
103
+ <dependencies>
104
+
105
+ <dependency>
106
+
107
+ <groupId>org.springframework.boot</groupId>
108
+
109
+ <artifactId>spring-boot-starter-data-jpa</artifactId>
110
+
111
+ </dependency>
112
+
113
+
114
+
115
+ <dependency>
116
+
117
+ <groupId>org.springframework.boot</groupId>
118
+
119
+ <artifactId>spring-boot-starter-web</artifactId>
120
+
121
+ </dependency>
122
+
123
+
124
+
125
+ <dependency>
126
+
127
+ <groupId>mysql</groupId>
128
+
129
+ <artifactId>mysql-connector-java</artifactId>
130
+
131
+ <scope>runtime</scope>
132
+
133
+ </dependency>
134
+
135
+ <dependency>
136
+
137
+ <groupId>org.springframework.boot</groupId>
138
+
139
+ <artifactId>spring-boot-starter-test</artifactId>
140
+
141
+ <scope>test</scope>
142
+
143
+ </dependency>
144
+
145
+ <dependency>
146
+
147
+ <groupId>org.springframework.boot</groupId>
148
+
149
+ <artifactId>spring-boot-starter-thymeleaf</artifactId>
150
+
151
+ </dependency>
152
+
153
+ </dependencies>
154
+
155
+
156
+
157
+ <build>
158
+
159
+ <plugins>
160
+
161
+ <plugin>
162
+
163
+ <groupId>org.springframework.boot</groupId>
164
+
165
+ <artifactId>spring-boot-maven-plugin</artifactId>
166
+
167
+ </plugin>
168
+
169
+ </plugins>
170
+
171
+ </build>
172
+
173
+
174
+
175
+
176
+
177
+ </project>
178
+
179
+
180
+
181
+ ```