java-错误:JPA中GeneratedValue的给定id不能为null
作者:互联网
我的对象:
@Entity
@Table(name="user")
public class User {
@Id
@Column(name="uid")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
//more code
}
当我不带uid的情况下发布用户JSON时,由于给定的id不能为null,我得到了错误. 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/"
}
解决方法:
这很糟糕,我在保留User对象之前调用了foo(user.getId()).
无论如何,要远离它; @GeneratedValue(strategy = GenerationType.IDENTITY)是正确的代码,并且在持久化时会生成相同的ID.和龙不是问题.谢谢.
标签:spring-rest,spring-boot,rest,jpa,java 来源: https://codeday.me/bug/20191111/2017641.html