回答編集履歴

1

追記

2019/12/17 11:32

投稿

rubytomato
rubytomato

スコア1752

test CHANGED
@@ -47,3 +47,521 @@
47
47
  1) MeisaiのGoalSeq型プロパティ名を`goalSeq`にリネームする。
48
48
 
49
49
  2) MeisaiRepositoryのメソッド名を`findByGoalseqid`にリネームする。
50
+
51
+
52
+
53
+ **追記**
54
+
55
+
56
+
57
+ 質問内容から分かる範囲で再現した、双方のエンティティクラスでOneToOneアノテーションを付与した、双方向の関連を持つコードです。
58
+
59
+ 下記の環境で動作確認できています。
60
+
61
+
62
+
63
+ * OpenJDK 11.0
64
+
65
+ * Spring Boot 2.2.1
66
+
67
+ * Hibernate Core 5.1.0
68
+
69
+ * MySQL 8.0.17
70
+
71
+
72
+
73
+ ```Java
74
+
75
+ import java.util.Date;
76
+
77
+
78
+
79
+ import javax.persistence.CascadeType;
80
+
81
+ import javax.persistence.Column;
82
+
83
+ import javax.persistence.Entity;
84
+
85
+ import javax.persistence.GeneratedValue;
86
+
87
+ import javax.persistence.GenerationType;
88
+
89
+ import javax.persistence.Id;
90
+
91
+ import javax.persistence.OneToOne;
92
+
93
+ import javax.persistence.Table;
94
+
95
+
96
+
97
+ @Entity
98
+
99
+ @Table(name = "goals_seq")
100
+
101
+ public class GoalSeq {
102
+
103
+
104
+
105
+ @Id
106
+
107
+ @GeneratedValue(strategy = GenerationType.IDENTITY)
108
+
109
+ private Long id;
110
+
111
+
112
+
113
+ @Column
114
+
115
+ private Long userId;
116
+
117
+
118
+
119
+ @Column
120
+
121
+ private Long goalId;
122
+
123
+
124
+
125
+ @Column
126
+
127
+ private String goalTitle;
128
+
129
+
130
+
131
+ @Column
132
+
133
+ private String goalDetail;
134
+
135
+
136
+
137
+ @Column
138
+
139
+ private String goalWeight;
140
+
141
+
142
+
143
+ @Column
144
+
145
+ private Date updatedAt;
146
+
147
+
148
+
149
+ @OneToOne(mappedBy = "goalSeq", cascade = CascadeType.ALL)
150
+
151
+ private Meisai meisai;
152
+
153
+
154
+
155
+ public Long getId() {
156
+
157
+ return id;
158
+
159
+ }
160
+
161
+
162
+
163
+ public void setId(Long id) {
164
+
165
+ this.id = id;
166
+
167
+ }
168
+
169
+
170
+
171
+ public Long getUserId() {
172
+
173
+ return userId;
174
+
175
+ }
176
+
177
+
178
+
179
+ public void setUserId(Long userId) {
180
+
181
+ this.userId = userId;
182
+
183
+ }
184
+
185
+
186
+
187
+ public Long getGoalId() {
188
+
189
+ return goalId;
190
+
191
+ }
192
+
193
+
194
+
195
+ public void setGoalId(Long goalId) {
196
+
197
+ this.goalId = goalId;
198
+
199
+ }
200
+
201
+
202
+
203
+ public String getGoalTitle() {
204
+
205
+ return goalTitle;
206
+
207
+ }
208
+
209
+
210
+
211
+ public void setGoalTitle(String goalTitle) {
212
+
213
+ this.goalTitle = goalTitle;
214
+
215
+ }
216
+
217
+
218
+
219
+ public String getGoalDetail() {
220
+
221
+ return goalDetail;
222
+
223
+ }
224
+
225
+
226
+
227
+ public void setGoalDetail(String goalDetail) {
228
+
229
+ this.goalDetail = goalDetail;
230
+
231
+ }
232
+
233
+
234
+
235
+ public String getGoalWeight() {
236
+
237
+ return goalWeight;
238
+
239
+ }
240
+
241
+
242
+
243
+ public void setGoalWeight(String goalWeight) {
244
+
245
+ this.goalWeight = goalWeight;
246
+
247
+ }
248
+
249
+
250
+
251
+ public Date getUpdatedAt() {
252
+
253
+ return updatedAt;
254
+
255
+ }
256
+
257
+
258
+
259
+ public void setUpdatedAt(Date updatedAt) {
260
+
261
+ this.updatedAt = updatedAt;
262
+
263
+ }
264
+
265
+
266
+
267
+ public Meisai getMeisai() {
268
+
269
+ return meisai;
270
+
271
+ }
272
+
273
+
274
+
275
+ public void setMeisai(Meisai meisai) {
276
+
277
+ this.meisai = meisai;
278
+
279
+ }
280
+
281
+
282
+
283
+ @Override
284
+
285
+ public String toString() {
286
+
287
+ return "GoalSeq [id=" + id + ", userId=" + userId + ", goalId=" + goalId + ", goalTitle=" + goalTitle
288
+
289
+ + ", goalDetail=" + goalDetail + ", goalWeight=" + goalWeight + ", updatedAt=" + updatedAt + "]";
290
+
291
+ }
292
+
293
+
294
+
295
+ }
296
+
297
+ ```
298
+
299
+
300
+
301
+ ```java
302
+
303
+ import javax.persistence.Column;
304
+
305
+ import javax.persistence.Entity;
306
+
307
+ import javax.persistence.GeneratedValue;
308
+
309
+ import javax.persistence.GenerationType;
310
+
311
+ import javax.persistence.Id;
312
+
313
+ import javax.persistence.JoinColumn;
314
+
315
+ import javax.persistence.OneToOne;
316
+
317
+ import javax.persistence.Table;
318
+
319
+
320
+
321
+ @Entity
322
+
323
+ @Table(name = "meisai_tbl")
324
+
325
+ public class Meisai {
326
+
327
+
328
+
329
+ @Id
330
+
331
+ @Column(name = "meisai_id")
332
+
333
+ @GeneratedValue(strategy = GenerationType.IDENTITY)
334
+
335
+ private Long meisaiId;
336
+
337
+
338
+
339
+ @Column
340
+
341
+ private String selfMidCmt;
342
+
343
+
344
+
345
+ @Column
346
+
347
+ private String bossMidCmt;
348
+
349
+
350
+
351
+ @Column
352
+
353
+ private String selfEndCmt;
354
+
355
+
356
+
357
+ @Column
358
+
359
+ private String bossEndCmt;
360
+
361
+
362
+
363
+ @OneToOne
364
+
365
+ @JoinColumn(name = "goalseq_id")
366
+
367
+ private GoalSeq goalSeq;
368
+
369
+
370
+
371
+ public Long getMeisaiId() {
372
+
373
+ return meisaiId;
374
+
375
+ }
376
+
377
+
378
+
379
+ public void setMeisaiId(Long meisaiId) {
380
+
381
+ this.meisaiId = meisaiId;
382
+
383
+ }
384
+
385
+
386
+
387
+ public String getSelfMidCmt() {
388
+
389
+ return selfMidCmt;
390
+
391
+ }
392
+
393
+
394
+
395
+ public void setSelfMidCmt(String selfMidCmt) {
396
+
397
+ this.selfMidCmt = selfMidCmt;
398
+
399
+ }
400
+
401
+
402
+
403
+ public String getBossMidCmt() {
404
+
405
+ return bossMidCmt;
406
+
407
+ }
408
+
409
+
410
+
411
+ public void setBossMidCmt(String bossMidCmt) {
412
+
413
+ this.bossMidCmt = bossMidCmt;
414
+
415
+ }
416
+
417
+
418
+
419
+ public String getSelfEndCmt() {
420
+
421
+ return selfEndCmt;
422
+
423
+ }
424
+
425
+
426
+
427
+ public void setSelfEndCmt(String selfEndCmt) {
428
+
429
+ this.selfEndCmt = selfEndCmt;
430
+
431
+ }
432
+
433
+
434
+
435
+ public String getBossEndCmt() {
436
+
437
+ return bossEndCmt;
438
+
439
+ }
440
+
441
+
442
+
443
+ public void setBossEndCmt(String bossEndCmt) {
444
+
445
+ this.bossEndCmt = bossEndCmt;
446
+
447
+ }
448
+
449
+
450
+
451
+ public GoalSeq getGoalSeq() {
452
+
453
+ return goalSeq;
454
+
455
+ }
456
+
457
+
458
+
459
+ public void setGoalSeq(GoalSeq goalSeq) {
460
+
461
+ this.goalSeq = goalSeq;
462
+
463
+ }
464
+
465
+
466
+
467
+ @Override
468
+
469
+ public String toString() {
470
+
471
+ return "Meisai [meisaiId=" + meisaiId + ", selfMidCmt=" + selfMidCmt + ", bossMidCmt=" + bossMidCmt
472
+
473
+ + ", selfEndCmt=" + selfEndCmt + ", bossEndCmt=" + bossEndCmt + "]";
474
+
475
+ }
476
+
477
+
478
+
479
+ }
480
+
481
+ ```
482
+
483
+
484
+
485
+ リポジトリ
486
+
487
+
488
+
489
+ ```Java
490
+
491
+ import org.springframework.data.jpa.repository.JpaRepository;
492
+
493
+
494
+
495
+ import com.example.demo.entity.GoalSeq;
496
+
497
+ import com.example.demo.entity.Meisai;
498
+
499
+
500
+
501
+ public interface MeisaiRepository extends JpaRepository<Meisai, Long> {
502
+
503
+ public Meisai findByGoalSeq(GoalSeq goalseq);
504
+
505
+ }
506
+
507
+ ```
508
+
509
+
510
+
511
+ DDL
512
+
513
+
514
+
515
+ ```SQL
516
+
517
+ CREATE TABLE goals_seq (
518
+
519
+ id BIGINT AUTO_INCREMENT,
520
+
521
+ user_id BIGINT NOT NULL DEFAULT 0,
522
+
523
+ goal_id BIGINT NOT NULL DEFAULT 0,
524
+
525
+ goal_title VARCHAR(255) NULL,
526
+
527
+ goal_detail VARCHAR(255) NULL,
528
+
529
+ goal_weight VARCHAR(10) NULL,
530
+
531
+ updated_at DATE NOT NULL,
532
+
533
+ PRIMARY KEY (id)
534
+
535
+ )
536
+
537
+ ENGINE = INNODB
538
+
539
+ DEFAULT CHARSET = UTF8MB4;
540
+
541
+
542
+
543
+
544
+
545
+ CREATE TABLE meisai_tbl (
546
+
547
+ meisai_id BIGINT AUTO_INCREMENT,
548
+
549
+ goalseq_id BIGINT NOT NULL DEFAULT 0,
550
+
551
+ self_mid_cmt VARCHAR(255) NULL,
552
+
553
+ boss_mid_cmt VARCHAR(255) NULL,
554
+
555
+ self_end_cmt VARCHAR(255) NULL,
556
+
557
+ boss_end_cmt VARCHAR(255) NULL,
558
+
559
+ PRIMARY KEY (meisai_id)
560
+
561
+ )
562
+
563
+ ENGINE = INNODB
564
+
565
+ DEFAULT CHARSET = UTF8MB4;
566
+
567
+ ```