Saturday, March 6, 2021

how to save data using jpa repository without primary key

How to use spring data jpa repository if there is no primary key?
How to accessing Table without primary Key -Spring data jpa?
How to save data in spring boot without primary key? 

 As we know primary key is required for Entity in spring data jpa repository  if you don't want to use integer ID, we can add some unique string Id for it, So we can generate uniq Id for this.

@Id
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "uuid2")
@Column(length = 36)
private String id;
OR
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid")
@Column(name = "UUID_ID")    
private String id;
OR
@Id
@GeneratedValue(generator="system-uuid")
@GenericGenerator(name="system-uuid", strategy = "uuid")
@Column(name = "uuid", unique = true)
private String uuid;
OR
@Id 
@GeneratedValue
private java.util.UUID id;

No comments:

Post a Comment