In below there is question and answer, What I need is I want comment about the a
ID: 3599070 • Letter: I
Question
In below there is question and answer, What I need is I want comment about the answer( feel free to agree or disagree with answer and explain why you agree or disagree)
Question:
Discuss some of the primary key generation strategies in JPA and when and why would you use the a certain approach.
Answer:
There are 4 ways JPA offers for primary key genration:
Auto, Identity, Sequence, and Table. In practice I feel the only way to do primary key generation is to look at the data you have. You ahve to build your tables first based on how you would wnat them independent of the generation method. Then you fit which ever generation method to the desired table you want. Overall, I think identity(auto incremention) is the most useful. This could be used hwen you want to give each user an unique ID.(it occurs most in real life)
Explanation / Answer
It is true that JPA offers 4 options to generate primary keys:
GenerationType.IDENTITY: Auto Increments the database column with each new insert and thereby inserting a new primary key each time. But in many options also it may not be so optomized to use this method.
GenerationType.SEQUENCE: Uses Database Sequence to generate new values as primary keys. It is also a good method based on that you have to query the DB (select query) to get the orimary key values.
GenerationType.TABLE: This method isn't used so much. Uses a database table to sequence
GenerationType.AUTO : Default Generation Type and lets the persistence provider choose the generation startegy which is generally the GenerationType.IDENTITY type.
The above are all the strategies. However selecting one for a database would vary from requirement to requirement. Generally for defualt user the AUTO or IDENTITY is used as it reduces headache and where auto increment is preferred. Any other types of customizations to the values would reuqire a different method.
Thanks.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.