How we can make the id field of the string type a primary key?
Entity id
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private String id;
@GeneratedValue(strategy = GenerationType.IDENTITY)
private String id;
Trying to save
Student student = new Student();
student.setId("tetasasa");
student.setName("Peter Smith");
student.setEmail("peter.smith@gmail.com");
student.setAddress("New York, USA");
student.setPhone("123456-2111");
studentRepository.save(student);
student.setId("tetasasa");
student.setName("Peter Smith");
student.setEmail("peter.smith@gmail.com");
student.setAddress("New York, USA");
student.setPhone("123456-2111");
studentRepository.save(student);
Error
Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: org.example.entities.Student
Solution
@Id
@GeneratedValue(generator="system-uuid")
@GenericGenerator(name="system-uuid", strategy = "uuid")
private String id;
For more details about id generation please refer this -
https://thorben-janssen.com/jpa-generate-primary-keys/
Comments
Post a Comment