Spring Data JPA [PRO] - How to connect Multiple Databases using Spring Boot | Multiple Datasources

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back to my another spring data jpa Marathon video and in this video our end goal will be creating multiple data sources for our spring boot application so if you have a spring boot application and your application need to connect to multiple databases like a S2 database and a mySQL database or a uh SQL Server database or a db2 database if you have two databases or three databases for your application then how you will create multiple data sources in order to connect to them and it's a very important interview question and we're going to be solving it in a stepbystep manner understanding everything that we're going to be coding over here in a in a in a in a step-by-step approach and you're going to be learning a lot of different things about um you know spring data jpa in this video as well this is not going to be a very straightforward video where I'll be coding everything without explaining so we will understand different things like how you will config a session Factory or entity manager how you will configure multiple transaction manager how you will be using atate enable jpa repository and how you'll be using different attributes which is present inside it you're going to be learning about atate configuration properties and how you can use it with atate bean and what is the benefit of it a lot of things we'll be covering here in a step-by-step approach which will give you a very good fundamental on your spring data jpa um understanding that you have right now obviously it is going to enhance that and guys you know before before we start this video if you are a if you are a beginner if you if you are very new to Spring data jpa you have never quoted in Spring data jpa you are coming from Spring MBC background or a raw hnet or a mind betst background then I have a video to get you guys started you can watch this video on Spring data jpa in order to understand the basic functionality how different things works and then you can continue watching this but if you are not a beginner welcome to the video my friend let's just get started guys one thing is that I have I have um seen lot of people answering this data jpa question in a very raw manner in Spring wood interviews for an example I have seen people whenever we ask any question about spring data jpa the way they're answering is the all I know about spring data jpa is to create an interface and extending it to the crow repository or the repository interface and then all the crow ated feature like saving updating cing every feature will be generated by Spring Boot and that's it we can take the method and can use it but spring data jpa is not about that we have to learn how to customize it how to configure all those internal stops because the developer you may need to customize your application anytime and that's what we're going to be doing in this video so with this challenge connecting to multiple databases we will be exploring lot of different internet components and we will be configuring them by ourself so I'm pretty much excited hope you are too so let's us get into the video don't forget to subscribe to the channel and obviously if you are enjoying this video right not right now after the end of this video if you like it if you feel that this video added some values to your spring data jpa knowledge then don't forget to like this video as well so I'll see you in the video right now let's just get started [Music] okay so right now let's just go ahead and start creating our project I am in my spring initializer website as you can see I'm in my start. spring.io website and I have created an artifact called Spring boot MDS the MDS stands for the multiple data source and I have created my group ID and also I have created my package name over here okay which looks good I'll be creating the project in Java and the build tool that I'll be using in mavin and I'll be using the 3.2.5 version here and the Java 17 and the packaging as jar this should be fine with with me so let's just go ahead and add some dependencies right now so the first dependency will be the spring data jpa because obviously I want to use hibernate behind the scene the spring data jpa will give me the hibernate support so that I can create connection and connect to the database that I want to connect to and I want to use initially uh the MySQL so I'll be using the MySQL driver here later on I'll be adding the second data dependency um maybe a post Grace SQL dependency here but let's just start with my SQL over here and I do not really need the spring wave but let me add it over here maybe later if I want to Sol something on UI I may need to use this so let me just add the spring wave for now but you might not need this so if you are building it for your practice purpose you can skip this dependency okay so minus 12 driver and the spring data jpa this should be enough for me I feel uh so that's it so I'll go ahead and generate the project okay and once it's downloaded I will get this into my intell idea all right so now I have my project imported to my intell and here I just want to do a very simple thing uh right now let's just imagine we have two different entity or two different tables right so now imagine there is a product table there is a order table right so we want to check in data to both of this table right now using our application called Spring boot MDS uh but the thing is this table need to exist in two different databases for an example the product table need to exist in my MySQL and the order table need to exist inside the post SQL and we need to continuously push data to both of this table which exist inside different databases and that is going to be the challenge for us in this video but as always let's start it in a stepbystep approach uh forget about multiple databases right now imagine we have only one database right now and that's going to be MySQL for an example so here goes my MySQL workbench let it open and right now if I'll go into my database by ENT entering the password here okay so here you can see I have created a schema for you called MDS and inside this MDS schema I have this tables and here I need to create my tables over here maybe uh initially let's just do one thing let's just create both of my tables here my product table and Order table both of this table let's just get uh let's just create it over here inside my mySQL database and and later we're going to be segregating it into two different databases like uh a product table goes here to the MDS schema of my MySQL then the order table should go to the Post Grace uh database right we're going to be doing that later but for now to make it simplified let's just create both of our entities inside this schuma called MDS so let's just go to our Intellis idea right here and let's just start creating are entities so I'll go to my source main Java folder and right over here first of all I will create a Java entity so I'll create a class and um I will be keeping this class inside a package by the way but let me create this class first of all okay the class name will be let's say product okay and this is going to be the entity why I'm telling this product is an entity because the product will be having physical presence inside my database right so I want to create a table called Product but this is going to be an entity so I'll be creating a new package first of all and right here the package name will be let's say product entities or product entity so all the product related uh related entities I'll be keeping it inside this particular folder so I'll refactor it and here goes my product class which is going to be my entity so let's just go ahead and create an entity over over here so let's just use The annotation here called entity come on man entity okay and the next thing that I want to do here I will be using the table annotation and I will name this name this entity as product so I want a table created in my database which we will say as products uh make it plural because the table will be better if it ends with plural form like this produ right okay so we want to create this table so inside this product entity uh let's just do one thing let's just have a few variable let's say private in product ID private string product name and a private uh double the product price that's it and also I want to create a Constructor so I will be just create a Constructor here uh first of all let me just create a default Constructor okay and also to initialize all this variables here I will be creating another Constructor so I'll hit command in and I'll create a another Constructor and I'll select all this field and there we go okay so right now let me initialize the ID by myself as well so whenever I'll be creating the product I will also um give the ID so uh ID obviously will be my primary key so where it where it went over here okay so ID over here is going to be my primary key so I'll be write ID annotation I I do not want to give the generated value as identity over here but you can Define your strategy if you if you would like to uh say it as an identity so that the ID will be Auto incremented but you know for now I just want to have control over my ID whenever I'll be creating a product and whenever I'll be checking it into the database I want to give the ID by myself because it will be easier for me to sew it in the demo uh that's looking good so I have my entity ready let's just create the getter and Setter for everything so I'll create the getter and Setter and I'll create the getter and Setter for all the variables that I have over here by hitting a control n here so looking good so we have our product entity ready now in order to insert product to our database we need to have some methods like save method date method delete method in order to do some data manipulation but I only want to save or insert data to the database so what I want to do here I just want to use the spring boot feature a little bit because I do not want to write the code for saving the product to the database by myself I want to give this luxury to uh you know I I want to have this luxury uh because I want to use the feature from Spring boot so what I'll be doing here I will create a new uh package here and this is going to be my Dow package or repository package and uh inside this repository package maybe I will just better if I will just segregate it a little bit and I will refactor it and I will say product repository there is a package let's say product repository okay and inside this product repository I want to create a interface and inside this interface I'll be writing my logic to SA my product so I'll create a Java interface and the name of this interface is going to be product Dao okay uh so this product Dao will be a interface I do not want to write the sa method by myself rather I want to extend the crow repository from the spring boot so Crow repository and I want spring boot to create save method for me uh for the product entity so I'll be defining the product entity over here and also I'll be defining the primary key of the product which will obviously integer okay I have written in here but you can change it to integer but that's fine over here uh the primary key for the product the type is integer so there you go so I have my interface ready and right now spring boot will basically give the implementation for me for all the methods that we have inside the crow repository like I have the save method over here obviously this is an abstract method and this is an interface but as we have the crow repository extends to the repository interface the spring boot will automatically detect our class or interface right now we have this uh interface called Product Dao which extends to the crowd repository and Crow repository internally extends to the repository so spring boot right now automatically going to detect uh the repository um you know interfaces this is right now will be treated as an interfaces which will be a repository interfaces and the implementation proxy will be given by Spring boot in the run time so that we can use the save method so we don't have to write the implementation for this it will be automatically stopped by Spring wot so right now let's just go ahead and use this product Dao uh which will be leveraged uh right now from the crowd repository and we will be getting the sa method for free so we can use this sa method right now to save our product entity so I'll go to my main application right now and right over here can I just click on this and expand it a little bit so right now I just want to save a product so maybe first of all I want to have some space to write my code so I will make sure to implement an interface called um uh command line Runner okay and here I will be overriding the method that I have here called the run method so after the main method my run method will be executed automatically by springbot so I can write my code over here to save a product to the database so right now I will be create a product first of all so product uh product is equal to new product and uh I will be creating my product I'll be using that Constructor that I have created the product uh ID is one the product name is uh let's say a laptop and the price is let's say um let's say 2 lakh okay something like this all right so this looks good so now I want to save this product so let's just use our uh product Dao that we have created over here so first of all I will take this product Dao and I'll create an instance of this I'll make sure to import this one so I will do a control space and import this one and um hold on import class and I'll make it a private variable and I'll make sure to Auto wear it so autoed so whatever the proxy will be created by Spring boot in the runtime for the product Dao the spring boot will be Auto automatically generate the implementation for this Dao and the implementation proxy will be assigned to this product Dao variable now I can take this and I can use product Dao Dove method and I can save my product over here that's good so I think we are all set to uh you know save my product product to the database but I think if I'll start this application this will not work so if I'll run my springboard application over here let's just see what is going to happen let's just see my console and I'm sure that we're going to be having a problem here and this s it is asking me fail to configure a data source URL attribute and it is not specified Define the data source URL it is saying so it does not know right now you are trying to save a product but into which database it doesn't know so it is asking for the data source the connection URL so that your springbot application can connect to the database and can store this product here into the product table so there are some uh things to be done first question is that there is no product table exist in the database that we want to create we did not give that information to Spring boot what database we are using what is the way to connect with the data so we need to Define all this thing and this is what we call the connection information and the connection informations are called as the data source in Spring boot or in Spring framework okay data source means a connection okay so now we need to define the connection information so let's just go to the resources and this is spring boot we do not Define the connection information by creating the data source object data source being by ourself we can Define the connection information over over here and the spring boot will automatically uh read the connection information from this properties file and do some order configuration and make our application running so let's just give that information right now and let's just configure our connection information right [Music] here all right so right now I want to utilize a class here called data source okay properties okay there is a pre-existing class as you can see it is there inside the spring uh boot uh so like this particular class is pre-existing which comes with our data jpa dependency that we have added inside the pum and right now inside this class we need to configure few of it properties for an example we need to define the driver class name like what database we want to connect to we need to def find the url uh for the database that we want to connect to and the username and password if we provide this properties of this data source properties if we can assign the variable uh value to this particular properties then this class will be automatically uh will be loaded by our spring boot Auto configuration and the data source object will be created for us so once the data source object will be created we can connect to the database because the data source properties are the just the connection information so this properties could be accessed through this uh you know uh this uh prefix called spring. data source which is defined through the configuration properties so in order to access the property of um driver class name URL username we can use the spring. data source prefix here so can I come to my application properties and can hit this spring. data source and I want to specify an URL so uh the URL information I'll be specifying here and like that spring. data source then the variable what I have over there called username I will be copying this and going to my application. properties and specifying it over here and like that I'll be specifying the password for my database as well so I'll be writing password property I'll be initializing here and also let me just Define the driver class name so can I go to the data source properties over here and can I can I look for the driver class name so I think um I can I can I can just do this but this is a variable I think uh to assign this variable uh like this is C is capital N is capital I can just do something like this instead of saying driver class name I can say driver space class uh then and hyphen uh then name I think this is how the driver class name properties need to be specified can I just confirm it once in my uh uh official spring board documentation can I just look for spring data properties and look for the common application properties and can I go to the data properties here can I do a contrl f here and look for the driver class and look at this how it is defined with a with a dash so spring. datas source. driver class name so this is how I think I have defined it over here driver class name so make sure that if your properties uh is like this C is capital N is capital replace that n and C with small letter and then give a dash or a hyphen between that connecting WS over here so this looks good so let's just go ahead and Define our properties right now so the URL for my application right now if I'll come to the Intellis sorry if I'll come to my MySQL and in my MySQL right now if I will go to uh my home and right over here I'll be connecting uh I'll be copying the jdbc connection string and right here in my Intel I will be specifying the URL over here so so now this is going to be the connection URL for my database and inside this database I have a schema that I want to connect to and inside that schema I want to store my tables and what is that schema name the schema name is MDS okay here I want to create my table so I will go to my Intellis and I'm going to be mentioning here MDS okay and the username for my MySQL is root the password for my MySQL is aash and I think I spelled it correctly aash okay and the driver class name name I have my MySQL driver added to my dependency so I can look for my or I can just look for driver here can I click on classes and this is java.sql this is this is the one that I want to use from com. myql cj. jdbc I'll copy the uh reference over here and can I go to my application properties and paste it over here so my uh properties are defined right now so this is good good so right now let's just do one thing now I think I have defined my data source information right over here now this um this values that I have defined over here with that my autoc configuration can read the values from here and will create a data source for me and I don't have to create the data source object spring will create that and things will work fine for me so let's just see that whether that is happening so if I will go if let me let me first of all close everything can I go to my main springboard application and close rest of the things uh so close uh other tabs okay so now let's just run our application so can I start it run the main method okay and I feel right now uh the things will work fine so let's just see what is going to happen so now uh that previous exception is gone so right now my data source is getting created and I do not have an error for my data source but we have an other problem so let's just see what is the problem right now let's just go to the root okay and now the problem is there is a problem executing the SQL so the save method over here that we are writing is creating a SQL for us which is a um you know insert query and obviously before inserting this product as we are adding the product by ourself by leaving an ID first it is just firing a select query to check whether this ID one is existing just to make sure that it is a new entry or a um or an entry which is already exist so that it can update it doing some internal stuff in order to make before inserting this data it is just checking whether this one is existing inside the database or not this why it is uh piring a select query before making an insert query over here but whatever the reason is it is giving me a problem because whenever it is firing some query when whenever the save method is firing some query in order to save my product it is just finding the product table does not exist and obviously we do not have a product table exist inside our MDS schema when we do not want to create our product table I want spring to create that table for me so that's why I will go to my application. properties and I'll tell spring that high spring go ahead and create the product table for me and to do that what I'll be doing I'll be going to my Google Chrome and look for a property here called ddl Auto okay hiate spring. gpa. hen. ddl Auto okay I'll copy this I'll go to my intell and I post it over here and I'll say that create I'll give a create command over here so this create command will do what it will create the table first then it is going to insert the data so the sa method will create uh will create a insert query behind the scene and before that this application. properties file this particular thing will make sure that your table will be created But be sure that this create is not a very good thing to use in production because the create will make sure that whenever your application is starting if the table already exist then that will be dropped first and then it will be created again so in production you you can always use update so if the table does not exist the update command will first create it and then um from the next time if it is already created it will be it will be only be an update only the rows will be inserted the previous data will not go away because the create command will do what it will it will drop the previous previously created table so whatever data that you have inside the table that will go so I will be using the create command over here for the demo purpose but yeah so I will go with the create so now let's just rerun it and I'm sure that my table will be right now autocreated and now my data will be inserted so I do not have any log for that but I can see there is no error here so can I go to my uh my SQL and can I do a refresh over here I'm sure that refresh all I'm sure that we should have an entity created over here called products and we should have a data over here one or 2 lakh and the laptop is the product name so our save method do just work and it's created our entity and it's just uh inserted it to the database so things are looking good so we have taken our first step uh where we have created an a data source and we have inserted one record to the table through the data source the data source given us a connection to the the myus skill database and we have created our product entity or product table inside it and we have inserted or check in the data to the product table things are looking pretty much simplified right now but from here only the game begins because right now what we have to do we have to make sure we have to take control from the framework right now the framework is creating the data source for us right now but we have to take the control right now because right now in our application. properties we are defining only one data source but right now what I want to do I also want to create another entity called order and the order table should be created right now inside the post Grace database for example this is my PG admin 4 just like my SQL workbench this is the workbench to connect to the postc SQL database so here is my PG admin let it start but I want my order table to be created inside this so uh let it let let it get created uh let let it let it open first of all so it is asking me to enter my password and my master password is aash so uh obviously inside the server right now let me just enter the password for my DB and now over here inside the database I also have created one schema called MDS and right here inside the m MDS schema right now inside the schemas we have the tables here we have the tables where now here I want to create the order table so to create the order table over here using an application using the springboard application and in order to check in the data to there we need to have another data source created so now I want to do something like I this is my MySQL thing right so now I want to Define my post data uh Source over here so right now I cannot write the exact same thing like this and I cannot Define my postra SQL things over here so uh you can see I cannot Define if I'm defining like this I'm just getting an error over here already so I need to do some modification so let me comment out this thing the previously written thing okay so now what I'll do I'll just do spring. data source this is my my SQL related data source can I just do my SQL like this okay and Dot my SQL like this and can I just do dot after data source can I just write my SQL like this so now this is and know I have randomly typed my SQL over here if you want to type my SQL over here you can also type it over here I have written my SQL over here just to make sure this related configurations are related to my myql and I can copy the exact same thing right now and I can Define another data source okay let's say data source 2 or db2 connection details right and the previous one are my DB1 connection details right details me and my spellings okay and now this is my postc cql data source uh related information so I'm going to write PG post Grace stands for PG stands for post Grace and I'll be defining my password my username the driver class name for my post Grace over here now you can see it is not giving me giving me any problem but let me configure this information over here right now so instead of my SQL for the URL I'll be writing post Grace ql okay and the Local Host the port that I'll will be connecting let's just see that so if I will go back to my pz admin right now if I'll go to the schema that I have here so uh my DB is my schema can I just click on the properties and click on the connection details the port that I have is 5432 copy that and can I if you want to take S2 as your second database you can absolutely take it um but I am taking post gra over here here okay so 5432 is going to be my port and the schema that I want to connect to is MDS that I have already created over here right so inside this MDS inside this table right now I want to create my post Grace related tables over here for an example the order table I want to create it over here one more thing that I want to get from here if I'll go to this properties if I'll go to this connection the username is postrace so I'll copy this and I'll go to my Intellis right now and over here I will change my postc username to postc and the password is aash and the driver class name in order to connect to the postc SQL in order to create our uh uh create our connection to the post Grace SQL so that we can create also tables inside that we also need to connect to that so for for that right now I have to update my P right now I only have the depending for MySQL connector now let's just go to our Google Chrome and let's just go to the spring initializer and let's just add the dependency for post gr SQL driver to connect to the post gr SQL click on explore and go ahead and copy the dependency for postc SQL copy that and go to Intellis idea one more time and paste your dependency over here stop your application do a save and right now to uh sync it off click on this reload option now this will get our post gr SQL from the internet and right now this looks good so now if I if I'll do a shi shift and look for driver again and click on this classes right now I should have one more driver available from postc SQL right and now I can copy the reference from here okay copy reference I can go to my application. properties file right now and I can replace this my SQL driver to the postc SQL driver and I am I'm all set right now to have I have my connection details right now but now there is so now you you understand like you know I have customized my um I have customized my uh this thing spring. dat Source URL uh spring. data source username to this kind of things to spring. data source I have customized the my SQL over here you can write any over here I have written these things by myself but this will lead uh to an problem right to a problem right now what is that problem if you're going to be starting your application uh this will not work your data source will not be created you can see again it is saying failed to configure data source over here and obviously this is obvious because internally as I have told you whenever you write something like this spring. datas source. URL if you click on this you land you will be landing off in inside that class uh what what class is that uh data source Properties by writing this spring. dat source. URL do username these are all the properties which is defined inside this class right so what what is this uh thing which class this thing is referring to I have already told you this is like what is that data source properties class this one right uh so this is the prefix that that it has in order to access to uh these variables but right now we do not have access to this particular uh uh properties anymore that we have inside the class and we are not able to set or initialize this particular variable right now by writing something like this because when we write something like this we're not referring to that class anymore and this URL username and password the driver class name are not getting initialized inside that particular class so that is the class which is being used by the spring uh spring boot whenever the auto configuration kicks up uh but as those things are right now null so we are having a problem and our data source is not getting created our application is not getting started so right now the problem here is that how will be able to map this URL this username this password this driver class name uh from the MySQL data source connection details that we have specified over here all the postrace related connection information that we have specified over here our end goal right now is to uh is is going to set the URL the username the password the driver class name all this variable of this class data source properties we need to set this particular variables over here but now that's the challenge because now we are not using spring. data source anymore we have customized it a little bit right so how can you do that okay now it is going to be very simple now say Okay vas copy this thing spring. datas source. MySQL over here go to the data source properties over here and you just change it up like this to a control B replace it but this will not this will not work this file is read only this file is from Spring framework this is written by Spring framework this is a third party file I cannot able to change the source code of it I can only read it so how to do it [Music] how to change uh this particular prefix right over here and for this we have to utilize the connection configuration properties over here uh so uh so I need to show you how to do that right so I think you understand what this configuration properties uh does right maybe for an example for for a quick example what I can do uh just just just for clarity I can come over here I can create a new new class and let's say the class name is student and inside the student for example I have an ID uh int ID student ID and student name private string uh name over here and let's say I am writing a component over here and I am uh I am basically writing the configuration properties over here I just want to drag your attention to this configuration properties annotation and here you can write anything this is my class I'm creating it so let's say I want to um Define a configuration properties over here as my app. student so what does it mean it means that using this um you know using this uh configuration properties name that I have given over here I can access this ID I can assign the value to this ID and assign the value to this name for an example I can create the gator and Setter for this okay ID and name right over here and I can just create a two string in case if I want to print the object for this so do a command in and create a two string over here okay so right now if I want to assign the value for ID and name so I can do what I can copy this particular properties name over here I can go to application. properties file and right over here I can Define my properties like this my app. student Dot and I want to assign the ID so I can write ID over here and I can specify the ID as 1 2 3 I can also say my app. student. let's say name and name I want to specify as AAS and that's it so we using this using this what I have done the ID and name of the student I have specified over here for this student. Java and right now I can utilize it anywhere so for an example I can go to my spring boot main application and right over here I can just write student over here and a student is already a component so the object will be created by Spring I can simply autoare it just for demo if I'll print this reference over here for an example if I'll do s out and if I'll print let's say student object value and if I'll be printing the student over here so you'll see right now obviously my data source problem is there right we haven't fixed it so I will comment this off for a minute uh I'll also comment this information I'll uncommon the previous uh data source URL the exact properties which will be basically referring to the um you know data source properties class I'm using the exact prefix over here so my data source will not bug me I just want to run this I just want to show you these two things using this I'm able to assign the value inside the student class ID and name and you can see right now in the log the ID is printed as 1 2 3 and the name as of elas because I have used this application properties this thing uh the ID and name right now assigned it in my properties file right over here okay and that's how I'm able to access the value over uh I I'm able to assign the value for ID and name because whenever uh you know you are assigning the value internally the setter method of set ID and the setter method of set name is getting called and through the setter method I'm able to access to this private variable over here and I'm able to access this so this why I have specified this Setter here but the problem will be uh when because imagine your client will say Okay AAS right now what I want instead of using my app. student over here can I just write selenium express. student I will say yes why not because this class I have created I can go over here and can Bank this class and I can change it to selenium Express right so now this becomes selenium Express right now I can copy this selenium Express student right now I can go to my um you know application properties and I can change it right now I can see if I'm going to be running my application one more time this variables right now will be null and null so if you're going to be seeing here the student variable that I'll be having will be ID default value is zero name default value is null right now this my app student is not working if you want to change change it to selenium expressor student and instead of my my app student you can change it to selenium expressor student in order to assign this ID and name to those uh you know variable inside the student class right now it should work fine so where did I click okay I think it's coming up now it's working fine so that's good so now my client told that okay change the configuration properties to selenium Express the student and I have changed the same thing but I was able to change it because the student class is written by me but if it is a class like data source properties which is a third party class I cannot come and change this particular properties name over here so we need to find a way around so how can we change this thing how can we change this thing in order to get our things like this spring. datas source. PG or spring. datasource MySQL if I want to give prefix like this in order to access this username password driver class name this kind of variable from this class if I want to access this variable instead of this name if I want to give my own custom name then how can I achieve it so let's just have a look on [Music] that all right so in case of a third party class like data source properties okay let's say if I want to change this prefix for an example example right now let's go to the application. properties file okay let's just comment out or let's just remove the things completely okay now what we care in our application. properties file is H this is how we'll be defining our data source information related to different database now imagine I have only one database right um imagine this is my SQL this is the information that I have related to my SQL the post one I have commented it out uh I'll be uncommenting it later imagine this is my uh you know this is how my URL username and password and the driver class has been defined now what I want now I want this uh this should be the prefix for this class called data source properties not spring. data source rather it should be spring. datas source. myql so I cannot go to here and can bang this particular class because this is a third party class okay this is given by spring so how to change this up for this we have to use this at theate configuration properties in other way okay to achieve this what I'll be doing I'll be going to my root package and I'll be creating a uh new package uh to have my configuration and let's say I'll be having a config package over here inside my config package let's just create a Java class and this is going to be the class where where I'll be configuring my data source related information so this is going to going to be a configuration file so I'll be writing here my SQL uh MySQL uh data source configuration so this is where I'll be configuring my MySQL data source so in this file I'll be creating the MySQL data source by myself so the first thing that I'll be doing I'll be mapping all these properties that I have written over here okay username password and whatever to this data source properties right so this data source properties is a class that I want to use to that I'll be using and I'll be going to my data source configuration file and I'll be creating a bean bean for data source properties right here so data source properties right over here and right now I'll be writing my SQL data source properties just give any random name here and right here what I'll be doing I'll be returning the new instance of this data source properties right and here this is going to be a bean that I'll be creating right now and on top of this Bean right now I'll be writing that same annotation called configuration properties and using this configuration properties annotation right now I can change the prefix right over here so now what is my new prefix so here for my my SQL this is going to be my new prefix so copy this and go to your data source configuration file and paste it over here now you are ready now what you have done here using this configuration properties annotation uh you can basically change the third party Library if you have any for example the data source properties class is written by Spring not by you it's a third party class for this you have changed the prefix of the configuration properties to spring. datas source. myql now uh whenever you are writing spring. datas source. myql right now it meant for the data source properties class now you'll be able to access and set the variable for all this uh you know properties that you have inside that particular class and all the password username and all this field will be assigned with this value that you have specified over here okay so now here our data source properties is right now initialized is variable by reading the data from this prefix that we have defined in our application. properties now let's just utilize those values to create our data source now I want to create the data source by myself so I I I want the data source object to be created but I just want to customize it it will not be created right now automatically because right now we have changed the variable format right over here the way we are defining our data source related information so the autoc configuration will back off so now we need to create the data source by ourself so let's just go to our MySQL data source configuration and let's just create a data source Bean over here so this is how uh the MySQL connection we will get we'll get a connection to our database using this uh information that we'll be writing over here so data source and I'll be saying my MySQL uh data source uh MySQL data source this will be the actual data source object that we'll be creating and we'll be returning a data source object right now for now I'm making it null but anyhow I'll be returning a data source object I'm writing here public here and obviously I'll be creating a data source Bean over here but to create the data source bean I just need the URL the username the password and everything so all those things has been initialized inside this class and all this values right now has been filled in over here with the previous thing that we have done over here if I'll go back so here these things has been taken care so for an example I can create a driver manager data source this is how we we create a data source object in spring so I'll write driver manager data source I can use this object I can create a data source and is equal to new driver manager data source and I can return this data source object over here and inside this data source I can configure data source. set username and I can set the data source. set uh password okay and I can set the other thing let's say data source. set URL and also I can set the data source do set the driver class name okay now from where we'll get the values for the username we can get the values from this data source properties class right so right now I can use this method MySQL data source properties method right over here this will get me the object for the data source properties right and I can just do MySQL data source properties. getet username so right now if this thing worked then obviously the values has been raid from the properties file the username over here has been assigned to the value of this data source properties username so whenever I'll be getting the username right now over here this will get me the username from the properties file right so similarly I can use this function over here right here and I can just do dot get password okay and I can use MySQL data source properties. getet URL and MySQL data source properties dot get driver class name okay so that's pretty much it so we have created our our data source over here so we have taken the help from the driver manager data source and we have set the username and password and URL and Driver class name from reading it from the data source properties that we have set over here and mapped over here so I think things are looking good let's just go ahead and run this and let's just see whether this is working so let's just get started with the running process and looks like there is an error and it still says the data source URL attribute is not not specified looks like our data source is not working why why our configuration is not picked up because we have not marked this class with at theate configuration that's why spring is not reading this configuration file and not creating the Bean for this but now I think it should be fixed because I have written etherate configuration so things should be good so if I'll be running this application right now one more time so I think my application should bootstrap without an error and looks like it does and there is not there is no error over here so I'm sure that if I'll go to my main application I'm sure that this particular couple of lines should have been added and my data should be seted can I change it to number two and can I just say a watch and I'll just say it is uh it cost 10,000 rupees and if I'll be starting this just to make sure the things are working fine and this data is checked into the database uh with the custom data source related configuration that I have done over here so just want to check that let's just go to the you know uh MySQL workbench and let's just run our uh you know product query and there we go so we have the details over here so I successfully able to map my data source related information right over here and this is how I'm creating my data source this is the MySQL data source and that's how I'm checking in the data to the MySQL database but guys I will also give you a quick uh tip instead of by setting this usern name and password the URL and the driver all by yourself what you can do this data source properties that you have uh mapped over here all your properties over here you have mapped to the data source properties uh class over here so you can use this data source properties class so this function Returns the data source properties class so you can use this method which Returns the data source properties and I think it has a method called initialized data source Builder which does the same thing that you have written by itself internally so I can do the initialized data source Builder method you can call this and you can call build method okay and this will also help you to create a data source you don't have to be messed off with all those lines of code that you have written by yourself so if you click on this initialized data source Builder method you can see inside this a it is calling some create method here but if I will if I'll show you over here it is setting the type it is setting the driver class name it is setting the URL it is setting the username and password by all by itself internally if you click on this dot driver class name if you click on that and internally it has some method but if you click on this this do driver class name see this is the same property inside the data source properties so I think this is a better thing to do because inside uh inside your MySQL data source the one that you are specifying if you are calling this intialized data source Builder method internally it just set everything just like the driver class name it is setting the URL over here can you click on the determine URL method over here and internally it is just checking if the URL has a value uh so that it can set it off so if you click on this. URL it is the same URL value that we have defined inside the data source properties and obviously inside the data source properties the URL value already been set with the method that you have written over here okay so you can call this initialized uh data source Builder method which will internally set the password the URL the username and everything uh by reading it from the data source properties class so this is a more convenient way to initialize your data source and this is where we are initializing our MySQL data source right [Music] so now this is really good here we are configuring the MySQL data source over here now I want to similarly configure the postr SQL data source over here as well so I do not have to do anything right now I have already written the MySQL data source uh configuration over here I can copy the same pile and I can change this my SQL to post Grace so I'm going to say postc uh postc SQL data source configuration click on okay I'll copy the same file and here first of all I'll map all the post related properties uh over here to the data source properties so in order to do so in my application. properties I can also uncomment right now all this code and this is all my PG URL PG username which stands for my postc so I'll copy the prefix this will be my prefix I'll go to my postc SQL data source configuration I'll replace the configuration properties to this so right now uh the data source properties will be initialized with all the values which will be raid from the p uh from this prefix the username the password the driver class name everything is related to The Post Grace right over here these things okay the post Grace driver this is the post gra URL and everything will be configured right now to the data source properties and using the data source properties right over here right now I'll be initializing the data source over here and I'll be creating a connection to the postc database so I'll change the uh name over here from my SQL data source to postc uh postc SQL data source okay um and uh that's it so right now I have created a connection to my postc SQL now if I'll run this application what will happen let's just see I'm sure that we are going to end it off with an error and you guessed it while let's just see whether we are having an error or not and here we have an error and what it says the b in MySQL data source properties defined in this path what it is saying consider remaining one of the Bean or enabling overriding setting the overriding true so what it is saying the bean my SQL data source properties defined in the path postra SQL data source properties uh consider remaining consider renaming one of the bean oh okay okay I'm I'm sorry inside the post gr SQL uh configuration this B name we haven't changed so this is my post Grace data source properties okay we have already a Bean available in the other file inside the MySQL file with the same name so that's why it is uh saying that okay you cannot have two beans with the same name so I'm I'm going to rename it to post cre data source properties so that's that's that's it so and this uh method name is going to be this post Grace uh data source properties and I have to initialize the data source Builder from this and I have to build it and now let's just run it but still I feel there will be a problem so let's just run this now because right now we have the MySQL data source and the postc data source and I think there will be a problem now the product Dao inside the springbot MDS application inside my springbot MDS application we have the product Dao so what it says required a bean name called entity manager that could not be found consider renaming The Entity manager Factory so what is this error uh is saying is that so whenever spring boot does some Auto configuration internally um it looks an entity manager okay the product Dao basically you know it it basically helps you to you know uh to give you those luxury to have those methods like save update or and all the automatically created method and internally it needs some entity manager to you know uh to save a product so whenever you do save or something The Entity manager or the session Factory uh basically gets involved to create a session or to create a connection to the database right so internally all this configuration spring board does it by itself so whenever it does this thing it's basically need the data source but right now we also have the data source over here so spring board can take this data source that we have defined the myql data source or we have also defined the postd SQL data source over here it can take one of this data source and can proceed further to create a session Factory and to a connection to the database but why it is not happening because uh internally spring boot needs one data source right right now we have two data source here we have post grael data source we have my my SQL data my my SQL data source over here as well so we we we need to give springbot right now one data one data source we will be fixing it later because right now the internal spring boot aut configuration need one data source to proceed further so let's just go ahead and here I'm going to be making this uh postc data source primary okay let's just see whether right now our application starts or not and also let me just write some log over here so I'm I'm writing s out over here let's say uh starting inserting the data okay and let's copy this line and and I'll be saying insertion completed okay something like that I'm going to be saying insertion completed completed okay so now this is done right so let's just run this right now we made our postr SQL data source as the primary data source so I feel the spring boot is right now going to insert my data to the postc SQL database so can I go to my MDS application and let me just uh have a record over here called watch and let's say ring and the price is let's say 50,000 or something like that okay let's just run this okay and I'm sure that this product should go to the postra database uh let's just see yeah it's just saying starting inserting the data and the insertion is completed uh there is no error right now that entity manager error is gone because the entity manager got one of the data source to proceed and right now we have given the data source for post CR so the entity manager would have created a connection for the Post data uh base and it should have inserted the data over there so let me just go to the postc database and this is our MDS database T Test this this is where we linked into to in the URL and if I'll refresh my table do I have a table called Product yes I do have can I do a query over here okay can I just do a select star from product right here is products right contrl a and let's just run it and there you go ring and the price is this and this is the ID for that so this is good so now what will happen if I'll go over here to my MySQL data source and I'll make this MySQL data source as a primary right right here okay and if I'll go to the Post gra SQL I'll remove the primary from the post gra SQL data source right now my MySQL data source is the primary okay so now if I'll run this then that uh the the table will be created inside the my SQL and the data will be checked in inside the my SQL so let's just go and let's just look that look look for that and here the table was already there I have not deleted it but it will not create the table over here let's say if I if I'll remove the table let me just remove the table from um because this is previously created this is the postra one and let's just go to the MySQL one uh where is the MySQL this is where it is let me also drop this table so let's just go here and drop the table okay right now we do not have any table here inside the my security workbench and we do not have any table here inside the postc okay so now let's just go to our um you know intell and run our application and let's just see where our product table will be created and where our data will be saved right let just go here okay now let's just go to the Post Grace one okay if I'll do a refres here I don't think we'll be having anything because uh the data source that we are using is for the mql one and let just come to the my SQL and let's just do a rephrase and there you go we have the product table if we'll query it and there you go there there goes the data okay so now we have two data source but we are making uh making one work at a time but that is not our goal but uh because the reason is that we may have more number of entities right now for an example right now the goal here is like product entity we might have entity called order entity for an example if I'll go here uh to the package and if I'll create a package for order entity okay and if I will create a new class inside it if I'll go here and create a new Java class called order and inside the order imagine this is an entity this is a table for example I'll write entity here and let's say I want to create a table for order and the name for this is going to be orders and and uh let's say we'll have private in order ID and private string let's say uh from whom the order is coming from let's say order from okay and let's say another variable let's say private date let's say the date when the order is placed let's say order uh let's say the date Okay order date or something okay uh let's say we have this thing we are making this ID as primary and right now uh private okay date we have to import or maybe I'm going to take local date over here for the order entity local date okay and there you go so we have all this thing now I will create the Constructor for the same uh same as same as our product entity and also I'll create a default Constructor okay and we we have the default Constructor over here and now we'll create the getter and Setter for all our variables for order like this okay we have all our gets and Setters here now let's say this is our order entity right now and we may have a order repository as well in order to save order data for an example just like our product repository we'll create a new package for order repository okay and right over here in inside the order repository we might create a class for order Dao in order to do some data manipulation for our order and right over here inside the order Dao we might need to extend into to the crow repository and uh in order to get the crot feature for our order entity so we need to specify our order entity and we have to define the um you know the uh the type for that and obviously I'm I'm sorry this should be an interface so I'm changing the order Dow as an interface okay and this is our order entity and this is our primary key order ID that's why I have taken uh an integer over there okay so now now the thing is that if I'll go to the springboard application the main application and if I'll have a variable for order Dao so if I'll write private order order Dao order Doo and if I'll be Auto wearing it so implementation will be created on the Fly uh you know during the run time and if I'll take this order Dao in order to save an order right over here like this uh order let's say order Dao Dove and I want to save an order over here so let's just create a order so order order is equal to New Order off and let's say I'll be creating uh let me just import this order okay and uh right now let's say the order uh ID is 101 and the order came from Vash paniki and let's say the order date uh created is local date dot now okay so let's say this is when the uh the order has been created now I will be saving this order over here using the order down now now look at the problem now if I will be relaunching my application where will my data will go so now what is the data source I'm using now in my MySQL data source configuration you can see this my MySQL data source uh data source is being used as the primary data source so I'm sure both of the table order and um uh and product both of the things that we are saving over here Order and product both of this thing will landed off in my mySQL database so if I will come over here and if I will right now refresh this um you know uh MDS you can see I have the order table created and there goes my order data 101 ofas in the date and the product table created is also over here and there goes my data for the products but I do not want that right now if I will go ahead and I will change the data so and I'll change the data source to the post gra SQL right over here and if I'll make this being primary the post gra SQL data source primary then the data will be landed off inside my postc SQL but I do not need that right now what I want my order table should be created inside the postc and the data should go to the postc order table whenever I am checking in the data like this okay and whenever I'm checking in the data to my uh product table it should go it should go to the mySQL database this should be the client requirement so this should send the data to my SQL and this should send the data to postc uh SQL okay so this is something that we have to achieve right now and this is not going to be achieved by by making one of the data sources primary uh so we need to override uh The Entity manager configuration right now Now by ourself there something that we have to do right now let's just get into that guys it is 11:25 right now uh I think I have to sleep so we'll be restarting tomorrow right from here so stay tuned and do subscribe [Music] [Music] all right so so far in our application we have two data source configured but we are actually not using it at a time so one data source we are making it primary and checking in the data uh to that particular database so uh in this case so far in our project my product Dow the order Dow basically the product Dow represents the product entity the order Dow represents the order entity whenever we are saving a product over here or a order over here they both are going to the same database but we do not want that we want the product to go to the mySQL database and the order to go to the uh postc database so how can we fix this up right now so it's going to be very simple okay so why I'm saying this is simple because think okay whenever you are checking in data to a particular database what do you want you want obviously a connection okay and obviously this to create this connection we need the data source which is which is already configured now we need another thing in hibernate or jpa what is that thing that thing is called session or session Factory in hiate or entity manager in JPM okay so entity manager or session Factory these are both similar thing entity manager is JP thing and session Factory is the H thing entity manager is a uh interface and session Factory you can think like you know this is some kind of uh implementation because jpa is the like you know interface and hibernate is the implementation you think like that spring boot uses hibernate behind the scene so uh this data source that we are configuring right now is being used by The Entity manager or the session Factory behind the scene in order to connect to the database so the data source that we are configuring is used by The Entity manager behind the scenes see this entity manager is right now being autoconfigured for us by the spring Boot autoconfiguration and the entity manager obviously picking off the data source that we are using in this case uh we have made our I think um which data source we have made um primary uh I think the ml one there you go okay so in this case uh The Entity manager is basically using the MySQL data source and creating a connection to the MySQL data source so whenever we are saving our entity is all going to the you know the myql database so right now what we supposed to do here uh let's customize the entity manager right now let let's not depend on the order configuration of entity manager right now rather we will create the entity manager we will say okay uh we will create a entity manager which will establish a connection for us to the my secq database we will create entity manager which will establish the connection to a post database right so we will create couple of entity manager which will responsible to connect to couple of different databases so we can tell the entity manager a that hey you are responsible to uh to to manage these much of entities like product entity and entity manager B you are going to connect to the postgress database and you will be managing these many entities like order entity and this is something that we're going to be doing right now it's pretty simple so let's just go ahead and create a configuration for our entity manager a so uh let's say this entity manager is going to responsible to use the MySQL data source and create a connection uh to the uh mySQL database so I'm going to say MySQL entity manager configuration okay I think uh this sounds better or you can say um MySQL jpa configuration or something like or know database configuration something like we can write MySQL jpa configuration and here in this MySQL jpa configuration first of all we need to make it a configuration and inside this configuration file configuration and inside this configuration file we will be creating a entity manager well so now let's go ahead and create a entity manager so how can we create a entity manager obviously we can create a entity manager from a entity manager Factory something similar to the hiet s session object how can you create a session from the session Factory and how can you create a entity manager Factory in Spring boot uh it's pretty simple I think the object name is local container entity manager Factory Bean or something let's just see entity manager entity manager uh Factory Bean local container entity manager Factory Bean this is the one so this is basically going to be the entity manager Factory which is going to get us the entity manager object right so uh here we are basically taking the entity manager Factory to set up entity manager for our application so now uh local container entity manager Factory bean and I'm going to say my SQL entity manager Factory Bean this is going to be the method name and I'm going to be uh returning a new entity manager object but how can I create entity manager object right now okay to create a entity manager Factory bean I can I think I can take a help from a uh class here uh that is I think entity manager Factory Builder or something yeah entity manager Factory Builder this is going to help me to build the entity manager Factory object and this is automatically configured by springboard you don't have to create the object for this so you can simply autoare this over here inside this um inside this method because this is going to to be a bean so whenever this object will be created obviously this is a dependency if you are putting it over here inside the brackets and this will be automatically autoare so either you can put this subject over here entity manager Factory uh entity manager Factory Builder and you can Auto Weare it or you can put it directly over here no need to autoare it by yourself spring will automatically do it so I'm going to say entity manager Factory Builder so uh you you suppose not to write The autoare annotation over here it will be automatically um you know Auto weared by springboard because obviously this is a bean and obviously this is the dependency whatever you're putting inside the parameter so right now I understand this this object will be created by Spring boot so what I'm going to do right now I'll take this guy and I'm going to say entity manager Factory Builder and now I'm going to be building an entity manager object so uh okay entity manager Factory Builder is from Spring framework om. jpa package is correct so now I have to run the build method so do I have the build method I think I'm not getting the build method right now I understand why okay I'll tell you right now what is the reason so now see this entity manager Factory Builder we are creating for uh for which entities obviously this entity manager Factory Builder is going to manage few of my entities well this um entity manager Factory Builder will connect to which data database well the mySQL database right so obviously I need to configure the data source with this entity manager entity manager because the entity manager will use this data source uh information in order to connect uh to the mySQL database so I'll be writing the data source and I will be um I will be giving the data source related information over here so I will be taking a data source over here right now data source data source as I have told you if if I put anything over here this is a dependency so when this object will be created it will look for the dependency of data source and will be injecting it right now do we have a data source object available obviously but there is a problem we don't have one data source object available we have two data source object available this is a post data source uh object and this is a MySQL data source object so my secq data source object is primary right now but this doesn't matter right now we have two data sources available so we need to make sure whenever we are going to inject the data source over here we need to tell like which data source we want to use because we have two beans available now the way we can tell it over here as I have told you if you put it over here it will be automatically Auto Weare um the object is already there one of the object will be injected but which object uh I want to inject which data source object I want to inject how can I specify in Spring I can use qualifier and inside this qualifier I have to give the B name name so here uh this entity manager Factory Bean I'm creating for MySQL so this is the MySQL jpa configuration that I'm doing so I'll go to my my SQL data source configuration and this is going to be the bean name if you want to change the bean name over here it can give the space and can write the name and can write anything over here but right now uh as I am not specifying the name this is the name of my bean so copy this and go to the jpa configuration and over here in the qualifier for the data source give the um you know data source so uh the MySQL data source will be injected to this now copy this guy this data source over here and give the data source reference to this and now just do a build and there you go now you will be getting the local container entity manager Factory Bean so your entity manager is going to be ready which is going to use this data source connection to connect to the mySQL database now uh one more thing that I want to do over here now let me bring it down now this MySQL um jpa configuration is the configuration that we're doing obviously it is going to use this entity manager Factory Bean but this entity manager is going to connect to the mySQL database but what kind of entity it is going to manage now I want the my SQL to manage my product related entities okay so here I am specifying my product related entities I have segregated it uh with a different package so all my product related entities I'll be putting it inside this package so I'll go here and I'll copy the path reference here okay I'll copy this um absolute path uh well I just want to copy the complete package um you know reference this one com. selenium Express whatever my package name do product entity all these entities or all these classes that I have inside this package they all will be managed by this entity manager that I'm creating so the way I can say it I can just two do packages to scan I'm going to give the packages over here and I'll be putting this package name over here so by doing so I'm saying uh this entity manager is going to responsible to manage all these entities that we have so everything that we'll be keeping inside this package those entities will be created inside the MySQL database so entity has a physical presentation in the database so whatever entities we have they have the table representation in the database so whatever entities object that you will be creating uh like this product those table will be created inside my SQL right now that's what we are instructing over here so there you go we are done I think and it's uh pretty much um we're pretty much okay with this so uh that's it so right now I let's just taste it out a little bit okay so right now I think um if I will go to my mySQL database where it is this is post Grace database do I have any table here did I delete everything can I just do a refresh uh I think I do not have any table here do I have any table in the myql yes let me just drop everything I just want to make sure that we do not have any tables either in my MySQL workbench or in my post CR so that we can taste it out right now so we do not have any tables here right so now what I want to do here uh if I will go to my intell so we have our entity manager set up right now so I'm pretty sure right now as I have defined the packages as product entity now only the product entity will be created in my SQL right now even though I am uh basically storing uh the uh order Dao in my main application Ed can see um I'm uh basically uh doing Auto wearing of the order Dao and saving it over here even though I'm doing it this will not go to the mySQL database because right now the product entity these package entities only will be managed inside the entity manager okay this is the first thing that you have to understand but right now let me just go to the MySQL jpa configuration and let's just try to run our application let's just see what is going to happen I'm sure that we'll be having some issue it is not going to work what I'm expecting that only the product entity will be created inside the mySQL database right now because we have specified this but there are some problem okay so let's us see what is the problem right now so my my product data has been checked out inside the myql or not I think it is not because there is an problem here when we are starting this application and it is saying that um the recorded BM entity manager could not be found but why is that right right now we are defining our own entity manager over here so I'm expecting that the autoconfiguration of the entity manager will back off and our entity manager will be picked up but it is not picking off it the reason is that behind the scene the spring boot has a lot of autoconfiguration classes which look for the bead name called entity manager Factory so what you have to do here you have to make sure your entity manager Factory the bean name should be this so you should name your Bean as entity manager Factory or you can change your bean name as entity manager Factory like this okay like this because the spring boot will look for this particular object behind the scene or if you are not doing it or if you are not doing it whatever you can take xyg name for your entity manager Factory but over here inside the configuration it can write something called enable um enable jpa repository annotation and in inside this you have to write the entity manager reference and give whatever name that you are taking over here so there you go so that's our entity manager reference right now okay now let's just start our application I'm sure that we will not be having this exception called entity manager Factory being not found because we are creating our entity manager Factory over here and this uh this will manage all my product entity and it should store my product entity inside the mySQL database and it should only manage the product entity but again there is another problem and the problem is what okay now the problem is failed the product Dao in our springbot MDS application in our main springbot MDS application it is failed to initialize this uh which Dao product Dao right now it is not finding the object for this okay and um the problem is the problem is right now Now product Doo that could not be found okay and and that's why the injection the autoare injection that we are doing for the product Dao is not working now what is the issue over here why it is right now not finding the Bean for product Dao why spring is not creating the Bean for product Dao and why the injection is not happening because once the injection happens over here we will be able to do a save here okay product da. Save Right now the save method comes from the implementation of the product Dao that will be given by Spring in the runtime but right now as I have told you the product Dao right now internally implements to the repository interface uh the injection should happen automatically because spring will detect okay the product Dao internally implements the repository so it will create the proxy for the product Dao and give all the implementation of save sa all find by ID and everything so that we can use it based on our entity that we are specifying but it is not happening the proxy is not getting injected there is a reason for that because right now if we'll go to our MySQL data source configuration right here okay uh MySQL jpa configuration by the way where we are specifying our uh entity manager here is a problem now here we are saying enable jpa repository previously this enable jpa repository was activated by default by the spring boot aut configuration and it is scanning all your Dow which which extends to the repository interface or the crow repository interface and behind the scene it is creating the proxy for you but as you are overriding it right now the enable in the jpa repository method you have to say which are the pro which are the repository classes for which the spring need to generate the proxy or the implementation for the Dow now you have to specify it like this so you have to write something called uh what is that um I don't remember it uh I think uh packages to scan or like base packages base packages I think this is the this is the one base packages you have to specify the packages over here for which The Entity and uh for which the repository implementation we need so right now for MySQL we are keeping all our uh my SQL will take care of our all our product related entities so here this entity man manager that we are creating it should take care of all this product related Dow and it should create the implementation for this because this is all our repository classes so I'll take this package again so I'll copy the path reference and I'll copy the path reference of the project and I will go over there and I'll put it inside the Bas packages and this looks good now we have this thing right now and I think uh this is this is looking good now let's just run this now I think for the product Dao over here okay the implementation will be generated by Spring and we should not have this exception called Product Doo that could not be found uh the autoare cannot happen because the proxy for the product Doo is not created uh so let's us see that we should not have this error at least let's just run this so right now what is the next error okay oh we have another error right now it is saying order Dao is right now failed why is the issue the issue is right now again in our main application we are also Auto wearing the order Doo right now it is saying the order Dao also required a bean type okay that could not be found and it is saying that the autoare cannot happen for the order Dao well the order Dao for this we we need to save it inside the um pogress SQL right and for the Post SQL we need to create a different entity manager which will take care of the order Dao right now for now we can come in this order Dao we are not creating the proxy for it through this entity manager that we are configuring right here inside my my SQL uh jpa configuration it will only scan the product repository the order Dao exists inside the order repository right here that's why the beIN for this is not going to be created because here we are not scanning it only the repository which will be there inside this package that will be scanned and the bean will be created automatically in the run time but that is not happening for the order Doo well we can commend this particular line for now for tasting okay or if you want to do it in the same shot I think you guys are experienced developer you should not have that confus confusion right now why this bean is not getting created because we are obviously the enable jpa depository is not activated for this so what we can do right now we already have our configuration file for MySQL data source sorry MySQL jpa configuration let's just create another configuration for for postc jpa configuration I'll say postc um jpa configuration I'll copy and paste this file well uh we'll do some modification over here first of all this will be uh my this will be post gra post Grace uh entity manager Factory Bean uh this is the bean it will manage and uh the data source here obviously you know uh this the it it will be needing a postra data so this entity manager Factory Bean we are creating for post race right now so obviously uh we will be needing to connect to the post Grace database so we'll be needing the connection information or the data source information so here I will be going to the postc um post gra uh which one postc data source configuration right over here I'll take the B name right here I'll go to the jpa configuration of the postc right here and right now I will be specifying the qualifier as this okay and now the next thing The Entity manager reference right now I'll be changing over here and I'll be saying it as postra entity manager Factory beam so this looks good right now can I just write G as small here so that I'll not have that uh Waring Mark okay uh this looks good right this is the entity name and I'm putting it inside the entity manager Factory reference okay the only thing that we have to change right now the packages over here this entity manager that we are creating is specific for the Post uh post gra so it will uh it will only manage us the entities which are the order related entities right now the post Grace will save the order related entities so right here in the order entity I will copy the reference here okay copy the reference and put it inside the package right now so only the order related entities will be managed and will be created inside the post Trace uh so this is the packages that will be scanned and those entity will be created and another thing that I want to do this jpa repository I want to enable I want to create the proxy for the jpa repository for the order repository uh this order da or implementation I want spring to create and this entity manager will be uh you know uh responsible for this so the postc um GPA configuration over here the base packages I will change to order repository so right now the and jpa repositories are activated so whatever the repository you are keeping over here by by extending that uh croud repository or repository interface those Bean will be created behind the scene because you are creating the interface the Bean or the proxy Bean will be created by the spring and this guy will do that work for you so now the things are good and it's ready so right now I think we have two entity managers specified let let me just close the rest of the thing let me just close uh just close all the tabs okay now we have the MySQL jpa configuration right here we have the postc jpa configuration right over here and this is our main application right here okay so now now I think uh over here in the postest jpa configuration uh the order repository will be scanned so over here whenever we are Auto wearing the order Dao that object we will get because spring will create that for us and same for the product Dao because the MySQL jpa configuration it is taking care of the product related things and obviously it will create the proxy Bean for the product repository and things are good right now so and the uh the myql jpa configuration will manage the product entity so the product table will be created inside MySQL and the postris jpa related um and the post CH jpa configuration will basically maintain the order entity so the order table will created inside the post crit so let's just see whether that is happening or not let's just run our application just to remind you right here in our MDS schema of our myql workbench we do not have any table here as you can see we do not have any table inside it so I'm expecting only the product table will come to the MySQL whereas um the the order table will go to here and as you can see over here also if I'll do a refresh we do not have any table exist inside the MD schema of our postra admin okay so now the product related table will come over here this is the previous output so don't see that we do not have any table right over here right now okay cool let's just go ahead and run our application and let's just see whether whenever we are checking in our data over here uh these things are going to save right now or not let me just write ring 123 and AAS pan2 three just to have some phrase data and let me run this but I'm sure it is not going to work right now it is not not going to check in the data but what is the issue the transaction management issue I think we'll get what is this right now uh consider making one of this being hey this is a different issue what is this parameter J of the open entity manager in view Interceptor configuration it is asking that two beans are found one is called MySQL entity manager Factory Bean one is post entity manager Factory Bean there are two entity manager Factory Bean make one of the bean primary but it is not needed I'll tell you why you are getting this exception and don't freak out whenever you'll get this exception and for this reason only I have added that spring web project you remember inside the project dependency in the beginning I have added a dependency and I told you it is not needed but I'm adding it because I'll show you something and that is the only reason why I've added this boot starter uh web um dependency so we are not creating a web application right now it's a standard learn application there is no request in coming to our server or something but the thing is uh the reason why I have added this web uh dependency because I want you to show this uh exception so this will be um you'll be basically you know uh you'll be you'll be pretty much tensed whenever you'll be working in a real-time project and we'll see this exception called open entity manager in view uh Focus over here in view Interceptor configure so why this exception is coming this is a design that spring people has done behind the scene the in view what we say this open in view design pattern there is some design pattern they have done we don't give a damn about it and you also don't give a damn about it because this is a this is not a pretty good design pattern for the uh in know production grade application so this is something they have implemented already so it's basically uh basically relates to the um like you know incoming request that will come to your server and once the once the request comes to your server then it goes through some layers like some first the session Factory will be created then the flow will go to the dispatcher shate then the dispatcher from there to flow will go to the controller then the service then the Dow the way it goes um you know it also this guy this is a uh you know uh component that the spring people has configured behind the scene and this component or this method uh behind the scene requires the entity manager Factory to proceed further so that's why it is asking you uh to configure one of your entity manager either the MySQL entity manager Factory Bean or the postra um entity manager Factory Bean one of this entity manager it is asking you to make the primary in order to proceed further because of the auto configuration they have done uh behind the scene but this is not a good approach uh to have this design pattern in in a produ production grade application that's why I'm going to turn this off and you will also do the same in your real time application so what I'm going to do I'm going to look for a springboard property right here and that's what we call uh I think open in view this one okay and turn this off right by default this is true now you go to your Intellis and um go to the application properties and turn this off okay and I'm going to say false right by default it is true so right now I'm just turning up this opening view uh design pattern that that that they have implemented behind the scene which needs one of the entity manager Bean to proceed further so I'm just stopping it okay now let's just run this let's just back into our business let's just see whether our data is getting checked into the databases that we want to okay one product thing should go to the myql one order things should go to the Post Let's us see whether that is happening and it is not happening again let just see what is the issue and right now it is saying error creating a be name called transaction manager defined inside the class path resource and this is um this is something uh that we have to care about right now that I was saying it is saying that pil to create a transaction manager obviously when we check in data to the databases where where we are checking in data right over here here we are inserting the product data here we are inserting the order data we are saving the data over here but if something goes wrong we have to revert back the data so you can learn about the transaction management more in my transaction management series uh lot of videos are also free on YouTube uh so if you already have the paid course access you can also see hundreds of bit not hundreds at least 10 20 videos and transaction manager over there in the paid course and um over here in the YouTube also you'll find a lot of about the transaction management but the main thing is here whenever we are manipulating data we need to uh we need to manipulate the data under a transaction so if something goes wrong uh in a unit of work whenever we're doing number of works that we're doing if something goes wrong we have to roll back the transaction so we do not ended up uh you know checking in some uh you know um like you know some data that we do not want to check in right so basically right now the same methods are falling under transaction but right now this data we are going to commit in the myql database and this data we want to commit inside the post Grace database right so we need to also specify the transaction manager previously only one transaction manager is there right now we have to create two transaction manager for each of our entity manager or session Factory so I will go to the my SQ jpa configuration first of all and I'll create a transaction manager okay so right here for the SQL I will create a uh Bean for transaction manager so transaction I think it's platform transaction manager platform transaction manager transaction manager um this is the MySQL transaction manager by the way okay so I'm going to say MySQL transaction manager okay and um and I will be creating the Bean for the transaction manager so now this platform transaction manager this is an interface but we have an implementation for this interface right so we need to go for there are two two uh implementation as you can see here I think they have written uh one is called JTA transaction manager is the implementation for this and one more um transaction manager we have called jpa transaction manager well let's not go to this uh documentation because I do not want you to crash your head uh just remember this platform transaction manager has two implementation one is called JTA transaction manager we'll talk about that later uh maybe you can find what it does in my other videos one is called jpa transaction manager so for this interface I will be using the jpa transaction manager right now so what I'll be doing I'll be returning the implementation class for this interface called new uh jpa uh transaction manager okay this one I think jpa transaction manager right so right over here if you going to go to the jpa transaction manager okay if you're going to go to the class of the jpa transaction manager uh it will see behind the scene it extends to the platform transaction manager over here okay behind the scene is implements to that so the jpa transaction manager is a implementation for the trans transaction manager and that's what we are creating a bean for this transaction manager Now to create the transaction manager this transaction manager right now will work for with session Factory now this transaction manager we are creating for our for our session Factory or for our entity manager which is my SQL entity manager okay which basically connects to this data source and this is your MySQL data source so now what I will do I'll create a entity man manager Factory um what is that entity uh local entity manager Factory or whatever that object okay this object I will create a dependency over here I'll say entity manager Factory Bean okay and this is your entity manager Factory Bean that the obviously this is a dependency for this bean and this object you have already created over here as a bean so whenever this Bean will be created it will look for the dependency for this bean and obviously this is created so I'm getting a hold of this and I'll give this entity manager Factory or this entity manager object to here but the jpa transaction manager needs a entity manager uh entity manager object so but this is a factory Bean okay so from this Factory I will just get the object for the entity manager right so from this I'll just do a get object this is this will get the object of the entity manager Factory from this uh entity manager Factory Bean okay and that's what I'll be giving it to the jpa transaction manager that's it so now I have the MySQL transaction manager ready now I will uh so by default um the spring again look for a bean called you can see called transaction manager but the bean that I have created is called MySQL transaction manager so what you can do you can copy this and you can just say it over here inside the enable GP repository the transaction manager reference is this MySQL transaction manager okay just specify it over here okay that's it so you are done and just do the same thing for the Post C post Grace where where are you post gra safia configuration right over here also we need to create a transaction manager uh and uh I will change the name to postc transaction manager okay and um I will make sure that this is going to be the reference that we have for the transaction manager so transaction manager reference and I need to put it over here okay there you go we have defined the transaction manager over here one transaction manager for the Post gra and it will work with this entity manager Factory Bean which basically connects to the postc database and we have done the similar thing inside the SQL over here as well okay so things are good so now let's just go to our main application and just run it and this should run right now because we have our transaction manager specified but I think still we'll have an error oh we had an error right what is that uh is just saying the same thing here let's just see it is just saying uh my SQL okay parameter zero of the my SQL transaction manager required a single Bean but two were found what is this uh this may be missing the parameter name information consider marking one of the bean as primary uh oh we don't have to do that I think why this is happen happening uh the qualifier we have to use it is saying oh okay so let's just see over here uh let's just go to the MySQL jpa configuration okay uh the in the my SQL uh so it is saying we have two entity manager Factory Bean so we need to use the qualifier over here and it is asking me for the M SQL okay this is a my SQL for the M SQL we need to use this um the transaction manager will use this entity manager Factory be over here okay and we'll do the same for the Post gra so go to the Post gra and this entity manager Factory being that the transaction manager will transaction manager will internally look for The Entity manager Factory and it is looking for uh The Entity manager Factory but it is finding two so we we are going to say that okay the qualifier this transaction manager the post transaction manager will internally use an entity manager Factory and that should be the postc entity manager Factory only so I'm specifying through a qualifier here uh for both of this um transaction manager that I have defined over here and over here as well okay now let's us go to our main application right now and let's just run it and let's just see whether whether everything is working fine or not not come on man looks like something happened oh we still have an error what is that error right now I think I I I guessed the error I think I have missed something and it is saying what okay it is just saying what what what jdbc it is saying uh right now everything is working but jdbc exception we are having uh for the cwell for the select query it is finding it is saying the product does not exist I think what is happening inside the save method right now here whenever we are doing save right here whenever we are saving the order and saving the product over here product over here order so it is saying the table does not exist so it is finding uh it is finding a select query obviously I told you because here we are um basically creating the product by giving an ID so before saving the save method faes p say select query internally in order to check whether this um uh ID already exists inside the database if exist then uh it will do an update uh if not exist it is going to fire a insert query and do an insertion so that's why it is firing a select query but that is that doesn't M doesn't matter they say behavior of the sa method but the problem is it is saying the product table does not exist okay whenever it is doing a save so I think the error and exception is happening over here so if you're going to do a debug here from here only the exception will be thrown okay why the product table does not exist uh I told already spring you will be doing uh you will be creating the uh you know ddl for me so the table creation is your part of the job but it is not doing it so maybe I need to specify another thing over here because some entity manager I have created some of the properties has been back off uh that has been uh that might have been created by Spring when I overridden it so some properties are gone so what I'll be doing here I will be right now searching for Generate SQL or something generate uh generate ddl yeah take this and and it is saying that okay whether to initialize the schema on the startup okay so can I come over here to intell and right here I'm going to give this to True okay let's just run this again okay and now so the table should be created oh yeah done so it is saying um starting inserting the data and insertion completed so now let's just test a look because right now all those hard work that we have done the MySQL part of the thing um MySQL jpa configuration right now should in insert the product entity okay so if I'll go to the my SQL right now and I'll refresh the schema here okay I have a table I hope this is not two this is one this should be the product and yes it is can I query this and ring one 2 three price and ID everything is good and the next thing that I want to do I will go to my post C here and I will do do a refresh here I should get the order Table and there you go I have the order table can I just do a select star from orders over here right now let's just do a run H orders here this errors are I'm just freaking out because this video is already 2 hour okay yeah I have the order from where it is from from the order date and the ID and everything is there over here as well so the order related entities are right now over here and um the product related entities are going to the myql right now and we are continuously able to check in the data uh that I have shown you over here uh to the yeah here and everything is working fine the auto wearing is happening good so things are good congrats cheers okay before you guys go two things I want to show you um like you know everything is done right now just for Best Practices whenever you are activating the transaction management make sure that uh you also use this annotation called at theate transaction management uh enable transaction management because you are saying to the framework that like with this transaction manager do all kind of aut configuration and and um do all kind of setup that you want to do because we want to enable the transaction management so do it over both the jpa configuration that you have inside the post gra and over here uh on the my SQL this is a this is one of the best [Music] practice before you guys go you know just for clarity I'll close everything and I'll ask you a question okay let's say close all the tabs okay let's say somebody come over here to the product entity okay and create a new Java class which will be entity let's say uh let's say product let's say I'll say imagine product okay so let's say imagine I have another entity called amazine Product okay and this is an uh entity okay and I have a table representation in my database and I'm going to be saying let's say uh um let's say imag imagine let's say uh let's say a p let's say this is going to be the table name uh which stands for Imagine product okay this is the name of the table and imagine I have couple of fields let's say private in ID private in uh product um let's say product name let's say imagine product name and let's say imagine this ID is the primary key that I have so I'm going to write uh it ID over here just to show you you and here I'm going to do a command n and I will create the Gators and Setters for the same okay so there you go so I have a another entity called imagine product and I have a table representation for this called AP now this is an entity I'm keeping it inside the product entity right now okay so right now if I'll restart my application so where do you think this Pro imagine product table will be created inside the postc or inside the myql now just ask yourself a question inside the MySQL jpa configuration which which entity it is managing look at the entity manager Factory it is managing the product entity so in the product entity package where we have right here and do we have a Amazon product entity over here yes we have so so this table will be created over over where over here inside the my SQL inside the my SQL database so if I'll go to the my SQL database right now over here uh did I run this I think I run my application if I'll do a refresh okay here you got your imagine product table right and obviously we haven't check in the data but you can create a dow and the Dow represent Dow proxy will be created and all those things will be taken care by our MySQL jpa configuration that you have done so uh but if you go to the Post gra and if you're going to do a refresh you will not find that um find that table over here that we have just created it only have the orders table so right now your configuration should work and congratulation of making it till the end and thank you very much for watching this video do not forget to subscribe guys um I hope that I'll be able to edit it because a very long video right now so thank you very much for watching make sure that you subscribe to the channel if you find this video helpful make sure to like this video guys if you're watching till now and if you are practicing everything everything is working do me a favor give me a like okay and um share this video with your teammates with your friends with your colleagues if it if you find it helpful follow me on insta follow me on Facebook my insta ID is uh this that you are seeing on the screen and uh and subscribe and take care of your health I'll I'll see you guys in the next video till then bye-bye take care and happy coding [Music]
Info
Channel: Selenium Express
Views: 9,683
Rating: undefined out of 5
Keywords: multiple databases connection using spring boot, spring boot multiple data sources example, spring boot multiple databases, spring boot tutorial, multiple datasource in spring boot, how to connect with multiple databases using spring boot, spring data jpa tutorial, spring data jpa, spring data jpa interview questions, spring data jpa with spring boot, spring boot multiple data sources, spring data jpa annotations, selenium express
Id: z65J3JPbs9A
Channel Id: undefined
Length: 121min 51sec (7311 seconds)
Published: Tue Apr 23 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.