SpringBoot Timestampが自動生成されない
ブラウザから商品をINSERTした時にTimestampを自動生成したい
独学でSpringBootを学習しており、Webアプリケーション作成をしています。
ECサイトを作っているのですが、ブラウザから商品を追加しようとする際、Timestampが自動生成されずNullが格納されてしまい、エラーが発生します。
コマンドプロンプトからINSERTすると自動生成されるのですが、ブラウザからだと上手くいかず、、
発生している問題・エラー
2022-08-17 15:39:42.311 WARN 75124 --- [nio-8080-exec-8] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 23502
2022-08-17 15:39:42.311 ERROR 75124 --- [nio-8080-exec-8] o.h.engine.jdbc.spi.SqlExceptionHelper : ERROR: 列"updated_at"内のNULL値はNOT NULL制約違反です
詳細: 失敗した行は(7, 革ジャン, 20000, null)を含みます
2022-08-17 15:39:42.322 ERROR 75124 --- [nio-8080-exec-8] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause
org.postgresql.util.PSQLException: ERROR: 列"updated_at"内のNULL値はNOT NULL制約違反です
詳細: 失敗した行は(7, 革ジャン, 20000, null)を含みます
該当するソースコード
GoodsForm.java
package com.example.ecsite.model.form;
import java.io.Serializable;
import java.sql.Timestamp;
public class GoodsForm implements Serializable{
private static final long serialVersionUID = 1L;
private long id;
private String goodsName;
private long price;
private Timestamp updated_at;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
public long getPrice() {
return price;
}
public void setPrice(long price) {
this.price = price;
}
public Timestamp getUpdated_at() {
return updated_at;
}
public void setUpdated_at(Timestamp updated_at) {
this.updated_at = updated_at;
}
}
AdminController.java
package com.example.ecsite.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.example.ecsite.model.dao.GoodsRepository;
import com.example.ecsite.model.dao.UserRepository;
import com.example.ecsite.model.entity.Goods;
import com.example.ecsite.model.entity.User;
import com.example.ecsite.model.form.GoodsForm;
import com.example.ecsite.model.form.LoginForm;
//Controllerクラスはmodel,viewの橋渡しをする。
//@ControllerをつけることでSpringBootはそのクラスをControllerとして認識する
//@RequestMappingはリクエストURLに対して、どのクラス、メソッドの処理を行うかマッピングすることができる。
//localhost:8080/ecsite/adminでアクセスできるよう設定する
@Controller
@RequestMapping("/ecsite/admin")
public class AdminController {
//UserRepository,GoodsRepositoryを読み込む。
@Autowired
private UserRepository userRepos;
@Autowired
private GoodsRepository goodsRepos;
//adiminindex.htmlに遷移するメソッド
@RequestMapping("/")
public String index() {
return "adminindex";
}
//"/welcome"をPOSTにする
//welcome.htmlに遷移するメソッドを定義
@PostMapping("/welcome")
public String welcome(LoginForm form, Model m) {
//下記ListはUserNameとpasswordでUserを検索する為のList
List<User> users = userRepos.findByUserNameAndPassword(form.getUserName(), form.getPassword());
//検索結果が管理者かどうか判断し、管理者だった場合のみ処理するもの。
if(users != null && users.size() > 0) {
boolean isAdmin = users.get(0).getIsAdmin() != "f";
if(isAdmin) {
List<Goods> goods = goodsRepos.findAll();
m.addAttribute("userName", users.get(0).getUserName());
m.addAttribute("password", users.get(0).getPassword());
m.addAttribute("goods", goods);
}
}
//管理者じゃなかったらwelcome.htmlを返す
return "welcome";
}
//新規商品登録と削除機能を追加
@RequestMapping("/goodsMst")
public String goodsMst(LoginForm form, Model m) {
m.addAttribute("userName", form.getUserName());
m.addAttribute("password", form.getPassword());
return "goodsmst";
}
@RequestMapping("/addGoods")
public String addGoods(GoodsForm goodsForm, LoginForm loginForm, Model m) {
m.addAttribute("userName", loginForm.getUserName());
m.addAttribute("password", loginForm.getPassword());
Goods goods = new Goods();
goods.setGoodsName(goodsForm.getGoodsName());
goods.setPrice(goodsForm.getPrice());
goods.setUpdatedAt(goodsForm.getUpdated_at());
goodsRepos.saveAndFlush(goods);
return "forward:/ecsite/admin/welcome";
}
}
0 likes