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_number
varchar) 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 exist13 Replies
⌛
This post has been reserved for your question.
Hey @Archa! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose 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.
What do you mean with the primary key not being id?
@Id
is how you tell JPA what the primary key isSorry i mean not annotation
Id
but parameter id(int; auto increment)
Usually i found tutorial used like this
But this is how i use
Is it not good if i use String reff
instead Long id
?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
when using function
save()
is it gonna select all data
to checking if my primary key duplicate or not first before execute save()
??
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
firstyeah it has to check whether the entry already exists
With
@GeneratedValue
, it can probably just generate a new entry without querying firstIf i want use
@GeneratedValue
the parameter have to auto increment?@GeneratedValue
means JPA will somehow make sure to fill the elementsIs 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 errorI 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 String
s💤
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.