有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Hibernate自动生成Id似乎不起作用

我有一个名为Post的实体,它以Id作为主键

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "Id", unique = true, nullable = false)
public Integer getId() {
    return Id;
}

public void setId(Integer id) {
    Id = id;
}

在上面的代码中,您可能会看到GenerationType.IDENTITY,我假设它负责在创建新的Post时生成新的ID

然而,当我用这个JSON做一个HTTP POST时:

{"id":0,"title":"test","viewCount":0,"body":"test","tags":"","answerCount":0,"commentCount":0,"postTypeId":1,"favoriteCount":0,"creationDate":"Nov 25, 2015 11:43:22 AM","acceptedAnswerId":0,"lastEditorUserId":0,"score":0}

我得到这个错误:

 SEVERE: Servlet.service() for servlet [rest] in context with path [/StackExchange] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: could not execute statement; nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement] with root cause
java.sql.SQLException: Field 'Id' doesn't have a default value

不过我还没有设置AI,也许这就是原因。但是我不能改变桌子。表中的数据来自SQL转储,我无法将其更改为将Id设置为AutoIncrement


共 (0) 个答案