【问题标题】:not-null property references a null or transient value : com.project.test.entity.Stamp.createdAt非空属性引用空值或瞬态值:com.project.test.entity.Stamp.createdAt
【发布时间】: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}&nbsp;${sl.createdAt}&nbsp;${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

标签: java spring hibernate


【解决方案1】:

我的猜测是您的数据库表中有空值。

在您的数据库中,updatedAt 可能为 null,但您将 JPA 属性设置为非可选。这是不一致的。

删除 updatedAt 属性上的 @Basic(optional = false) 注释,因为该属性在插入时为空。我对吗?

【讨论】:

  • 是的,请查看问题。我已经提供了我的数据库架构图片。
猜你喜欢
  • 2017-04-25
  • 1970-01-01
  • 2011-12-23
  • 2010-10-08
  • 2012-06-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多