【发布时间】:2017-10-02 19:20:01
【问题描述】:
我有一个使用 spring mvc 和 hibernate 执行和更新操作的简单应用程序。但我收到错误。我想在我的数据输入中有 createdAt 和 updatedAt 日期时间。那么我怎样才能做到这一点。
POJO:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@Basic(optional = false)
@Column(name = "name")
private String name;
@CreationTimestamp
@Basic(optional = false)
@Column(name = "created_at")
@Temporal(TemporalType.TIMESTAMP)
private Date createdAt;
@UpdateTimestamp
@Column(name = "updated_at")
@Temporal(TemporalType.TIMESTAMP)
private Date updatedAt;
控制器:
@Controller
@RequestMapping(value = "/")
public class HomeController {
@Autowired
private NameDao nDao;
@RequestMapping(method = RequestMethod.GET)
public String getIndex(Model model){
model.addAttribute("message", "Welcome to Spring");
model.addAttribute("sL", nDao.getAll());
return "index";
}
@RequestMapping(method = RequestMethod.POST)
public String addName(Model model, @ModelAttribute Stamp s){
nDao.addName(s);
return "redirect:/";
}
@RequestMapping(value="/update/{id}", method=RequestMethod.GET)
public String getUpdate(@PathVariable int id, Model model){
Stamp st = (Stamp) nDao.getById(id);
model.addAttribute("s", st);
return "update";
}
@RequestMapping(value="/update/{id}", method=RequestMethod.POST)
public String postUpdate(@ModelAttribute Stamp s){
nDao.updateName(s);
return "redirect:/";
}
Index.jsp:
<form method = "post" action="${SITE_URL}/">
<div>
<label>Name: </label><input type="text" name="name">
</div>
<input type="submit" value="Submit">
</form>
<h3>List...</h3>
<c:forEach var="sl" items="${sL}">
<li>${sl.name} ${sl.createdAt} ${sl.updatedAt}<a href="${SITE_URL}/detail/${sl.id}">Detail</a><a href="${SITE_URL}/update/${sl.id}">Update</a></li>
</c:forEach>
更新.jsp:
<h1>Update Here...</h1>
<form method = "post">
<div>
<label>Name: </label><input type="text" value="${s.name}" name="name">
</div>
<input type="submit" value="Submit">
</form>
我无法将数据保存到数据库中。 not-null 属性引用了 null 或临时值:com.project.test.entity.Stamp.createdAt 错误。这里有什么问题。
【问题讨论】:
-
请为表格添加您的 DDL。
-
你去...我错过了早点发布图片...@mdeterman
-
修复的两个选项之一。通过添加 NOT NULL DEFAULT CURRENT_TIMESTAMP 或在您的实体类中删除 @Basic(optional=true) look here 来更新更新列 updated_at