Error: given id must not be null for GeneratedValue in JPA

My object:

@Entity
@Table(name="user")
public class User {
    @Id
    @Column(name="uid")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;

  //more code
 }

      

When I am POST

user

JSON

without uid

, I get an error as the given id should not be null. Which shouldn't be the case if it uid

should be generated by the database. Please point out what I am missing.

JSON:

{
"email": "john@mail.com",
"name": "John Doe",
"phone": "98-765-4445"
}

      

Mistake:

{
"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/"
}

      

+3


source to share


2 answers


It was bad, I called foo(user.getId())

before continuing the facility User

. In any case, avoid it; @GeneratedValue(strategy=GenerationType.IDENTITY)

is the correct code and it generates identical IDs when saved. And Long

not a problem. Thank.



+2


source


To create a uuid string for the primary keys (as I assume you are trying to do), you can try the following code:



@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
private String id;

      

0


source







All Articles