実現したいこと:型の変換をしてDBにinsertする
エラー内容
There was an unexpected error (type=Bad Request, status=400).
Validation failed for object='kakeiboEntity'. Error count: 1
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'kakeiboEntity' on field 'id': rejected value []; codes [typeMismatch.kakeiboEntity.id,typeMismatch.id,typeMismatch.long,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [kakeiboEntity.id,id]; arguments []; default message [id]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'long' for property 'id'; nested exception is java.lang.NumberFormatException: For input string: ""]
Entityクラス
@Entity
@Table(name="kakeibo")
public class KakeiboEntity {
**@Id @GeneratedValue(strategy=GenerationType.AUTO) @Column private long id;** @Column(nullable=false) private String date; @Column(nullable=false) private String koumoku; @Column(nullable=false) private int kingaku; @Column(nullable=true) private String syousai; @Column(nullable=true) private String bikou;
(getter,setter省略)
Dtoクラス
public class KakeiboDto {
private long id;
private String date;
private String koumoku;
private int kingaku;
private String syousai;
private String bikou;
(getter,setter省略)
Fromクラス
public class KakeiboForm {
private long id;
@NotNull
private String date;
private String koumoku;
@NotNull
private int kingaku;
private String syousai;
private String bikou;
HTML
<input type="hidden" name="id" value="">
Controllerクラス
@PostMapping("/insert")
public String insert(Model model,@ModelAttribute KakeiboForm form,BindingResult bindingResult,
@Validated KakeiboEntity kakeiboentity ) {
if(form.getKingaku()==0) {
model.addAttribute("title", "タイトル");
model.addAttribute("errmsg","金額は必須項目です");
return "init";
}
KakeiboDto dto=new KakeiboDto();
BeanUtils.copyProperties(form, dto);
KakeiboDto registerdata=service.register(dto);
repository.saveAndFlush(registerdata);//エラーで落ちる
model.addAttribute("msg",dto);
return "update";
}
あなたの回答
tips
プレビュー