【发布时间】:2018-01-01 11:46:50
【问题描述】:
我的对象:
@Entity
@Table(name="user")
public class User {
@Id
@Column(name="uid")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
//more code
}
当我 POST user JSON 没有 uid 时,我收到错误,因为 给定的 id 不能为空。当uid 应该由数据库生成时,情况不应该如此。请指出我错过了什么。
JSON:
{
"email": "john@mail.com",
"name": "John Doe",
"phone": "98-765-4445"
}
错误:
{
"timestamp": 1501058952038,
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.dao.InvalidDataAccessApiUsageException",
"message": "The given id must not be null!; nested exception is java.lang.IllegalArgumentException: The given id must not be null!",
"path": "/api/user/"
}
【问题讨论】:
-
你可以试试 --> @GeneratedValue(strategy = GenerationType.TABLE),这个使用sequence table id assignment
-
不,它不起作用。
-
@GeneratedValue(strategy = GenerationType.AUTO) 将起作用,参考:stackoverflow.com/questions/4102449/…
标签: java rest jpa spring-boot spring-rest