我的目標:錯誤:給定的ID不能用於GeneratedValue爲空在JPA
@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": "[email protected]",
"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),這個使用序號表編號分配 – JavaLearner1
不行,不行。 –
@GeneratedValue(strategy = GenerationType.AUTO)將工作,參考:https://stackoverflow.com/questions/4102449/how-to-annotate-mysql-autoincrement-field-with-jpa-annotations – JavaLearner1