JPA Entity Manager Details | How we can make the id field of the string type a primary key?

 

How we can make the id field of the string type a primary key?


Entity id  

@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);


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

Popular posts from this blog

12 Must-Know LeetCode+ Links for Coding Excellence

Stay Updated As Software Engineer

Getting Master in Hibernate and JPA