Spring Data JPA Example with Oracle Database | Spring Boot App

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we will understand how to develop spring data jpa application to communicate with Oracle database all right my name is Ashok Welcome to our Channel Ashok it let's get started first step create spring boot Standalone application with below dependencies one is boot starter that will come by default second one boot starter data jpa to perform DB operations third one database driver I'm using Oracle database so I will use Oracle driver if you're using my SQL you can configure my SQL driver and project lombok which is used to generate Setters and Getters for our entity class if you're not using Lomo you can write Setters Getters manually once the project got created successfully we need to configure data source properties in application. properties file data source properties means database URL username password and Driver class then third step step create entity class entity class is used to map with the database table the Java class which is mapped with database table is called entity class as we are going to use orm benefits we need to map our Java class with the table that is called object relational mapping we will use some annotations to map our Java class with the database table then create our repository interface by extending from data jpa repository jpa provided repository and jpa repository to simplify crowd operations we can extend the properties from any one of those repositories now let's start I'm using Oracle database so to connect with the Oracle database I'm using SQL Developer as a client software using this SQL Developer I connected with my database and let me create a table in the database I'm creating a table contacts Master this table is having contact ID data type is number contact name data is worker contact number that is also a number type and I'm representing contact ID as a primary key in the table my table got created successfully now go to spring tool suit IDE to create our boot application with data jpa I'm creating spring starter project give some name for the project click on next and select here dependencies as we discussed jpa dependency required verle driver and lombo dependencies yes spring data jpa dependency selected you can search for that verle driver dependency selected project longbo dependency selected click on next and click on finish our project got created successfully let us configure data source properties in application. properties file configure data source URL configure data source driver class let us create new Java class that class we are going to use as entity class I'm creating this in a separate package called entities contact Master entity I'm writing variables in this entity class these variables will be mapped with database table column names to represent Setters and Getters for these variables we are using at the rate data it is a lombok annotation if you don't know about lombok you can write the setter Getters manually this this is our Java class and this is our database table now we need to map our Java class with the database table class name will be mapped with the table name and variable names will be mapped with the column names to map class name with the table name we will use at theate table annotation to map variables with the column names we will use at the rate column annotation like this you can map all the class variables with the columns in the the table here writing at theate table at theate column annotations are optional because if you don't write table and column class name will be considered as a table name variable name will be considered as column name if your class name table name if your variable name and column names are different then you can map them using table and column annotations to represent this Java class as a entity it's mandatory that we need to write at theate entity annotation at theate entity is a class annotation it makes our Java class as a entity class and to map our variable with primary key column in the table we will use at theate ID annotation it is variable level annotation we are using contact ID as a primary key so contact ID variable mapping to primary key column using at theate ID now let us make changes to this Java class to make it as a entity class at theate entity which is used to represent our class as ID entity at theate table annotation to map our Java class with the database table table name is contacts _ Master at theate ID to represent this as a primary key column at theate column to map this variable with particular column name in the table same thing I'm mapping all the variables with columns in the table using at theate column annotation now let's create repository interface by extending the properties from jpa repository I'm taking this as contact Master repo keeping in a separate package called repositories to represent this as a repository we need to extend the properties from jpa repository I'm extending from the crud repository we need to specify this repository should communicate with which table for that we will map entity class name here and what is the primary key of that entity data type of the primary Q will specify as ID T represents type of the class so this repository should communicate with the table using context Master entity and that context Master entity primary key mapped variable data type you can specify serializable or you can specify integer our primary key is a number type this is our interface which is extending from cred repository using this we can perform operations with our database table now let's go to our spring boot start class in the start class we are calling the run method run method will return configurable application context object as a return type using that we can get our B objects I'm storing run method return type into context variable using this context we can get our Bean objects I'm getting context Master repository Bean object by using context context. get bin of Master repository. Class here repository is a interface then how can we get the object that's the magic here data jpa will provide implementation for our repository interface in the runtime by using proxy the proxy Bean object we are going to get here using that proxy Bean object we can perform our operations now I'm setting the data to entity object to insert your record created enti object setting the data by using Setra methods contact ID contact name and contact number with this our entity object is ready with the data now we need to insert This Record using our repository interface as our repository interface extending properties from crud repository we can access the methods available in the crud repository there is a predefined method called save method which is used to insert the record bin. save of entity bin is representing our repository object by using that we are calling save method save method is a predefined method provided by CED repository which will take entity object as a parameter and it will insert the record into database table based on the data available in the entity object before going to execute our application let's verify the table select a star from table name let's see what is the data that is available in the table if you see our table does not have any data as of now table is empty now run our application which is calling repository. save method to insert your record run as boot application application executed and terminated go to table and execute the query now we can see our record got [Music] inserted
Info
Channel: Ashok IT
Views: 8,021
Rating: undefined out of 5
Keywords: spring boot tutorial, learn spring boot, spring data jpa tutorial, spring data jpa repositories, learn microservices, jpa find by methods, data jpa custom queries, spring orm, spring jdbc tutorial, hibernate tutorial, learn java programming, data jpa interview questions, ashokit, spring boot vs spring, spring vs spring web mvc vs spring boot, programming tutorials
Id: ZGKHCJsp4hg
Channel Id: undefined
Length: 8min 58sec (538 seconds)
Published: Wed Aug 09 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.