not-null属性引用null或transient值:com.project.test.entity.Stamp.createdAt

时间:2017-10-02 19:20:01

标签: java spring hibernate

我有一个简单的应用程序,可以使用spring mvc和hibernate来执行和更新操作。但我收到了错误。我想在我的数据输入中创建和更新日期时间。那么我怎样才能做到这一点。

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>

Update.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或transient值:com.project.test.entity.Stamp.createdAt 错误。这是什么问题。

我的数据库架构是: enter image description here

1 个答案:

答案 0 :(得分:0)

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

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

删除updatedAt属性上的@Basic(optional = false)注释,因为此属性在insert上为null。我对吗?