Id的Hibernate自动生成似乎并没有起作用

时间:2015-11-25 11:06:38

标签: java mysql hibernate

我的实体名为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。

但是,当我使用HTTP POST

制作JSON
{"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 个答案:

没有答案