Spring Data JPA Tutorial - #9 - Primary key generation strategies AUTO, IDENTITY, SEQUENCE and TABLE

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi welcome back in previous lecture we have seen how to use jp annotations to customize the table structure right in this lecture we will see four types of jpa provided primary key generation strategies well basically jps specification supports four different primary key generation strategies that generate the primary key values programmatically or use database features like auto incremented columns or sequences okay and here are the four primary key generation strategies like auto identity sequence and table let's discuss each of these primary key generation strategies one by one well let's first take a look into generation type ito well generation type ato is a default generation type and it lets the persistence provider choose the generation strategy well whenever we specify generation type as ato then this value will let the persistence provider of the database vendor to choose the generation strategy okay and in order to specify generation type ato we have to use add generated value annotation so you can see here code snippet we are using add generated value annotation and it has the attribute strategy and we can specify generation type ato as a value to the strategy attribute okay and if you are using hibernate as a gpa implementation or the jpf you know persistence provider then hibernate will basically select the generation type based on the database specific dialect well most of the popular relational databases like postgresql database oracle database mysql database so these databases uses sequences to generate a primary keys for the tables right so hibernate will basically you know use or select the primary key generation strategy based on the database specific dialect and most of the time hibernate use a generation type sequence as a primary key generation strategy because most of the you know relational databases especially the popular relational databases like postgresql database oracle mysql so all these relational databases support the sequences so hibernate will most of the time uses sequence as a primary key generation strategy now let's go and let's see how to use a generation type ato in our product jp entity and how hibernate will behind the scene uses a generation strategy as a sequence in our project well let's head over to the intellij idea and here we have used identity as a primary key generation strategy so now we are going to replace from identity to ato okay great now i am going to run the spring boot application and we will see how hybrid it will use a sequence as a generation strategy let's go and let's run the springboot application so look at here in a console hibernate basically created one more table called hibernate underscore sequence so this is the default name okay so each time we insert a record in the table then hibernate will you know get the value from this table and it will increment it and it will again insert that one okay so basically abundant will use this hibernate underscore sequence table to maintain a sequences for the primary keys for the table all right and mysql database it doesn't have a sequence mechanism that's why hibernate created separate table to maintain the sequences but if you use the other relational databases like postgresql database or oracle databases so these databases have their own sequence mechanism okay so in this case we are using mysql database so ibund it will create a new database table to maintain the sequences all right now if you can head over to the mysql workbench and refresh the schemas and you can able to see hibernate underscore sequence a table is created to maintain the sequence for the primary keys okay so this is how we can use a generation type ato to provide a primary key generation strategy okay so this will basically let the persistence provider to select the primary key generation strategy and we are using hibernate so i haven't most of the time use sequence as a primary key generation strategy all right i hope you understood how to use a generation type i2 well the next is generation type identity well this basically relies on the auto incremented database column and lets the database generate a new value to each insertion operation well probably you are familiar with auto increment you know database column right so this is the sql ddl script property auto increment so each time we insert a new row in the database then database basically you know auto increment a primary key value right so we can do that by using generation type identity so in order to specify generation type identity we simply pass a generation type dot identity as a value to the strategy attribute of add generator value annotation okay and from database point of view this is a very efficient because the auto increment columns are highly optimized and it does not require any additional statements well identity primary key generation strategy has some drawbacks if we use hibernate for instance hibernate will immediately create the primary key and then it will insert the record in the database right and this is not good for jdbc batch operations and if you are not using jdbc batch operations in your project then you can go ahead and use identity as a primary key generation strategy all right now let's go and let's see how to use this identity primary key generation strategy in our product jp entity well let me go to the english idea and let me replace from auto to identity now what i will do i will simply drop the tables okay so i am going to drop these two tables because i want to see how this works okay how this identity primary key generation strategy works all right now let me run the springboot application and let's see how hibernate will create a table and primary key for the products table well look at here hibernate basically created this sql statement behind the scene and for identity primary case status hibernate added auto increment as a primary key generation strategy all right and this auto increment is basically you know done by the database database will basically create a new value and it will add as a primary key in the in the table isn't it so just remember identity primary generation strategy it relies on the auto incremented value of the column well the next primary key generation strategy is sequence well sequence is the most commonly used primary key generation strategy in a large applications all right and i'm going to suggest you to use a sequence as a primary key generation strategy if your application is huge all right and you can see here we can specify sequence as a primary key generation strategy using add generated value annotation and add sequence generator annotation well we can use add sequence generated you know annotation to specify the sequence name along with that allocation size well here allocation size equal to one meaning the sequence can increment by one all right and by default allocation size is 50 and once we generate the sequence we can provide its reference in at generated value annotation for example here look at here add sequence generator has a name product underscore generator and this name we should give here okay you know add generated value annotation all right we'll see an example how we can use it and sequence primary key generation strategy basically uses you know additional select statement to get to the next value from the database sequence but this has no performance impact on most of the applications all right and most of the relational databases you know supports a sequence for example postgresql database oracle database mysql database all right so let's go and let's see how to use sequence as a primary key generation strategy in our product jp entity well let me go to the intellij idea and let me first of all you know drop the tables let me drop this product table first because we'll see how sequence primary key generation strategy works right now what i'm gonna do is i'm gonna simply use sequence okay and we can use at sequence generator annotation to generate the sequence okay so if you don't specify sequence by using this annotation then hibernate will by default create a sequence for you all right so here i'm going to provide a sequence okay as per my requirement for example let's say i want to give name to the sequence for instance let's use name here so let me align properly so let's use name to give name to the sequence let's say product generated something like that and let's use sequence name something like product underscore sequence underscore name okay and let's give our location size by default it is 50 so we can give one okay so here one meaning we will tell hibernate that create a sequence uh by incrementing one okay and once we generate the sequence now we need to provide a reference to the add generated value annotation okay so this annotation has a generator attribute okay generator and then simply simply pass this okay copy and paste it here okay so this is how we basically create a sequence for this primary key okay so yeah here we are basically using add sequence generator annotation to specify sequence manually okay so if we don't specify this sequence then hibernate will create a default sequence table for us right and here basically we are explicitly specifying the sequence okay the sequence name is this and this is the name to this sequence to just provide a reference to this at generated value annotation here all right great now let's one let's run the springboot application and let's see how this works so look at here hibernate basically created a lot of sql statement all right let's go one by one so this is the sql statement to create the products table and then this is the sql statement to add a constant and you can see this table product underscore sequence underscore name so this is the table the hibernate created to maintain the sequences and you can see in a product jp entity we have given a sequence name as product underscore sequence underscore name right and hibernate will basically use this sequence name to create a new table to maintain the sequences all right great now let's head over to the mysql workbench and let's refresh the schemats and you can able to see product sign product underscore sequence underscore name so these two tables are created and this table is to maintain the sequences and as of now you can see value 1 well each time we insert a new row in the product table then hibernate will trigger one more select query to get the value from this sequence table let's go and let's take a look into the next primary generation strategy that is table well this primary generation strategy is rarely used nowadays because it simulates a sequence by storing and updating its current value in the database table which requires the use of pessimistic locks which puts all the transaction into a sequential order and this will basically slow down your application and you should therefore prefer using sequence as your primary generation strategy all right so table primary generation strategy is rarely used nowadays okay and hibernate team basically suggest or recommend to use a sequence as a primary key generation strategy well let's go and let's see how to use table as a primary generation strategy in our product jp entity okay so before that let me go ahead and let me drop the tables so that we can see how the table primary generation strategy works all right now i'm going to simply you know remove these lines of code and also i'm going to remove this one and i will simply specify generations type as a table perfect now let me run the springboard application and let's see how this works well look at here hibernate generated as close statements so this looks similar to sequence primary generation strategy but it uses different mechanism that's why i bennett don't you know recommend this approach all right so i haven't recommend to use sequence as a primary key generation strategy so let me simply you know undo it and let's use a sequence as a primary key generation strategy all right so these are the four primary key generation strategy that jpa supports all right and we have seen all of them with live example all right in next lecture what we'll do we'll see how to use hibernate provided few of the important annotation to populate date created and last updated you know values alright i will see you in next lecture
Info
Channel: Java Guides
Views: 24,181
Rating: undefined out of 5
Keywords:
Id: n_tc6Nc4tfI
Channel Id: undefined
Length: 15min 48sec (948 seconds)
Published: Mon Aug 29 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.