Spring Data JPA Tutorial | Crash Course [2024]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone ramesh here welcome to my youtube channel i am going to start a new series on spring data gpa well this series is basically for beginners who want to learn how to use spring data jp in a spring boot project well this is a very basic series or course on spring data gpa but if you want to master spring data gpa then i am going to suggest my udemy course that is masters ping data gpa with hibernate so this is a 15 hour course and in this course you will learn everything about spring data gpa and you will also learn how to build a domain model relationship for simple e-commerce project using hibernate vm framework well this masters spring data jpeg with hibernate course is highest rated course on video so this is a very popular course on udemy so i am going to suggest you to check out this course in order to learn spring data jp with hibernate in depth well this spring data gps series has around 25 lectures or 25 lessons okay so i am going to cover a basic features of spring data gpa in this series and if you are beginner then i am going to suggest you to watch this series so that you will learn how to use spring data jp in a spring boot project alright lets kick start this series with what is spring data gpa hi welcome back in this lecture we'll take a look into what is spring data gpa well before understanding about spring data jv let's first understand what is the problem and what problem spring data jp solved well consider we have java application architecture over here so this is basically called a three layer architecture we have a controller layer service layer and dowel layer and typically we use jpr hibernate to develop the dao layer all right well if we use hibernate to develop the dialer then we need to write a lot of code for example let me jump into github repository here i have created spring hybrinet project to just showcase how much amount of code that we need to write in order to develop the dowel layer using hibernate well if we use hibernate then we need to create a interface for example customer tao in this case and we need to define all these crude methods in this interface next we need to create a class for example customer mpl class which implements this interface and override all the methods from this interface and provide the implementation all right so if you use habenite to develop the dowel layer or the repository layer then we need to write a lot of code right we need to create an interface and then we need to define crude methods in interface next we need to create a class which implements this interface and its methods right well if you closely observe this code for single entity that is customer entity we are writing this much amount of code let's say in your project you have a lot of entities for example you know student uh employee user order right so in order to develop the dowel here for these entities we need to create an interface and then we need to define a crude methods and then we need to create a class and then we need to implement interface and its methods right we need to basically write a lot of code for a entity isn't it and if you closely observe this code here you can see only the difference the entity name and the primary key type rest of the code remains same for all the entities alright so this is basically a problem we are basically repeating the same code again and again right so instead of repeating the same code why not we basically write a library and keep all the generic code over here so that is what the spring data jp basically does well i have created one more project which demonstrate the usage of spring data jpa by using spring data gpa we can reduce a lot of code here for example we just need to create an interface and extend spring data jpi provided interface that is jp repository interface and just we need to pass entity name and the primary key type that's it we will get a full crude operations for this given entity that is customer entity right so this is how spring data gpa basically provides a solution to reduce a lot of amount of code that we need to write you know in order to develop the dowel here well spring team has developed a spring data jp library to reduce amount of boilerplate code that is required to develop that value for example if you can just head over to spring official website here you can see under project section you can see spring data framework well within spring data framework you can see spring data jpa model okay and we use bing data jpa model to develop the repository layer and spring data jp basically reduce amount of boilerplate code that is required to develop the dowel layer or jpa based repositories well spring data framework basically provides all these modules for different purposes and in this course we are going to use ping data jpa model to see how to you know reduce the amount of boilerplate code that is required to develop the dao layer all right we are going to learn a lot about spring data jpa in this course well i hope you pretty much understood what is bing data gpa well spring data gpa is basically used to reduce the amount of boilerplate code required to implement data access object layer and spring data jp is not a jpi provider it simply hides the java positions api behind its repository abstraction it means that spring data jp is not a jpi provider for example hibernate is basically you know jpi provider right but spring data is not a gpa provider it's simply you know developed on top of jpa well basically spring data gpa is abstraction layer on top of java persistence api right and entity manager is the interface of java persistence api well basically spring data jpa has a jpeg repository interface it internally uses all the jpa provided methods for example this is the jpa repository interface okay it has a methods like flush save and flush save delete okay exist count find all so these are all the methods belongs to jp repository interface and all these methods internally uses entity manager api to perform different operations with the database okay so this jpeg repository interface methods don't directly connect to the database these repository methods will first internally get call to the entity manager methods and these entity manager methods will talk with the database okay so this repository is basically is an abstraction on top of jpa interfaces or the apis all right i hope you understood how the jpi repository methods will internally call entitymanager methods to talk with the database okay this is how spring data jpa basically works all the spring data jpeg repositories internally uses entity manager to talk with the database okay so spring data gpa is just abstraction on top of jpa that is java positions api well if you are developing spring boot project and if you want to develop a repository layer or dowel here for relational databases then you can go ahead and use spring data jpm well in next lecture we are going to take a look into basic blow up spring data gpa then you will able to understand how spring data gpa works behind the scene all right great i will see you in the next lecture hi welcome back in this lecture let's quickly understand the basic blow up spring data jpa and this diagram shows the basic flow at the time of accessing the data from the database table using spring data gpa and if you look at the steps here one two three four five six well whenever we perform any database operation using spring data gpa then these steps these six steps will be you know performed internally well we basically call gp repository methods from the service layer for example let's say we want to save an entity into the database table using spring data gpa then what we basically do we inject repository that is jp repository in a service layer and then we call it save method and then we pass the entity as a parameter right so this is the first step second step is like jpeg repository is just a interface and there should be an implementation for jpeg repository interface and its methods right spring data jpa provides simple jpi repository class which implements jpeg repository interface as well as its methods and spring data gpa also provides flexibility to write your own custom repository implementation class all right so consider in this flow we are using simple jpeg repository class which implements jpeg repository interface so this is the second step third step is spring data jp internal uses jba apis it means that spring data jpg just abstraction layer on top of gpa to reduce the boilerplate code okay so all the spring data jpa methods internally uses jpa apis in order to talk with the database alright so this is the third step now as we know that jpa is just a standard specification or a standard api and it doesn't have any implementation right and there should be an implementation for jpa apis well spring data jpeg internally by default uses hibernate as a jp implementation or jpa provider so in this case consider we are using hibernate as a jpa implementation all right so jpa api internally called hybernet core apis and then hybernet will basically generate the sql queries based on the method call for example let's say we want to purchase an object into a database table then hibernate will create the insert sql query and then it will internally call gdbc basic apis and then jdbc basic api will internally talk with the database okay so this is how the flow goes service layer will call spring data jpi repository methods and it will pass the required parameters and then spring data jp internally uses jp apis and it will pass the required parameters and jp api internally call hibernate core apis and it will pass the required parameters and then hypernet query api will generate the sql query based on the method call and it will bind all the values and then hibernate query api internally use you know jdbc apis to talk with the database all right so notice here hibernate internal uses gdbc to talk with the database and execute the sql queries alright this is how the flow goes whenever we perform any database operation using spring data jpa and if you can notice here spring data gpa is just a abstraction layer on top of jpa to reduce the boilerplate code okay and spring data jp internal uses jp apis and hibernate implementation okay so it means that spring data gpa is just abstraction layer on top of gpa to reduce a boilerplate code alright i hope you pretty much understood the basic flow of spring data jpa all right i will see you in next lecture hi welcome back in this lecture let's quickly take a look into the difference between hibernate and spring data jpa well hypernet is basically is an implementation of jpa specification well jp is just a specification or a standard api that facilitates object relational mapping to manage relational data in a java applications well in java jpa basically provides object relational mapping feature okay it means that instead of creating the sql statements we can directly persist an object into relational database table using jpa okay and jp is just a specification it don't have any implementation right so that is where the hibernate comes into picture hibernate is basically a warren framework that implements jps specification okay so hibernate is a jpa implementation provider all right on the other hand if you can see spring data gpa well spring data jp is abstraction layer on top of gpa to reduce the amount of boiler plate code required to implement dow layer okay so spring data gp is nothing but it is abstraction layer developed on top of jpa to reduce the amount of boilerplate code that is required to implement the data access object all right so spring dataship it internally uses jpa apis and hibernate basically implements jpa apis and if you can see here hibernate internal uses gdbc to you know communicate with the database this is how the basically pro goes spring data jp internally uses jpa apis and hibernate you know it implements jp apis and hibernate internally uses jdbc to communicate with the database well let me summarize the difference between hibernate and spring data jpa well hibernate is a java based volume tool that provides a framework for mapping application domain model objects to the relational database tables and vice versa well if we use the hibernate volume framework then we don't have to create the sql statement to perform different operations like crore operations basically like insert operation select delete and update right so hybrid it basically behind the scene a generate sql statement and it will use the gdbc to communicate with the database we don't have to create the sql statement if we use hibernate volume framework right so we can use ibnet framework to directly map application domain model objects to the relational database tables it means that we can use a hybrid to directly persist an object into a relational database table all right we don't have to create the sql statements well on the other hand spring data jp is abstraction layer on top of gpa to reduce the amount of boilerplate code required to implement dao layer well spring framework basically provides spring data jpa model which is developed on top of gpa to reduce the amount of boilerplate code required to you know implement data access object layer well spring data jp8 minimizes a lot of boilerplate code guys we just have to you know extend spring data jk provided a repository interface that's it we will get a full crude operations and spring data gpa provides out of the box implementation for all the crude operations hibernate is a jpi provider it means hibernate it implements jpa specification on the other hand spring data jp is not a gpa provider spring data gpa simply hides the jpa behind its repository abstraction it means that spring data jp it is developed as abstraction layer on top gpa to reduce the boilerplate code required to implement dao layer all right well spring data jp uses hibernate as a default gpa provider well spring boot offers a starter dependency to use spring data jp in a spring boot project and spring boot starter data jpeg dependency it internally uses hibernate as a default gpa provider for spring data gpa spring data gpa cannot work without a gpa provider well there can be a multiple gpa previews for example hibernate eclipse link top link mybatis okay so there are a lot of implementations for jpi specification right and spring data gpa cannot work without gpa provider we have to provide one of the gpa provider in order to work with spring data gpa you can use hibernate or you can use eclipse link you can use my batteries you can use top link okay you have to use one of the gpa provider in order to work with spring data jpi alright so this is the difference between hibernate and spring data jpa alright i will see you in next lecture hi welcome back in this lecture we will create a springboot project using spring initializer and then will import that springboot project in intellij idea well let's open your favorite browser i have opened chrome browser over here and just type start.spring.io this will brings up spring initializer website well spring initializer is basically a web tool that we can use to quickly create spring based applications all right now let's go and let's fill up all the project related information well let's keep project as marvin project here as selected and language java so you have a couple of options like java kotlin groovy we're going to use java so let's keep java selected over here and here we need to basically choose a spring boot version and by default 2.6.3 is selected okay so this is basically recommended by spring boot team so let's keep this stable and latest version of spring boot as it is all right and this may be you know different in your case as of now it is 2.6.3 but when you create your springboot project that time this version may different now let's go ahead and let's fill up the project metadata over here let's give group as something like com dot spring data jpa all right and artifact id let's go and let's give spring data jpa course and the project name is same as artifact and demo let's go let's give something like spring data jpa with spring boot all right and package name let's keep com dot spring data jpa dot spring boot all right and here packaging let's keep jar as selected and java version 11 you can choose java 17 or 8 as per the java installation on your machine but as of now i'm going to keep java 11 as selected as it is now let's go ahead and let's select the dependencies go ahead and click on add dependencies over here and just type spring data jpa well spring data jpeg is a starter dependency provided by spring boot team and this spring data jpa starter dependency include all the required libraries to develop the positions layer of the dow layer all right and spring data gpa by default uses hibernate as a jba implementation okay basically we use bing data jpa to reduce a lot of boilerplate code that is required to develop the dow layer all right so go ahead and select spring data gpa dependency we will explore more about spring data jpa starter dependency in the next lecture now go ahead and select one more dependency that is my sql driver so in this course we are going to use mysql database so let's go let's use mysql driver java dependency so this dependency we basically used to connect our springboot application with mysql server well spring data jpi and mysql driver these two dependencies are and up as of now and you can explore this spring boot structure by using explore option over here just go ahead and click on explore and you can see the dependencies in the palm.xml for example click on palm.xml over here and you can see springboot starter data jpeg dependency and mysql connect connector java dependency so these are the two dependencies we have added and by default spring boot starter test dependency is you know added by spring initializer so whenever you create a spring boot application using spring initializer then this spring boot starter test dependency by default included in the form.xml file alright so go ahead and close it once you are happy with all these details go ahead and click on generate to generate this spring boot application as a zip file so i am going to click on generate over here and this will basically generate a spring boot application as a j file and it will download in the file system now let's go and let's open this g file in the file system and as this is the g file let's go and let's extract it all right now let's open the english idea and let's import this project so i'm going to open the integer id over here and you can see open option so go ahead and click on open option here and just go to the location where your springboard application is downloaded so in my case i go to downloads folder and then spring data jpi course project i have just selected over here and click on open so this will basically import spring boot application in the integer idea well basically increase your idea will take couple of seconds to you know import spring boot project successfully because it will it need to download all the dependencies and it need to set up the application properly all right so you can see here resolving dependencies let's wait couple of seconds all right our springboot application is successfully imported in the integer idea just you can go ahead and expand this project and you can able to see this is the spring boot main entry point class and you can run your spring boot application using this main entry point class all right and if you can open palm.xml and you can see springboot starter data jp dependency well we'll explore more about this dependency in the next lecture and you can go to the external libraries over here you can see all the libraries are added in the class path all right well let me recap what we have done in this lecture we have basically created spring boot application using spring initializer and then we have imported in the intellij idea in next lecture we will explore more about spring boot starter data gpa dependency all right i will see you in the next lecture hi welcome back in this lecture let's go and let's explore more about spring boot starter data gpa dependency well let's first understand what is the problem uh the developer we are facing and what solution this starter dependency offers well consider we are developing a springboard application without using you know this starter dependency then what basically we need to you know do is we need to add a lot of dependencies for example we need to add jpeg dependencies hibernate dependencies spring data jpeg dependencies along with that we need to add a couple of spring you know dependencies as well right and as a developer we need to also manage the compatible versions of all these dependencies okay that is where the problem you know developer you know need to manage all these things developer need to add all the required libraries to develop the persistence layer of the repository layer and developer need to manage all the compatible versions of all these libraries right so that is where you know this starter dependency were introduced so spring boot team basically created this starter dependency and this starter dependency internally manage all these libraries and its compatible versions we don't have to add a lot of libraries like gpa dependencies or hibernate or spring data gpa or you know spring based dependencies and yeah so we no need to add all these libraries we need to manage all the compatible versions of these libraries this starter dependency will take care of you know adding all the dependencies internally and managing the all compatible versions right so that is awesome right we just need to add one starter dependency and it will include all the i know required libraries are the dependencies to develop the persistence layer or the repository layer right so this is basically one stop shop for all the libraries to develop the repository layer or the positions layer okay now what we're gonna do is we're gonna deep dive into this dependency and we'll see what this dependency internally provides okay let me go ahead and let me deep dive into this dependency and you can able to see here this starter dependence internally added spring data jpa dependency okay and it has already managed the version of this dependency and along with that this starter dependency also added spring framework dependencies spring aspects okay and you can see here this starter dependency also added spring aop dependency for managing the transactions and this starter dependency also provides spring boot starter jdbc dependency internally and look at here couple of jpeg dependencies okay jakarta positions api well basically the name has changed from java positions api to jakarta positions api and you can see here hibernate dependency all right so this starter dependency will basically include all these required dependencies to develop the persistence layer or the dowel here so this is awesome right we don't have to add all these required dependencies and manage its compatible versions so this single starter dependency will take care of adding all the libraries internally and manage the compatible versions okay so whenever you want to develop the perceptions layer or the repository layer for any relational databases then you can go ahead and use spring boot starter data jp dependency okay and whenever you add this starter dependency make sure that you also need to add the jdbc driver for example in our case mysql connector java make sure that you add jdbc driver dependency whenever you use spring boot starter data jpeg dependency in your spring boot application okay great so this is a quick overview of spring boot starter data jp dependency in next lecture we will configure mysql database in our springboard application so that we can able to connect our springboot application with mysql server all right i will see you in the next lecture hi welcome back in previous lecture we had a quick overview of spring boot starter data jp dependency in this lecture we are going to connect our supreme boot application with the mysql database well in order to connect our spring boot application to the mysql database we need to configure few of the properties for example we need to configure jdbc url mysql database username and password and couple of hybernet properties well let's go and let's open application.properties file in our springboard application okay and before configuring my sequel details in our springboot application first we have to create a database well in order to create a database we are going to use mysql workbench well let me go to the mysql workbench and here just type the sql statement create database and we are going to give a database name something like e-commerce okay and go ahead and execute this sql statement all right and go ahead and just refresh the schemas and you can able to see e-commerce database is created okay now we are good to configure this database in our springboard application well let's go to integer idea and open application.properties file and here just type the property spring dot data source dot url so let's go and let's give the jdbc url here gdbc colon mysql so this is a gdbc url to connect to the mysql database followed by localhost so we are going to connect our spring boot application with the local mysql database that's the reason localhost followed by port 3306 is the default port of mysql server followed by database name in our case e-commerce and we are not going to use ssl connection so let's use use ssl equal to false to just disable the ssl connection similarly let's go and let's configure username and password spring dot data source dot username in my case mysql server username is root and similarly let's configure password dot password equal to mysql at the rate 123 so make sure that you have to change username and password as per the mysql installation in your machine alright now we have configured jdbc url username and password and one more very important point here is we don't have to add driver class name here because springboot will automatically detect the driver class name as per the driver dependencies in a form.xml for example in our case you can see here we have added mysql connector java driver dependency right spring boot will automatically add a driver class name by looking into this dependency in the form.xml all right we don't have to add a driver you know class name here okay now what we're gonna do is we're gonna add hibernate dialet all right so let's go let's type the property spring dot gpa dot properties and then dot abernate dot dial it equal to this is the mysql innodb dial it all right so make sure that whatever the database you are using for example in our case we are using mysql database right and we have to add mysql hibernate dialect like this okay so basically hibernate will create the sqls based on the dilate that we add in our springboot application in our case we are adding mysql you know hibernate direct right so hybernet will basically create a sql queries with respect to the mysql database let's say if you are using other databases like postgresql database then you have to add postcode sql hibernate dilate so hybernet will basically use the postgresql dialet to create the sql queries based on the database we've entered okay so in our case we are using mysql database vendor so hybernet will generate sqls behind the scene with respect to the mysql database vendor now let's go and let's add one more hybrid property that is auto ddl property just type the property here spring dot jpi dot hibernate dot auto ddl and here basically we have a couple of options for you know creating the database schema generation well hibernate basically provides a lot of options over here for example you can see none create only drop create drop validate update so these are the possible options for you know for this property all right so basically hibernate provides these these values or the property options to customize the hibernate data schema generation process well let me explain you all these possible options so that you will understand better let's say if you use none here none value for this property then this none option basically disable the auto ddl tool which means that hibernate is not going to take any action for managing the underlying database schemas okay hibernate is not going to create a database tables for you you have to manually execute all the database tables manually in the mysql database okay so this will basically disable the hibernate auto ddl tool okay tool meaning hibernate will basically provides a tool to generate the database tables behind the scene for us right so this option will basically disable that tool all right and second is create only let's say if you use create only value then this option basically tells hibernate that hibernate have to you know generate database schema from the entity model well whatever the jp entity models that we are going to create in our spring boot application this option tells hibernate that create all the tables for all the jpa models that we create in the springboard application okay next comes drop drop well basically drop option tells hibernate to drop the database schemas using entitymodel as a reference for the ddl drop statements well this option tells us about it all the you know tables by just referring the jp entities are the models that we create in our spring good application okay so this will basically instruct hibernate that just drop all the tables by referring the jpa models that we created in our springboot application pretty simple well the next option is create okay create well this create option basically tells hibernate that drop the existing database schema and recreate all the tables by referring the jpi modules that we create in a springboard application okay so this option basically tells hibernate to recreate all the you know database tables by referring the jpi models that we create in our springboot application well next is create hype and drop so this option or value basically tells hibernate that drop the existing database schema and recreate all the database schema tables and once the entity manage factory or the hibernate session factory close then again drop all the database tables okay so this will tell hibernate that drop the existing database schema and recreate all the you know database schema tables and once the entity manager factory or the hibernate session factory closed then again drop all the you know existing database schema tables all right well the next is validate well validate option tells hibernate that just validate the underlying database schema against the entity mappings so this value just tells hibernate that just validate the underlying database with the entity mappings that we have done in the springboot application well next value is update well update option tells hibernate that basically you know update the existing schemas with whatever the changes we have done in the jp entities all right in this course you know most of the time we are going to use update value because it won't drop the existing tables it simply alter the table whatever the changes we do in a jp entities okay so this is pretty useful property while development so we are going to use update as a value for this property in this course okay i hope you understood all these possible options for this auto ddl hibernate properties now what i'm going to do is we're going to add a couple of more hibernate properties to just see the sql statements that hibernate generate behind the scene and also the sql statement should be well formatted for that i am going to add a couple of properties here for example spring.jpg.showsql equal to true so this will basically show all the sql statement that hibernate create behind the scene and spring.jpi.properties.hibernate.permit underscoresql equal to true so all the sql statement that hibernate create behind the scenes should be well formatted okay great well that is pretty much it we have compared all the mysql configuration in our springboard application now let's go and let's verify whether our springboot application will connect with the mysql database that is e-commerce database or not well here we basically made a small typo so this should be ddl hyphen ato so make sure that the property name should be you know correct now what you want to do is we're going to run our spring boot application and we will see whether our spring boot application will able to connect to the mysql database or not in order to do that let's go and let's run our spring boot project we'll go to the springboot main entry point class here and let's run the springboot project and there we go there are no exceptions no errors in the console it means that our springboot application is successfully connected with the mysql database and you can able to see the locks in the console like hibernate is using mysql for you in node eb dial it and by default spring boot uses hikari you know as a connection pool you can able to see that hikari data source okay spring boot auto configure this connection full by default well let me recap what we have done in this lecture we have configured mysql details in application dot properties file and we have gone through all these hibernate ddl auto property values and we ran our supreme boot application and we can able to see that our springboot application has successfully connected with the mysql database well in next lecture we are going to see how we can create a jp entity and how we can map our jp entity to the relational database table alright great i will see you in next lecture hi welcome back in previous lecture we have seen how to connect our springboot application to the mysql database well in this lecture we are going to create a jp entity and we will see how to map jp entity to the relational database table alright so first of all we are going to create a packaging structure for our spring boot application so head over to the main package right click new and then choose package and let's give package name as entity well dependent different developers you know prefer using different different naming conventions for example here we are using entity but some of the developers may use a model package to keep all the jp entities or some of the you know developers may use a domain package to keep all the jp entities well in this course we are going to use entity package okay great now let's go let's create one more package and let's call it as repository well within our repository package we are going to keep all the spring data jp repositories perfect now let's go and let's see how to create a jpa entity well we are going to basically create a product jp entity okay so go to the entity package right click on it new and then choose java class let's go and let's give class name as product hit enter now i want to define the private instance variables well let's say private and then long id private string skew so this is basically a stock stock keeping unit and this is a short form for stock keeping unit okay and then private string product name all right and private string product description let's call it as a description perfect and private and let's have a price for the product we are going to use big decimal okay big decimal and let's say price and then private and let's use a boolean whether a product is active or not that is active field and then let's go and let's use one more field that is let's say image url and let's go and let's create couple of more fields like date created okay and then one more field updated date or we can say last updated okay perfect so to keep it simple these are the you know fields for product class so go ahead and create a get a setting methods to access these private fields we'll right click inside this class and then choose generate and then choose getter and setter and select all these fields and click on ok now we have created getter setter methods to access these private fields okay and if you can closely observe this class this is the pojo class basically pojo meaning plain old java object now let's go and let's make this java class or a puzzle class as a jp entity well in order to make this pojo class as a jp entity we have to use jpa annotations well there is one annotation that is at entity and notation and this add entity annotation from java x dot persistence package all right so we basically use this annotation to specify this class as a jpa entity well you can read the description like here okay so this entity annotation specifies that the class is a entity and this annotation is applied to the entity class well whenever you want to make a pojo java class as a jp entity then you have to use this at entity annotation and make sure that you use identity annotation at a class level you cannot use this identity annotation at a field level now we have made this product class as a jp entity well in order to map this product jpnt to the relational database table we have to also create a primary key so in order to create a primary code for this product shape entity we are going to use add id annotation and make sure that add id annotation from java x dot positions package now we have created a product jp entity by using these api annotations well let's go and let's run our spring boot application and let's see how hibernate will automatically create a table for this jpeg entity all right and if you can open application dot properties file over here you can see this property well this is the hibernate property which is responsible to automatically create a table for the jp entity all right let's go and let's run this premium application and let's explore more about how hibernate will create a tables for jp entity well look at here in a console you can able to see hibernate generated sql ddl script to create a product table and by default hibernate you know provide a table name as a product for example if you can see the jp entity so hibernate will by default provide a table name as a product okay and we have to customize the table name right so in next lecture we will see how to use jp annotations to customize this table structure okay and again you can see hibernate uses the default column naming conventions like this for example you can see here date created so this is the field right and you can see the column name date underscore created okay so this is how the column name by default hibernate will create for this this kind of field names okay date created so whenever the field has two words like date created then hibernate will provide underscore between these two words okay so this name this name for column makes sense right that's why ubernet uses this naming convention to provide a column name for this kind of fields now let's go to database and let's see whether table is created or not let me refresh the schemas and you can able to see under tables you can see product table is created okay and you can see all the columns all right so this is how hibernate will create a sql statement behind the scene to create a product table for product jp entity and this is how hibernate uses naming conventions for table and columns okay and again we need to customize this table a lot using jpi annotations for instance if we want to give a different name to the table okay then jpa provide one more annotation that is add table annotation we can use to customize the table name and let's say if you want to give different name to the column well jpa again provides add column annotation to customize the column you know column name mine column attributes okay and let's say if you want to make one of the column as a unique unique constraint then you can go ahead and use at uni constraint annotations okay in next lecture we'll see how to use all these jp annotations to customize this table structure okay great i will see you in next lecture hi welcome back in previous lecture we have created a product jp entity and we have seen how hybrid net will create a ddl sql script behind the scene to create product database table and look at here hibernate basically uses you know depart naming convention to provide a database table name and you know default column names to the fields right for example look at here this is the hibernate generated sql ddl script to create the product table in the database and hibernate by default provided a table name as a product and if you can see the jp entity that is product jp entity we haven't provided a table name to this product jp entity right by default hibernate provided you know table name to this product jp entity as a product it will just you know lower case the post character okay and hybrid provided these default naming conventions for columns for the for these fields right and we need to customize this table a lot for instance we need to give a proper name to the table and we also need to provide a unique constraint to these columns for example let's say if we want to provide a unique constraint to this stock keeping unit column then we need to you know use the jb annotations and let's say if we want to provide primary key generation strategy like we want to increment the id or we need to provide a sequence to you know create a primary key right and we need to provide a size for the columns right so in order to you know customize the table uh details or table structure we have to use jp annotations all right so in this lecture let's see how to use gpa annotations to customize the table structure so in order to configure you know table details for this product jp entity we can use add table annotation from java x dot positions package so now we can give a name to the table for example add table annotation has a different attributes so let's one let's use name attribute and let's give name to the table for instance different different developers prefer giving different different naming convention for the tables right let's say some of the developer give a table naming conventions like this table underscore products okay or some of the developers prefer you know using a plural form of the entity like products okay so to keep it simple i will give products as a table name so along with a table name you can also provide a schema here itself for example i'm going to use schema attribute and then i can define schema for example our schema is e-commerce right schema or database so let me copy this and i can simply paste it here so in this way you can also configure schema or database for this product jb entity all right great now let me quickly format this perfect now let's go and let's see how to define a unique constraint for a columns all right so let's say in product jp entity we are going to make this stock keeping unit column as a unique constraint so in order to define any constraint for this column we're gonna use unique constraints attribute of this annotation so you can see add table annotation has attribute called unique constraints okay so go ahead and use this unique constraint over here and within a unique constraints we can define a multiple unique constraints for multiple columns okay so now i'm going to use add unit constant annotation to define any constraint for stop keeping unit column okay so this annotation has two attributes name and column names so let's go ahead and let's use name field to provide a name to the unique constraint so let's say stock keeping unit unique okay and also provide a column name to which this unique constraints applies so in our case the column name is skew okay stock keeping unit all right perfect so this should be unique right perfect so this is how we basically make a column as a unique constraint by using add table annotation okay perfect if you want to make you know other columns as a unique constraint then you can go ahead and use at any constraint annotation like this okay just use name here and let's say if we want to give a unique constraint for product name so we can go ahead and say name unique and also define the column name okay column name is name like this okay so to keep it simple i am going to define unique constraint for stop keeping unit column okay perfect so this is how we define unit constraint for columns in a jpeg entity all right now i'm going to show you how we can customize the column for the fields let's say we want to change a column name i mean we want to give a different name to the columns for instance let's say i want to give column name for this stock keeping unit like this for instance i'm going to use add column annotation and name and then let's say i want to give different column name something like stock keeping underscore unit alright so in order to customize the column name and column details we can use add column and quotation all right so add column annotation specifies the mapped column for the persistent property order field if no column annotation is specified the default values applied and by default we have seen like hibernate provides a default name to the columns as a field name okay now we have changed a column name for this stock camping unit so i can copy this and i can give column name here okay great now we can also define this column as a not null for that we have property nullable equal to false okay and if you want to make a product name column as a not null then simply you can use add column annotation and you can simply pass null label equal to false so this will basically make this column as a not null okay perfect so this is how we basically customize a column for the field all right great well you can also mention the size or length for the column for instance add column annotation has a attribute called length you can use a length attribute to define the size by default it is 255 all right so let's keep default as it is now let's go and let's see how we can use a primary key generation strategies to create a primary key for the you know row in the table well jpa or hibernate basically provides four types of primary key generation strategies well in next lecture we will go through these four types of primary key generation strategies but in this lecture i am going to use identity as a primary key generation strategy well in order to define a primary key generation strategy we have to use add generated value annotation and make sure that you choose add generated value annotation from java x dot persistence package and this add generator value annotation has attribute called strategy okay go ahead and use strategy attribute and then simply type generation type and identity well basically jpa provides four types of primary key generation strategy ato identity table and sequence in next lecture we will see these four primary key generation strategy as of now let's use identity well identity will basically know how to increment the table column in the database now we have pretty much customized the product jp entity all right now let's go and let's run our spring boot application and let's see how hibernate will create a table for this jp entity well i am going to run spring boot application now okay and let's see how habernet will generate sql ddl script behind the scene for this products table and you can see here hibernate generated ddl script and let me compare with product jp entity well you can see table name products and we have configure table name as a product and next you can see primary key generation strategy we have provided identity and this should be uh to increment you can see in a detailed script auto increment and you can see we have customized the column for stock keeping unit and you can able to see the column image change from skew to stock keeping unit and you can see here not null because we have provided none label equal to false right and again you can see we have provided any constant uh for you know stop keeping in it right that we can see a little bit below yeah here you can see hibernate basically generated the alter sql statement alter table products add a constant stop keeping unit okay and this is the basically a unique constraint name that we have provided over here all right great so this is how basically we use jp annotations to customize the table structure okay as per our requirement we can provide a table name column name and we can provide a size for the column we can define the unique constraint okay we can define the primary key generation strategy all right great now let's head over to the database and let's verify whether this table is created or not i'm going to head over to the mysql workbench and i'm going to refresh the schemats and you can see products table is created okay and you can see all the columns and stock keeping in it so this column has changed right and this product table it was already you know created in one of the previous lecture so i am going to drop this table perfect now we have products table in our database all right great well in next lecture we will see four types of gpa provided primary key generation strategies all right so this is very important so let's discuss in the next lecture all right i'll see you in next lecture hi welcome back in previous lecture we have seen how to use jb annotations to customize the table structure right in this lecture we'll see four types of jpa provided primary key generation strategies well basically jps specification supports four different primary 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 hybernet will basically you know use or select the primary generation strategy based on the database specific direct 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 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 atom okay great now i'm going to run the spring boot application and we'll see how happen 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 ibun 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 hibernate 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 generated 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 integer 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 springboard 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 e 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 key 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 annotations 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 will see how sequence primary key generation strategy works right now what i'm going to do is i'm going to 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 you 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 go and 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 habit 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 products and 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 key 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 am going to simply you know [Music] remove these lines of code and also i am 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 key generation strategy but it uses different mechanism that's why habenet 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 specification 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 all right i will see you in next lecture hi welcome back in this lecture we will see how to use hibernate provided time stamp annotations to automatically populate the values for dates so you can see here in our product jp entity we have a date created and last updated fields right and each time we create a product object we have to specify the values for these fields right well tracking the date created and last updated database record is a common requirement right so instead of manually specifying the values for these fields we can use hibernate provided time step and timestamp annotation to automatically assign values for these fields all right so let me show you how we can do that so let's go and let's use the habit provided annotation that is at you know creation timestamp annotation so make sure that you choose creation timestamp annotation from hibernate package okay that is org dot hibernate dot annotations so this annotation will basically automatically populate the value for this build okay so this annotation will basically get the current time from the jvm and then it will assign to this field all right next let's go and let's use add update you know timestamp annotation from our dot hibernate dot annotations package to automatically provide a you know timestamp value for this field well once we add hibernate provided timestamp annotations to these fields in a jp entity then hybrid will basically take care of the necessary updates we don't have to manually add or you know track the values for these fields hibernate will automatically take care of updating these values for us okay so these are the useful annotations we can simply use to just you know let hibernate to manage the values for these fields all right great now let's quickly run our spring boot application and let's verify these changes will work as expected so let me run this premium application and you can see there are no exceptions or the errors it means whatever the changes we have done it is working as expected all right great i will see you in next lecture hi welcome back in this lecture we will see how to use lambok library to reduce a boilerplate code well lombok library is basically a popular java library that helps us to reduce a boilerplate code here boilerplate code meaning get a setter methods constructor tostring method equals method okay for example consider a product jp entity here and we have created a lot of data setter methods right and look at here the lines of code okay we have created these lines of code to just generate a getter setter methods all right and let's say if we have you know too many jp entities for example in our e-commerce project we can create a product jp entity product category order order item okay user roles address right so if we create these jp entities then obviously we need to create a get a set of methods to access the private fields right and sometime we need to create a constructors we need to create you know equals and hash code methods to string methods right so this is basically a boilerplate code we need to create this kind of code repeatedly right in order to reduce this kind of boilerplate code we can use lombok library well basically lombok is a java library which will help us to you know reduce this kind of boilerplate code and you can see it provides a lot of annotations okay for example look at here at getter add setter annotation so we can use these annotations to automatically generate a getters and setters for the private fields and add to string annotation we can use to generate a tostring method for the class and equal sign hash code and these are the couple of annotations to generate no argument constructor required argument constructor and all argument constructed and if you if you want to have a better pattern for your class you can use add builder annotation all right so let's go ahead and let's see how to use lombok library in our spring boot application and let's see how to reduce the boilerplate code well first of all let's get the lombok dependency so in order to get the lombok dependency from the internet so i am going to head over to the start.spring.io so this will head over to the spring initializer and go to add dependencies and here just search for lombok okay and just choose the lombok dependency so this is basically java annotation library which helps to reduce a boilerplate code and go ahead and click on explore and you can simply copy this long book dependency okay and let's head over to the english idea and go to form.xml and just go here and just paste it so as soon as you paste this dependency you will get icon here okay so go ahead and click on this icon to load my changes all right now we have added lombok library to our supreme boot application and we are good to use lombok annotations so go back to product jp entity and here we are going to simply remove getters and setters all right and just go ahead and annotate this class with add getter annotation and add setter annotation and if you want to use no argument constructor or all argument constructor then you can go ahead and use annotations like no argument constructor meaning a default constructor and all argument constructor meaning a parameterized constructor okay great now we have used a lumbar connotation to reduce the boilerplate code so going forward whenever we create a jp entities will be using lombok annotation to reduce a boiler blade code for example get a set of methods constructors two string methods okay well now let's go and let's run our springboard application and let's see these changes will work as expected so i'm going to run the spring application and look at here there are no errors or exceptions in the console it means that we have successfully used a lombok library in our springboard application all right great i will see you in next lecture hi welcome back in this lecture we'll take a look into important repository interfaces in its relationships and its implementations all right so before looking into repository interfaces let's first take a look into spring data framework well spring data framework basically contains a lot of modules for instance spring data commons spring data ldaps bring it among db spring data jps spring data jdbc and there are a lot of other spring data modules and if you look at the hierarchy over here spring data commons is the parent project and the other projects like spring data ldaps bring that among db spring data jps ping data jdbc so these models basically extends spring data commons alright so spring data commons is basically contains all the common interfaces and classes and the other modules like spring data ldaps bring data mongodb spring data gpa spring data gdbc so these modules basically extends spring data commons you know interfaces okay so just remember spring data commons is the separate project and it is basically a parent project it contains all the common interfaces and the other you know models basically extends spring data commons interfaces okay and these spring data projects have their own purposes for instance spring data ldap it basically supports ldap implementation spring data mongodb it supports mongodb based you know dow layer spring data jpeg basically supports gpa related downloaders and spring data gdbc basically supports gdbc related dowel layer right so each project have their own purpose and in this course we'll be more focusing on spring data jpa it basically support for gpa based data access layers well once we understood spring data framework modules and its hierarchy let's go back to our repository interfaces class diagram over here and if you look at the class diagram repository interface is basically a root interface and crude repository interface extends repository interface paging and sorting repository interface extends crew repository interface and query dsl predicate executor so this interface is is basically independent of these interfaces all right and these four interfaces are belongs to spring data commons model and you can see here jp repository interface and jps specification executed interface so these two interfaces are belongs to spring data jpa module and as we know that spring data jpa module it extends spring data commons model so if you can see the interfaces jpeg repository interface from spring data jpi module extends paging and sorting repository interface of spring data commons module all right this is how the repository interfaces hierarchy looks like jpeg repository interface internally extends paging and sorting repository interface paging and sorting repository interface internally extends screw repository interface and crew repository interface internal extends repository interface all right so this is how the repository interfaces relationships you know looks like well in this course we'll be more focusing on jpeg repository interface so let's go ahead and let's learn more about jpeg repository interface well jpi repository interface you can see here it extends paging and sorting repository interface and paging and sorting repository it internally extends good repository interface and crude positive interface internally extends the repository interface and crew deposit interface basically provides accrued methods for example save save all find by d find all delete id delete alright so these are the crude methods that crude interface basically exposes and paging and sorting repository interface it provides support for paging and sorting alright so basically these two methods support sorting and pagination apart from that jpeg repository interface also exposes few of the methods which supports batch operations and other operations for example find by id find all with sort as a method argument flush save and flash delete in batch delete all in batch okay save all so these are the few methods that jpeg repository interface also exposes well in order to leverage all these methods we simply create our own interface for example product repository interface and extends jpeg repository interface and we need to simply pass entity name and the primary key type that's it once we do this we'll be able to leverage all these methods right this is awesome right we just have to create one interface and it extends jp repository interface and just pass entity name on the primary key type that's it will be able to leverage all these methods we don't have to write any code i mean we don't have to write you know implementation class to implement this interface and a repository interface and its methods okay spring data gpa internally provides implementation for all these methods we don't have to write any code we just simply need to create the interface and extends jp repository interface that's it will be able to get all these methods for ndt in this case product is the jp entity all right now you may have a question like so far we have seen the repository interfaces and its methods and where's the implementation who will be able to implement all these repository interfaces and its methods right so basically spring data jp is responsible to implement all these methods basically spring data jpe provides simple jpeg repository class it implements all these important methods okay for example look at here jp repository implementation well spring data gpa internally provides simple jpeg repository class which implements all the methods of jp repository interface okay just remember spring data jp provides simple jpeg repository class it is basically a default implementation class of jpeg repository interface and simple jpeg repository class it provides a transaction management for all its implementation methods right it means that all the methods of jp repository interfaces are by default transactional we don't have to make these methods again transactional for example you can see simple jp repository class it is annotated with add transactional annotation it means that all the methods in a simple jp repository class are by default transactional we don't have to again annotate these methods with a transactional annotation all right well in next lecture we'll deep dive into spring data jp library and you will understand these repository interfaces and its implementation classes all right i will see you in next lecture hi welcome back in previous lecture we have gone through some of the important repository interfaces its relationships and its implementation classes at a higher level in this lecture we will deep dive into spring data jp library and you will understand more about these repository interfaces and its implementation classes all right let's head over to the integer idea and let's deep dive into spring data jp library well basically we are going to decompile spring data jpa library that is a jar file and we will see its interfaces and implementation classes well in order to see the spring data gpa jar file linux dot class files what we can do is we can go to the external library section over here just expand it and go ahead and just search for spring data jpl library for instance you can see here yeah spring data jpa library so go ahead and just expand it and then you can able to see spring data jpa jar file right and just go ahead and dive into this package and just go ahead and click on a repository package over here and here you can able to see jpeg repository interface all right so this is basically interface just go ahead and double click on it and you can able to see jpeg repository interface and it's all methods all right so basically this is a dot class file all right so we have deep dive into jar file and we are looking into its interfaces and classes so you can see jp repository is the interface and it internally extends paging and sorting repository interface so just you know press ctrl line click on this and you can able to deep dive into paging and sorting repository interface and paging and sorting repository interface exposes these two methods to support sorting and pagination and passing a sorting repository interface it internally again extends good repository interface and just press ctrl and click on the screw repository interface you can able to you know go into this screen repository interface and you can able to see queue repository interface exposes all these methods okay so mainly we'll use save save all find by d okay and delete by id delete delete all so these are the very important crude methods that crew repository interface exposes and you can able to see crew deposit interface in turn extends a repository interface just press ctrl and click on this repository interface and you can able to see repository interface it does not contain any methods it means that repository interface is basically a marker interface okay and in repository interfaces hierarchy repository interface is basically a root interface all right so let's recap the repository interfaces hierarchy again so we have a gpa repository interface it in turn extends paging and sorting repository interface it in turn extends code repository interface it in turn extends repository interface so this is how the repository interfaces hierarchy looks like all right now let's go and let's see the implementation class of jp repository interface well just go ahead and expand this support package and just scroll down a little bit and you can able to see here simple jpeg repository class so just go ahead and double click on it and you can able to see simple jpeg repository is the class it internally implements jpi repository interface and its methods okay so simple jpi repository is the default implementation class which provides implementation for all the jpeg repository interface methods okay simple jpi repository class implements jpi repository implementation interface and jpa repository implementation interface internally extends jp repository interface it means that simple jpeg repository class it indirectly implements all the methods of jpeg repository interface for example jpeg repository interface has all these methods for instance find all find all by id save all flush save and flow save all line flush delete in batch delete all in batch okay and find all so all these methods basically you know simple jpeg repository class implements for for example we can just scroll down and you can able to see delete by id method implementation and delete method implementation okay delete all by d delete all by d in batch delete all okay delete all in batch all right so basically simple jpeg repository class it implements all the methods of jpeg repository interface well if you can closely observe this implementation class this class is annotated with add transactional annotation it means that simple jpeg repository class it already provide a transaction support for all its methods so we don't have to again provide a transaction support for these methods for example delete by id method so this method is annotated with ad transactional annotation it means that this method provides support for transaction management and similarly delete a method delete all by id method okay so all these public methods are by default transactional we don't have to again provide a transaction annotation to make these methods transactional okay so this is a very important just remember simple jp repository is the default implementation class which provides implementation of all jpeg repository interface methods okay and second important point is simple gpa repository class it by default provides a transaction support for all its methods okay and we don't have to again make its methods transactional all right so these are the few points you have to keep in mind whenever you use jpeg repository interface in your you know project all right i hope you pretty much understood the repository interfaces and its implementation in a spring data jpa model well let me recap what we have done in this lecture we have basically decompiled spring data jp jr file and we understood the important interfaces like gpa repository interface and its methods jpeg repository interface internally extends paging and sorting interface paging and sorting a repository interface it internally extends screw repository interface core repository interface it internally extends a repository interface all right we have seen all this hierarchy and we have also gone through its implementation class that is simple jpa repository class alright and by default all the methods in a simple jpeg repository class are by default transactional all right great i will see you in next lecture hi welcome back in this lecture let's take a look into steps to create and use spring data jp repository well in order to create spring data repository in our springboot project we typically follow these steps well first we create a repository interface which extends jpeg repository interface next we basically add custom query methods or finder methods to the created repository interface well basically this is the optional step if we want we can add a query methods or finder methods to the repository interface well third step is inject the created repository interface to another component and use the implementation that is provided automatically by spring data jpa let's go through these steps in detail well in order to create a repository interface we basically create an interface for example product repository which extends jp repository interface and jpeg repository interface basically takes two arguments first is the jp entity name and second is the primary key type well consider we have product jp entity in our springboot application and we want to create spring data gpa repository for product jp entity well we basically create a interface like this product repository which extends jpeg repository and we pass jp entity name and the primary key type well primary type here is for example look at here product jp entity has id of type integer so this is the integer basically we pass as a second argument to the jpi repository okay so jpeg repository basically is a generic so we have to pass a wrapper class as an argument all right this is how we basically create a repository interface and get all the crude methods that is provided by spring data jpa well we can also add custom query methods or finder methods to the repository interface for example find by name is the query method or the finder method well we'll learn more about query method or a finder method in the upcoming lectures so this is basically an optional step if you want you can create a query method or finder method as per our requirement well next is how to use this created repository interface in our project well we simply inject this repository interface for example look at here product repository we are using at advaita notation to inject product repository and we are calling repository methods for example save method save all find all find by id all right so simply we use atta to add annotation to auto wire you know repository interface and we call its methods okay so you can inject a repository interface in a service layer components or controller layer components or junit classes now let's go and let's create a product repository in our springboard application well let's head over to the integer idea and let's go to repository package over here right click new and then choose java class and choose interface and just go ahead and give interface name as product repository now let's make sure that this should be our interface and next we need to extend this interface with jpa repository interface and look at your jpeg repository interface takes two arguments first is the type of the entity second is the type of the primary key and this jpeg repository interface should be from this package that is org spring framework.data.jpg.repository package and just pass post argument as you know type of the entity in our case product jp entity and second long as you know primary key data type for instance if you can go ahead and open product jp entity you can able to see id has of type long right so this is long and this long we are simply passing to the jp repository as a second argument all right now one more important point here is we don't have to annotate this repository interface with at repository annotation here because as we know that simple jpeg repository is the implementation class of jpeg repository interface right so let me quickly open simple jpeg repository class over here so this simple jpeg repository class is from spring data gpa library and simple jpeg repository class it internally you know annotated with add repository annotation that's the reason we don't have to again annotate this interface with at repository okay just remember this point all right perfect now we have created product repository interface which extends jp repository interface and simple jpeg repository class which implements jpeg repository interface and its methods so we don't have to write any implementation class to implement this interface once this product repository interface extends jpeg repository interface then this product repository interface gets all the methods to perform code operations on product jp ntt alright great well let me recap what we have done in this lecture well we understood the steps that we need to you know use to create and use spring data gp repositories and we have also created product repository in our springboard application in next lecture onwards we'll deep dive into understanding all the important methods of repository interfaces all right i will see you in next lecture hi welcome back in this section of the lectures we'll take a look into important methods of these repository interfaces so mainly we'll focus on the important methods of crude repository interface because you can see these are the methods we frequently use to perform different operations like crude operations okay we frequently use save method to save or update an entity we use save all method to save multiple entities at once and we use find id method to retrieve or find identity by d and we use exist by id method to check whether a entity is exist with a given id or not and we use find all method to find or retrieve all the entities from the database table and we use count method to count the records in the database table and we use delete by id method to delete an entity by id and we use delete method to delete a given entity and we use delete all method to delete all the entities or records from the database table so these are the frequently used method so we are going to focus on all these methods in this section of the lectures and you can see here our product repository interface extends jpeg repository interface jpeg repository interface internally extends paging and sorting repository interface paging and sorting repository interface internally extends code repository interface right so once our product repository extends the jpi repository interface then jpi repository indirectly extends code repository interface right hence this product repository interface leverages all these methods from all these repository interfaces because these repository interfaces are extends each other isn't it now let's quickly take a look into what is the relationship between repository interface and entity manager interface from the jpa api well basically spring data gpa is abstraction layer on top of java persistence api right and entity manager is the interface of java persistence api well basically spring data jpa has a jpeg repository interface it internally uses all the jpa provided methods for example this is the jpi repository interface okay it has a methods like flush save and flush save delete okay exist count find all so these are all the methods belongs to jpeg repository interface and all these methods internally uses entity manager api to perform different operations with the database okay so this jpeg repository interface methods don't directly connect to the database these repository methods will first internally get call to the entity manager methods and these entity manager methods will talk with the database okay so this repository is basically is a abstraction on top of jpa interfaces or the apis all right i hope you understood how the jpi repository methods will internally call entitymanager methods to talk with the database okay this is how spring data jpa basically works all the spring data jp repositories internally uses entity manager to talk with the database okay so spring data gpa is just abstraction on top of jpa that is java positions api in next lecture onwards we'll take a look into some of the important and frequently used methods of these different repository interfaces alright i will see you in the next lecture hi welcome back in this lecture we will take a look into how to save an entity into the database table well this is one of the common requirement in real time project to save an entity into the database table right well in this lecture we'll see how to use save method from crew repository interface to save an entity into the database table all right so same method as name suggest this allows us to save an entity into the database table alright so basically we can use save method to perform both operations save and update if we are trying to store new entity then save method will internally call entity manager you know persist method to save a new you know entity into the database table if we try to update a existing entity into the database then save method will internally call merge method to merge the updated information into the database table alright so in this lecture we will see how to use save method to perform a save operation that is we are going to save an entity into the database table in next lecture we will see how to use save method to perform update operation all right let's go and let's head over to the included idea and let's see how to use save method to save an entity into the database table well basically you have two options either you can write the junit test to execute a piece of code or you can simply you know implement command line runner interface or application runner interface to run a piece of code for example so this is the main entry point class of our springboot application you can simply implement a command line runner interface it provides a run method right just go ahead and implement it and you can write the code here okay and if you run this springboard many to point class then this code will be executed well this is the first approach we can use to execute the p sub code as a standalone second approach we can use a jna test to test a piece of code okay so basically we are going to write the jne test to execute a piece of code all right so just let me do this okay and now go to the product repository interface select it and right click generate and then select test and by default increasing idea will provide a class name as product repository test so let's keep as it is and go ahead and click on ok now you can able to see a repository package will be created under test folder and within a repository package you can see product repository test class has created now let's go ahead and let's annotate this class with add spring boot test annotation well basically spring boot provides add spring boot test annotation to load a full application context of your springboard application well here we want to load a full application context because we want to inject some of the spring bins in this class okay so go ahead and choose spring boot test annotation to load all the spring bins from our spring boot application now let's go and let's see how to use a product repository and its methods in this class well in order to use a product repository we have to auto wire it right so let's go and let's use add auto add annotation to inject product repository interface so here we are basically doing a field based dependency injection so let me simply create a product repository instance here now we have injected our auto wired product repository interface now we are good to call its methods right and product repository interface internally extends jpi repository interface and jperepository interface internally extends paging and sorting repository interface and paging and sorting repository internally extends scroll repository interface and we are going to basically look into this save method to save an entity into the database table right well notice here we are not going to write a real junior test cases to test a repository layer components here here we are basically writing a jna test case to you know execute a piece of code right so basically we use add data jpeg test annotation to test the repository layer components but here we are basically using add spring boot test annotation to load a full application context so that we can able to inject any cup spring bean in this class and we can simply use it all right so don't worry about how to test the repository layer components i will create one dedicated section of the lectures to show you how we can test repository layer components using add data jpa annotation and also will follow bdd style naming convention to write the jne test cases okay and here we are basically write the jna test case to execute the p sub code all right so i am going to create one jna test case here i will call it as save method something like this and let's annotate this method with add test annotation okay so this is the jna test case and within this dna test case we are going to write the logic so basically we need to first create a product object okay and then we will save that product object into the database table and then we'll display the product information product information okay so let's go and let post create the product object product and then product new product and let's go ahead and let's use setter methods to set the values to the product object let's go and let's give a product name something like product one so just give dummy data and let's go and let's give description as product one description all right and let's go and get you a product stock keeping unit something like let's say 100 abc something like that all right and let's go and let's give a price to the product so this should be a big decimal so i'm going to call big decimal instance and just pass 100 and product dot set active let's say true and next product dot set image url so let's give any image url here let's say product1 dot png something like that and product dot set what else remaining okay so we don't have to set the last updated and you know date created fields because we are using hibernate provided time stamp annotations to automatically you know fill the values for these fields now let's go and let's save this product object into the database well this is a save method right and let's go and let's save this product object into the database let's call product repository and call its method that is save method and look at here save method expect entity you know as a method argument and go ahead and call this your method and just pass product as identity perfect and look at your save method returns the saved entity in this case the type is product so let's go ahead and let's take the result of this save method in a product object let's say saved object something like that okay now once we save a product object into the database then we simply display this product information okay i'm going to simply call this this out over here saved object and just call get id well you might be wondering how the primary key will be generated and it will be assigned to this product object while saving into the database right well basically save method internally you know uses hibernate to create the primary key and it will assign to product entity and then it will save into the database right and here same method basically return the saved product object right and that we are basically storing in the different object and here we are basically checking whether this saved object has a primary key or not well in order to print a complete product object we can use tostring method right so go to the product class and here we can simply use add to string annotation from lombok library so lombok library will basically provided a tostring method at a run time for this class so go ahead and call tostring annotation over here and then let's go back to our test class and here we can simply put another cs out okay and just call tostring method of saved object okay now let's go and let's run this jna test case and let's see how this works and how abundant will create sql queries behind the scene so let me execute this jne test case now well well one more important point here is we are using sequence as a primary key generation strategy right and hibernate will trigger a few more sql statements to get the sequence from the table and update the sequence into the table now let's go and get see the output of this jd test case and here you can see hibernate generated couple of sql statement to get the sequence from the sequence table and update the sequence in the sequence table so these are the couple of uh hibernate generated sql statements to drag the sequence into the sequence table next you can see hibernate generator sql statement that is insert sql statement to insert a record into the database table right and these are the two six out uh you know prints for example we have provided a sysout to print the primary key and the product information right here you can see all right so this is out prints a primary key as well as product information all right it means that hibernate behind the scene created a insert sql query to save a product record into the database and as we are using sequence as a primary key generation strategy so ubuntu will trigger a couple of sql statements to get the sequence from the sequence table and update the sequence into the sequence table now let's head over to the mysql workbench and let's see how this record is you know saved into the table well let's go ahead and let's refresh the schemas and let's go ahead and let's select the rows from the products table and you can able to see a product record is successfully saved into the database table and let's also see the sequence and you can see two okay so sequence is also updated successfully in the sequence table it means that we have successfully used save method to insert a product record into the database table in next lecture we will see how to use save method to update an existing entity all right i will see you in next lecture hi welcome back in previous lecture we have seen how to save an entity using save method in this lecture we will see how to update an existing entity using save method well as i mentioned we can use save method to perform two operations like save operation as well as update operations right so in case of save operation save method internally uses entitymanager provided persist method and in case of update operation save method internally uses merge method to merge the updated information all right so let's switch to the english idea and let's see how to use save method to perform update operation i am going to head over to the increase idea and go to the test class and we are going to simply write the journey test case to execute the piece of code and here i am going to use a test method something like update using save method all right and let's go and let's annotate this method with add test annotation so that junit framework can recognize this method as a jna test case all right perfect now you can see in the database table we have already a product record right so let me show you so we have already a product recording to the database table now we are going to update this product record okay so you can see product id is one right in order to update an existing entity into the database table we're going to perform different steps so first what we're gonna do is we're gonna find or retrieve [Music] retrieve an entity by id from the database and then what we'll do will update retrieved entity update the entity information and then finally we save the updated entity into the database table all right so in order to retrieve a entity from the you know database table first we need to have id right and as we know that product has id one right so let's go and let's declare id here let's say long id equal to one so we are going to basically update a product with id 1 and then we need to call find by id method to retrieve a record from the database so we're going to simply call here product repository and then we call find by d method and look at your find by id method basically takes id of type long and fired by id method returns optional of type product so go ahead and select this find by id method over here and just pass id as a method argument and then call get method to get a product object from the optional class now we got the product from the database table let's go and let's update the information of this product object so if you can go back to database here you can see product name is product one okay and product description is product one description so let's go let's update these two fields so i'm going to say set product name something like updated product one okay and product dot set description let's say updated product 1 description something like that and now let's go ahead and let's save this updated product object into the database table so in order to do that let's call save method right so just call product repository and its method that is save method and just pass a product object to the same method well in this case save method will basically update the product information into the database table because product is the existing entity in the database table right and product has a primary key isn't it that's why same method internally called entitymanager's merge method to merge this updated information into the database table all right let's see how this works let's go and let's execute this jna test case and let's see how hibernate will generate sql statement behind the scene well let's go and let's see the output of the jne test case and you can see hibernate has generated a lot of sql statements and let's understand one by one if you go back to our jna test case we have called find id method to retrieve a product from the database by using id right so hypernet basically created a select sql statement for find by id method behind the scene and if you look at the where clause here hibernate generated sql statement that is select sql statement it has a where clause with id okay and next in order to update the information hibernate trigger one more select sql statement to retrieve you know product by id and then it will update the product information all right so look at here the update sql statement all right so as i mentioned earlier we can use save method to perform both save and update operations and in this case you can see hibernate behind the scene generated update sql statement to update the product information into the database table alright so this is a very important you should have to understand how to use a method to perform save and update operations right so by looking into this sql statement that hibernate generated behind the scene we can able to see that how save method works and how it uses hibernate to save and update the information into the database table alright i hope you understood how to use save method to update an entity into the database table alright i will see you in next lecture hi welcome back in this lecture we will see how to retrieve an entity from the database table using find by d method well find by id method is basically from crude repository interface and find by id method returns optional i type entity all right so look at here as name suggest find by id method allows us to get or retrieve an entity based on the given id from the database table alright let's head over to the inclusion id and let's see how to get or retrieve an entity based on the given id from the database table using find by id method well let's head over to test class and here we are going to quickly write the jna test case and we will write the logic to retrieve an entity from the database table using find id method find by d method all right and let's annotate this method with add test annotation now let's head over to the mysql workbench and let's see what is there in a product table well here you can see we have one product recording the products table all right now let's pass this id 1 and retrieve this product by using find by d method all right it's pretty simple let's first declare here the id in our case it is one so typically in real time project this id basically comes from the client now in order to test find id method we simply hard code the id as a one here okay now let's go and let's declare a product object over here and then call product repository and it has find by d method all right and look at your find id method takes id i type long and it returns optional app type product okay just go ahead and select find by d method here and just pass id to it and as find by the method returns optional so we can simply call get method to get a product object from the optional type perfect now we got a product object by using find by d method and if you can simply press ctrl and you can click on find by d method you can able to see that find by id method is belongs to crude repository interface okay let's go back to our code now let's run the this jna test case and let's see how hibernate will create a sql query behind the scene for this statement all right let's run the jne test case so this is simply like we run the main method right so this jna test case we simply run as a standalone to just check this piece of code all right look at here test passed and let's see the output of this jna test case and here you can able to see hibernate generated select sql query okay so here select all these product columns from products table where product id equal to one okay so this is the basically hibernate generated sql query behind the scene for find by d method well whenever we have a requirement to retrieve an entity from the database table using spring data gpa then you can go ahead and use find by id method of crude repository interface all right and find id method internally uses habit to generate sql query like this this is the select sql query all right i will see you in the next lecture hi welcome back in this lecture we will see how to use save all method to save multiple entities the new database table well save all method is basically from crew repository interface and save all method returns a list of iterable objects okay so whenever you have a requirement to save multiple entities into a database then you can go ahead and use save all method all right let's head over to the entry id and let's write the logic to see how to use save all method to save multiple entities into the database table let me go to the increase idea and let's quickly write the method over here wide and let's call it as save all method and let's annotate this method with add text and notation and let's head over to the mysql workbench and let's see how many records are there in a product table well let me refresh the schemas and you can able to see only one record we have in a product table let's go and let's insert multiple records in a product table using save all method well let's go back to integer id again and in a save method we have already written you know code here to create the product object so i am going to simply copy this piece of code and i am going to paste it here okay so let me quickly change the values let's say product 2 and this is product to description and let's say the stock keeping unit something like abcd and let's change price price from 100 to 200 okay and the image url product2.png okay and i will create one more product record let me quickly copy this and paste it here and let's call it as product or let's call it as product 3 and let's change the values product 3 product 3 description and this is 100 abcd let's call it as e and let's change the price from 200 to 300 and also the image url all right and also replace this object reference here product3 all right now we have created two product objects let's go and let's use save all method to save these product objects let's one let's call product repository and it has save all method well look at your save all method basically takes a list of iterable entities and it returns a list of type entity okay let's go and let's select several method here and it expect list so go ahead and you know create the list and it has op method all right and let's go and let's pass the product object to this op method perfect well what we have done basically we have called save all method and we have passed a list of product objects to the save all method now let's go back to the mysql workbench and if you can see the products table contains only one recorder now let's go and let's run this jna test case and let's see how this save all method works and how hibernate will create sql query for this save all method behind the scene well let me quickly run the jna test case now and there we go test passed and let's see the output of the test case well look at here hypernet have generated a lot of sql statements let's understand these sql statements one by one well post this sql statement is to select a value from the sequence table and this sql query to update a value in the sequence table okay so basically whenever we insert the record then hibernate will create these you know two sql statement to maintain a sequence as a primary key in the sequence table okay so this is for inserting a single record and next these two sql statement for inserting a second record okay because here you can see save all method basically insert two product objects right and here you can see there are total four sql statement for managing the you know sequence values in the sequence table okay so first one this is second and this is third and this is fourth and then later hibernate create you know insert queries to insert the records in the database table and here you can see save all method basically insert two product records right that's why you can able to see hibernate has generated to insert sql statement so this is one and this is second all right i hope you understood how save all method works well basically save all method internally uses hibernate to generate insert sql statement to insert a individual record into the database table now let's head over to the mysql workbench and let's see how these records are inserted in the products table well let me go to the massive workbench and let me select records from the products table and you can able to see product 2 and product 3 so these records are inserted in the products table well whenever you have a requirement to save multiple entities at the same time then you can go ahead and use save all method well save all method is basically you know it does a bulk insertion operations all right great i hope you understood how to use save all method to save multiple entities in the database table alright i will see you in the next lecture hi welcome back in this lecture we will see how to retrieve all the entities from the database table using find all method well basically find all methods belongs to crew repository interface and find all method returns a list of iterable objects all right so whenever you have a requirement to retrieve or get all the entities from the database table then you can go ahead and use find all method all right let's head over to the intelligent idea and let's quickly write a piece of code to see how to retrieve all the entities from the database table using find all method well let me go to the english idea and let me minimize this and i will go at the bottom and here i'm going to quickly create a method wide and let's call the method name something like find all method all right and let's annotate this method with add test annotation well let's head over to the mysql admin and you can able to see there are three records in a products table and once we call final method then final method should return all these three records from the products table right so let's go ahead and let's see how to use find all method to retrieve all the records from the products table well within this method here i am going to declare a list and then i'm going to pass product as a type [Music] and let's go and let's use product repository over here it has find all method and look at here there are multiple find all overloaded methods and we're going to use this find all method that is first one you don't have any method arguments right and find all method returns a list of entities in our case list of products all right let's go and let's select find all method that's it this final method returns a list of products and that products we have stored in this list of products object okay now what we can do basically we can simply print the products name from the products list for that here i am going to use for each method and for each method takes a functional interface we're gonna use a lambda expression to implement this functional interface so let me quickly write the lambda expression here and within a lambda expression i am going to put one cis out to just print product name okay great now let's run this test case and let's see how this final method works and how this final method you know use hibernate to create the sql statement behind the scene let's run the jna test case now and there we go the test passed and let's see the output of the test case well look at here select sql query that is generated by hybernet behind the scene so this is a plane you know select sql query there is no where clause for this select sql query right and this select sql query basically retrieve all the you know records from the products table all right and here basically you can see we have printed product name in the for for each loop right here by using for each method right so that is what we can able to see here in a console alright it means that find all method internally uses hibernate to generate this sql select query to retrieve all the records from the database table all right i hope we understood how to use find all method to retrieve all the entities or records from the database table all right so whenever you have a requirement to retrieve all the entities or the records from the database table then you can go ahead and use find all method all right great i will see you in the next lecture hi welcome back in this lecture we will see how to delete an entity by id from the database table using delete by d method well basically delete by id method is belongs to crew repository interface and delete by id method does not return anything it simply return a white okay so whenever you have a requirement to delete a record or entity by id from the database table then you can go ahead and use delete by id method well let's head over to the integer idea and let's write a piece of code to see the usage of delete by id method let me go to the integer idea let me minimize this and here at the bottom i'm going to simply create a test case let's say delete by id method okay and let's annotate this method with add test annotation all right now let's head over to the mysql page and here you can able to see there are three records in the products table and we are going to delete the product with id one okay so go back to intellij idea and here i am going to declare id equal to 1 well in real time project basically this id comes from the client and in order to test delete by id method we simply hard code the id as a one here okay now let's go ahead and let's call product repository it has a delete by d method well look at here delete by id method takes id of type long and it returns a wide it means it doesn't return anything okay simply call delete by id method here and just pass id okay that's it so this will basically delete a product with id one okay so let me format this and let me run this jna test case and let's see how hypernet will create a sql statement behind the scene for this delete id method let me run the test case now and there we go test passed and let's see the output of the test case and here you can see couple of sql statements generated by hypernet well whenever we use delete by id method to delete a record by id then hibernate behind the scene generate these two sql statements first sql statement is to select the product by id and then second sql statement is to delete that particular you know product with id given id from the products table okay so hybrid basically generate these two sql statement behind the scene for delete by id method call okay now let's head over to the massive workbench and let's see whether this record is deleted or not well let me refresh this key marge and let me select rows from the products table and you can able to see the record one is successfully deleted from the products table well whenever you have a requirement to delete an entity by given id from the database table then you can go ahead and use delete by id method alright great i will see you in the next lecture hi welcome back in press lecture we have seen how to delete an entity by id from the database table using delete by id method right in this lecture we'll see how to use delete method to delete an entity from the database table and notice here delete method you know it does not take id it takes a whole entity object all right and delete method is basically from crude repository interface and look at your delete method does not return anything it simply returns a white wide meaning nothing right and delete method takes you know entity object all right now let's see how to use this delete method to delete an entity from the database table let's head over to the inclusion idea and let's quickly write the piece of code to see how to use delete method well here i'm going to quickly create a method let's say wide delete method and let's annotate this method with add test annotation and if you can head over to the mysql workbench and we have two records in the products table now we are going to basically delete the record with id 2. well in order to use a delete method to delete a particular entity from the database table first we need to retrieve that particular entity from the database table by using id okay so here basically we need to perform two steps first we need to find an entity okay by id and then we call delete method and we pass the entity that we are going to delete okay first let's go and let's retrieve a product by d so first we'll declare the id let's say id equal to 2 okay so we are going to basically delete a record with id 2 right so so that is what i have just initialized id with you know value 2 so ideally this id basically comes from the client right so in order to test this delete method we just simply hard code this id over here and next so this delete method basically expect entity as a method argument so let's go and let's retrieve this entity from the database table so in order to retrieve we're going to call find by id method right so simply call here product repository it has find by id method and we're going to pass id so find id method basically returns optional okay of type entity so let's go and let's call get method to get a product object from the optional type now we have identity that is product entity now we can call delete method and we simply pass this product entity to the delete method to delete from the database table well let's call product repository over here and then simply call its method that is delete method and look at your delete method takes identity as a method argument and it does not return anything it returns wide all right so let's go and let's select delete method and just pass product object to it that's it now let's go and let's run the test case and let's see how this delete method works and how hibernate will create sql statements behind the scene for this delete method well let me run the test case now and there we go test passed and let's see the output of the jna test case and here you can able to see a lot of sql statements generated by hibernate let's understand these sql statements one by one so this first sql query that is select sql query is for find by id method well if you can go to the code you can see here we are calling find by id method and it takes id as a method argument right and this find by id method behind the scene you know uses hibernate to generate this select sql statement all right and look at here the where clause here in a where clause id equal to placeholder and this placeholder will be replaced with the actual id value while executing this sql query all right so this is the first sql statement well basically this delete method it internally first select the record by using id for that it you know create this sql statement that is select sql statement and then it will trigger one more sql statement that is delete sql statement to delete that particular you know record by id okay that's why delete method internally create these two sql statement by using hibernate okay and earlier we have seen you know in case of delete by id method also there were two sql statements you know generated behind the scene by the hypernet right and for delete method as also we can able to see haven't generated two sql statement to delete the record post sql statement is to retrieve you know record with the id and then second sql query to delete that record with id so if we can closely observe these two delete methods like delete by id method and delete method so there is a some performance impact okay so when we were using delete by id method it behind the scene generated two sql statements right first one is select and second one is delete but in case of delete method we are basically you know pass a entity instance as a method argument but in order to you know fetch this instance we need to make find by id method called right and this find by id method call basically you know trigger one more sql statement behind the scene that's why whenever you have a requirement to delete an entity by id then you should use delete by id method because it will trigger to sql statement and if you use delete method then you need to first get the entity by you know id by using find id method this will trigger sql statement behind the scene and then when you call delete method then this will basically trigger two more sql statements you can able to see first one is select sql statement and second one is delete sql statement whenever you have a requirement to delete an entity from the database table by using id then you should use delete by id method alright great i will see you in the next lecture hi welcome back in this lecture we'll see how to delete multiple entities from the database table using delete all methods will basically delete all method is from crude repository interface and if you can able to see here there are two overloaded delete all methods well first delete all method takes list of iterable entities and second delete order method does not take any method arguments all right let's see how to use these two delete all methods to delete multiple entities from the database table let's head over to the intellij idea and let's write a piece of code to see how to use these delete all methods to delete multiple entities from the database table let's head over to the integer idea and let me minimize this and let me quickly create the method over here let's say delete all method and the return type should be wide all right and let's annotate this method with add test annotation and let's head over to the massacre workbench and let me trigger this sql statement that is select star from products and you can able to see there is only one record in the database table okay so now what i'm going to do is i'm going to insert one more record into this product table well in order to insert one more record i am going to run this save method test so this will basically you know insert this product one entity into the database table well let me quickly run the test case so this will basically insert this record into the database table all right test first and let's head over to the mysql workbench and let's select rows from the products table and you can able to see there are total two records in a products table now we are good to use delete all method to delete these multiple records from the products table now let's go back to english idea and go back to our method here and let's call product repository object and let's call delete all method over here so delete all method basically delete all the entries or the you know records from the database table all right that's it now let's run the test case and let's see how this delete all method works and how the hibernate will you know generate sql statement behind the scene for this delete all method well let me run the test case now there we go test passed and let's see the output of the test case and here you can able to see hibernate has generated couple of sql statements so first one is to select all the records from the products table right so when we were using delete and delete by id methods that time hibernate generated you know select sql query to select a particular particular required right but in case of delete all method hibernate basically execute or create this select sql query to select all the records from the database table right there is a no where clause you can able to see this is a plain you know select sql query to select all the records from the product table next hypernet will trigger individual delete sql statements to delete individual records from the database table well if you can closely observe this sql statement hibernate first select all the records from the database table and then based on the records it will get it will create the delete sql statements for example in our products table we have two records right so hibernate behind the scene created two you know delete sql statements to delete two product records from the products table right and if we can see the where clause here it will pass the id to delete that particular you know record from the products table right and let's say if there are five records in the products table then habinet will create five individual delete sql statements to delete for you records from the products table right so this sql statement is to get all the records from the database table and then based on the records it will get it will create a delete sql statements and pass and then it will pass id to delete that particular record from the database table alright i hope you understood how this delete all method works and how hibernate will create sql statements behind the scene for this delete all method well whenever you have a requirement to delete all the entities or records from the database table then you can go ahead and use delete all method well there is one more delete all method which takes a list of iterable objects and if you can see you know my second workbench let me select the rows from the products table there are no records in the products table right let's first insert couple of records in a product table and then we'll see how to use delete all method which takes a list of iterable objects so in order to insert multiple product you know products objects i'm gonna execute this save all method so this basically insert two product records in the database table so let me execute this jne test case and there we go test passed and let's head over to the mysql workbench and let me select rows from the products table and you can able to see there are two records inserted in the products table now we are good to use delete all method to delete these records from the database table and you can able to see the id 5in6 okay now let's head over to the integer idea and let's write the logic to use delete all method to delete specific entities from the database table okay now what i will do i will comment out this okay and i will get a product individual product from the products table well in order to get a product by d then what we need to do is we need to simply call find id method and if you can see the id for the records 5 and 6 right so we need to pass five here all right and then call get method so this will return a record that is product record with id for you similarly let's get one more entity from the database table by using find by id method let's call it as product1 product repository and then find by id method and just simply pass six as id and then call get method perfect now we have two product entities okay now we are good to call delete all method right let's call product repository and then delete on method so make sure that you choose delete all method which you know takes list of iterable objects this one let's select it and it expect list as a method argument let's create a list here and just pass product comma product 1 it means that we are using this delete all method to delete a specific entities from the database table we are not deleting all the entities we are specifying here the entities that we are going to delete from the database table right so let me run the test case and let's see how this deletal method works and how hibernate will create a sql statement behind the scene all right test pass line let's see the output of the jne test case and here you can see a lot of sql statements generated by hibernate so we have called find by id method twice right so these two find by id methods you know behind the scene create sql statement so this sql statement used to retrieve a product with id file this one and second sql statement that is select sql statement to retrieve a product with id 6 all right and remaining these four sql statements you know that is created by delete all method and again you can able to see there are two delete sql statements to delete two entities from the database table in our case we are deleting two product entities right that's why we can able to see there are two delete sql statements all right i hope you understood how this delete all method works and how hibernate will create sql statement behind the scene for this delete all method all right great i will see you in the next lecture hi welcome back in this lecture we'll see how to count a number of records that exist in the database table using count method well count method is basically from crude repository interface and count method returns of type long okay so whenever you have a requirement to count the number of records that are exist in the database table then you can go ahead and use count method all right let's head over to the integer idea and let's write a piece of code to see the usage of count method let me go to the integer idea and here i want to quickly write the method let's say white count method and then let's annotate this method with add test annotation okay let's head over to the mysql workbench and let's see how many records are there in a database table let me select rows from the products table and you can able to see there is only one required in the products table now let's go and let's call the count method over here and basically count method returns you know long type so declare here long and let's say count equal to product repository and it has a count method so let's go and let's call the count method over here and let's put one sis out to just print the count all right that's it let's go and let's run the test case and let's see how this count method works and how happened it will create the sql statement behind the scene for this count method and there we go you can able to see test passed and let's see the result of this test case and you can able to see the count is 1 and here is the hybrid generated sql statement select count star from products table alright so this is how the hibernate will create a sql statement to count to the number of records in the database table alright and if you go back to my second workbench you can able to see there is only one record in the database table right that's the reason the count is one well let me insert a few more records and let's see how this count method you know works well i am going to run this save all method to insert these two records okay so let me run this test case and we'll insert these two product entities into into the database table and let's see how this count method works okay let me go back to uh my second workbench and let me select the rows from the product table well you can able to see there are three records in the products table now all right now let's go and let's call the counter method okay so let's run this test case and let's see how this count method works all right so look at here this pass line let's see the result of this test case and you can able to see count method returns you know three because there are three records in the database table all right so whenever you have a requirement to you know retrieve a number of records in the database table then you can go ahead and use count method all right great i will see you in the next lecture well this is a very basic series or course on spring data gpa but if you want to master spring data gpa then i am going to suggest my udemy course that is masters ping data gpa with hibernate so this is a 15 hour course and in this course you will learn everything about spring data gpa and you will also learn how to build a domain model relationship for simple e-commerce project using hibernate orem framework well this masters spring data jpeg with hibernate course is highest rated course on udemy so this is a very popular course on udemy so i am going to suggest you to check out this course in order to learn spring data jp with hibernate in depth
Info
Channel: Java Guides
Views: 38,783
Rating: undefined out of 5
Keywords: spring boot, spring data jpa
Id: dKK2dZVLFug
Channel Id: undefined
Length: 167min 2sec (10022 seconds)
Published: Wed Sep 21 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.