前提・実現したいこと
SpringBootの勉強中です。
1つのフォームで、2つのテーブルの内容を登録したくて、OneToOne結合しています。
主のテーブルの自動採番(mst_sensorのsensor_id、mst_mailのmail_id)はinsert文に表示されていますが、その採番されたID(sensor_id)を結合先のテーブル(mst_mail)に登録したいのですが、nullとなり登録できずに困っています。
よろしくお願いします。
発生している問題・エラーメッセージ
10. insert into mst_sensor (sensor_name, updatedata, sensor_id) values ('AA11', '05/25/2018 09:54:22.253', 36) {executed in 66 msec} 10. insert into mst_mail (sensor_id, sendmail_address, sendmail_threshold, updatedata, mail_id) values (NULL, NULL, 1, '05/25/2018 09:54:22.047', 11) {FAILED after 47 msec} org.postgresql.util.PSQLException: ERROR: null value in column "sensor_id" violates not-null constraint 詳細: Failing row contains (null, 0, 1, null, 2018-05-25 09:54:22.047, 11).
該当のソースコード
postgresql
\d mst_sensor Table "public.mst_sensor" Column | Type | Collation | Nullable | Default -------+------+-----------+----------+-------------------- sensor_id | integer | | not null | nextval('mst_sensor_sensor_id_seq'::regclass) sensor_name | character varying(30) | | not null | updatedata | timestamp without time zone | | not null | Indexes: "mst_sensor_pkey" PRIMARY KEY, btree (sensor_id) \d mst_sensor_sensor_id_seq Sequence "public.mst_sensor_sensor_id_seq" Type | Start | Minimum | Maximum | Increment | Cycles? | Cache -----+-------+---------+---------+-----------+---------+------- integer | 1 | 1 | 2147483647 | 1 | no | 1 Owned by: public.mst_sensor.sensor_id \d mst_mail Table "public.mst_mail" Column | Type | Collation | Nullable | Default -------+------+-----------+----------+----------------------- sensor_id | integer | | not null | sendmail_threshold | integer | | | sendmail_address | character varying(128) | | | updatedata | timestamp without time zone | | not null | mail_id | integer | | not null | nextval('mst_mail_mail_id_seq'::regclass) Indexes: "mst_mail_pkey" PRIMARY KEY, btree (mail_id) \d mst_mail_mail_id_seq Sequence "public.mst_mail_mail_id_seq" Type | Start | Minimum | Maximum | Increment | Cycles? | Cache -----+-------+---------+---------+-----------+---------+------- bigint | 1 | 1 | 9223372036854775807 | 1 | no | 1
Machine.java
java
1@Data 2@NoArgsConstructor 3@AllArgsConstructor 4@Entity 5@Table(name = "mst_sensor") 6public class Machine { 7 8 private Integer sensorId; 9 private String sensorName; 10 private Timestamp updatedata; 11 private MachineMailAdd machineMailAdd; 12 13 @Id 14 @Column(name="sensor_id") 15 @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="sensorIdSeq") 16 @SequenceGenerator(name="sensorIdSeq", sequenceName="mst_sensor_sensor_id_seq", allocationSize=1) 17 public Integer getSensorId() { 18 return sensorId; 19 } 20 21 public void setSensorId(Integer sensorId) { 22 this.sensorId = sensorId; 23 } 24 25 @Column(name="sensor_name") 26 @NotNull 27 @Size(min = 1, max = 15) 28 public String getSensorName() { 29 return sensorName; 30 } 31 32 public void setSensorName(String sensorName) { 33 this.sensorName = sensorName; 34 } 35 36 @Column(name="updatedata") 37 public Timestamp getUpdatedata() { 38 return updatedata; 39 } 40 41 public void setUpdatedata(Timestamp updatedata) { 42 this.updatedata = updatedata; 43 } 44 45 @OneToOne(mappedBy = "machine", cascade = CascadeType.ALL) 46 public MachineMailAdd getMachineMailAdd() { 47 return machineMailAdd; 48 } 49 50 public void setMachineMailAdd(MachineMailAdd machineMailAdd) { 51 this.machineMailAdd = machineMailAdd; 52 } 53 54}
MachineMailAdd.java
java
1@Data 2@NoArgsConstructor 3@AllArgsConstructor 4@Entity 5@Table(name = "mst_mail") 6public class MachineMailAdd { 7 8 private Integer mailId; 9 private Integer sendmailThreshold; 10 private String sendmailAddress; 11 private Timestamp updatedata = new Timestamp(System.currentTimeMillis()); 12 private Machine machine; 13 14 @Id 15 @Column(name="mail_id") 16 @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="mailIdSeq") 17 @SequenceGenerator(name="mailIdSeq", sequenceName="mst_mail_mail_id_seq", allocationSize=1) 18 public Integer getMailId() { 19 return mailId; 20 } 21 22 public void setMailId(Integer mailId) { 23 this.mailId = mailId; 24 } 25 26 @Column(name="sendmail_threshold") 27 @NotNull 28 public Integer getSendmailThreshold() { 29 return sendmailThreshold; 30 } 31 32 public void setSendmailThreshold(Integer sendmailThreshold) { 33 this.sendmailThreshold = sendmailThreshold; 34 } 35 36 @Column(name="sendmail_address") 37 @Size(min = 1, max = 128) 38 public String getSendmailAddress() { 39 return sendmailAddress; 40 } 41 42 public void setSendmailAddress(String sendmailAddress) { 43 this.sendmailAddress = sendmailAddress; 44 } 45 46 @OneToOne(cascade = CascadeType.ALL) 47 @JoinColumn(name = "sensor_id") 48 public Machine getMachine() { 49 return machine; 50 } 51 52 public void setMachine(Machine machine) { 53 this.machine = machine; 54 } 55 56}
MachineController.java
java
1 @RequestMapping("add") 2 String create(Model model) { 3 return "admin/machine/add"; 4 } 5 6 @PostMapping(path = "add") 7 String createAdd(@Validated Machine form, BindingResult result, Model model) { 8 if (result.hasErrors()) { 9 return create(model); 10 } 11 Machine machine = new Machine(); 12 BeanUtils.copyProperties(form, machine); 13 machine.setUpdatedata(new Timestamp(System.currentTimeMillis())); 14 machineService.create(machine); 15 16 return "redirect:/admin/machine/"; 17 }
補足情報(FW/ツールのバージョンなど)
Java
SpringBoot
PostgreSQL
回答2件
あなたの回答
tips
プレビュー