JPA When Primary Key Not Id

I'm new using spring and jpa, i notice when my primary key is not id is gonna effect performance and take more time for process when i try save/insert data, is it just my conclusion but is it true when primay key is not id (reference_numbervarchar) gonna doing select all data for checking if primary key already exist or not? I hope i could still using reference_number as primary key and prevent jpa to select all data and just try insert data and throw error when primary key exist
13 Replies
JavaBot
JavaBot2mo ago
This post has been reserved for your question.
Hey @Archa! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st2mo ago
What do you mean with the primary key not being id? @Id is how you tell JPA what the primary key is
Archa
ArchaOP2mo ago
Sorry i mean not annotation Id but parameter id(int; auto increment) Usually i found tutorial used like this
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
But this is how i use
@Id
@Column(name = "reference_number")
private String reff;
@Id
@Column(name = "reference_number")
private String reff;
Is it not good if i use String reff instead Long id?
dan1st
dan1st2mo ago
the name doesn't matter as long as it matches your schema For the data type, different databases might handle diferent data times better or worse
Archa
ArchaOP2mo ago
when using function save() is it gonna select all data to checking if my primary key duplicate or not first before execute save()?
dan1st
dan1st2mo ago
?
Archa
ArchaOP2mo ago
When i use this entity and then using jpa for insert data into database using function save() my service take a long time, when i trace it when function save() execute query insert it will execute query select first
dan1st
dan1st2mo ago
yeah it has to check whether the entry already exists With @GeneratedValue, it can probably just generate a new entry without querying first
Archa
ArchaOP2mo ago
If i want use @GeneratedValue the parameter have to auto increment?
dan1st
dan1st2mo ago
@GeneratedValue means JPA will somehow make sure to fill the elements
Archa
ArchaOP2mo ago
Is it possible to avoid check entry without using @GeneratedValue I want put the entry @id and force to insert new entry without checking duplicate entry even it will throw error
dan1st
dan1st2mo ago
I think if you use @GeneratedValue for the @Id field and that field is null before inserting, that check shouldn't be necessary but it depends on the strategy and idk whether it works with Strings
JavaBot
JavaBot2mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server