In this session, let us learn how to integrate a spring MVC application with hibernate for database operations. Here we are going to use Spring Boot for creating the spring MVC application. Spring Boot support, integration of warum frameworks like hibernate JPA, Java Persistence API, resource management, data access, implementation and transaction strategies. I'm going to share this in two parts. In the first let us do risk editor in part two, we will be doing the real coding. Now let us see the steps to integrate hibernate with a spring MVC application.
First you need to create a spring starter project and add web pack JPA starter dependencies. You also should select my SQL connector Java, while creating the project itself, we are going to work with my SQL database. So we need my SQL connector j s t driver. Basically, spring uses JPA for persisting data. Now we are going to use hibernate or JPA provided for persisting data. You whenever you want to work with a database you pre have a video layer with a DMR interface dmo implementation wherein you will be writing the code to connect to the database.
If you are using spring both in that scenario, you can create a demo interface. You don't have to provide the implementation. The implementation will be taken care by Spring Boot if you are using JPA as the persistence provider. In our case we are going to use hibernate. So using hibernate we will be storing the data into the database tables. And we will be writing explicit coding We will be creating a separate class which implements the do layer interface and write the coding.
I will also show you how to work with JPA with creating the implementation class Okay, now, let us see an example. First thing is form dot XML then you need to add hibernate properties in the application dot properties file when you are working with JDBC all the data source related things you have added in application dot properties file. Here also you're going to do the same thing. You have to configure the internal view resolver then hibernate properties, this data source properties everything in application dot properties file that you need to create a session factory object only using session factory you can open a session and perform hibernate operations right. So for creating a session factory object, you are going to write the coding in the application class that This we are going to use an internal class which will create the session factory object using Java based configuration you are going to create a bean in the application class.
Then as usual going for a controller service and da vo layer where I'm going to work on the same application book application. Once they are completed you need to create an application similar to this using hotel hotel a C model class and again we need to create the view pages I will show you palm dot XML wherein I have added web and data JPA my SQL connector Java these two are started packages this you need to select it then adding hibernate properties in application dot properties file. See here I have configured view resolver then adding data sources later details what is the URL username password those details then hibernate the things which database you are using the same dialog You need to select and then whether you want to create a new table for each and every one or you want to work on the existing tables based on that you need to use h HBM to DDL already we have learned about that.
So that's what this this then configuring sessions actually been an application class. I have created a Beanpot session factory Can you see this entity manager factory dot unwrapped I am trying to create a session factory entity manager factory is from JPA with the help of entity manager factory we are trying to create a session factory object. Once the session factory objects is available, you will be auto wiring it in the D o implementation class with the help of that you will try to call all the methods. So here I'm showing an example of Portal da y MPL. Once you've got the session factory It is also weiden portal da y e MP. And as usual, you need to follow the steps for working with hibernate using the factory Open the session and call session dot save method.
Whatever transactions in the service layer itself you will be using another annotation called factory transactional meaning to say that it is working in a transaction that also is taken care by spring. So spring takes care of persisting the data into the database using hibernate. Now, let us do the initial steps. I will do part of the coding in part one and rest of the coding in part two. I am an eclipse let me create a new spring starter project file new spring starter project. Let me give the name of spring MVC hibernate.
I want to have the package name is comm dot vuca. Click Next. Add the dependencies gone search for it. You will be getting it web web is already there. Then JPA you go on type JPA automatically. You will be Getting it JPA then my SQL.
Why am I using JPA JPA Java Persistence API Are you are using the annotations of JPA only. But hibernate is the JPA provider internally JPA will capture all the API's that are needed to work with hibernate. I hope you can understand all the jar files that are needed to work with hibernate is available when you select JPA itself. Let me click Finish. Now the application is created. I'll open the application spring MVC hibernate first let us examine this palm dot XML.
So this is palm dot XML wherein I have got this JPA web, my SQL connector Java. Okay, I will go in for this dependency hierarchy in the dependency hierarchy. You can just see here your God spring boots start Data JPA so what it is giving me Spring Boot starter JDBC hibernate core hibernate JPA whatever is needed to work with hibernate, then hibernate entity manager, if you want to work with the annotations that are available in JPA you need the hibernate entity manager. So all that is needed to work with the database is provided by Spring Boot starter data JPA package Okay, okay this is done. Now I will close this. I will open here.
First let me just start with this. Mayor root package comm dot vuca I'll modify the application class first the application classes modified now here only we are going to create a beam for session factory that is unique to our failure entity manager factory with the help of this you will be creating a session factory. Let me just add the method at the rate beam public session factory get session factory. Inside this method we are going to create a session factory object with the help of entity manager factory. So, first I need to auto entity manager factory at the rate auto via private entity manager factory let me just do Ctrl Space shift manager factory inside this method let me just call manager factory dot unwrap session factory class. Let me add the complete coding first and then I will explain you I have completed the code Let me explain this using the entity manager factory I am trying to unwrap session factory which belongs to hibernate.
When you're driving you're unwrapping a second party you put a smile in that scenario it will throw a null pointer exception. If the factory is available, you are capturing the factory and returning it to the session factory. So, a session factory bean is created in the application class. This is the first step. Next let me create a model class. So I'm going to work with books so here SRC main Java, right click new class, the class name is book.
Click Finish, we are going to use the same book table which we have worked with you earlier. So what I need to do, I'm going to work with hibernate also together. So this class should be annotated with our current entity. And the table name is understand this I am going to work on the same table which is already available over there. If you want to create a new table, you can just give table name equal to new We'll make this case I'm going to work on the same table only scientists giving it as books. I have to import it both are from Java x dot persistence package.
Okay, this is that now inside this book class, I need to create instance variables, title, book ID, author, category and price. And Id has to be annotated with at the rate ID white because we are working with hibernate. hibernate needs a key a primary key. So that is why I'm going to annotate this ID with the red book. I mean, I don't say the book it was at the rate ID, then together that we need to create getter and setter methods and generate two strings. Let me do that code first.
The class is ready. I have added at the rate ID and generated value for book ID so that hibernate can automatically generate the ID for this book ID. Okay, this is that one more thing. If you While creating a new table, if you want to give length four columns, then you can go in for address column. If you want to give a different name for your column, again, you can just do the column name in at the rate column annotation, because we are going to work on the same table which is already over there, which is also going to have the same column name, just like the instance variables, and just leaving it with a default. Now together with that, I need to add a component annotation because I want spring framework to create the object.
So let me add a direct component. So this is about the model class. This is completed next, even before going into the back end, but we will complete this book controller part together with the view pages. I'm now creating the controller book controller. The first step is annotate the class with at the rate controllers. Then I need to have a homepage right so first let me create the homepage in the home page, I'm going to add three links for adding a book to the table for updating the book and for deleting the book First I will add that flow and then we will discuss I have added to methods in the controller for the road mapping I have added form dot JSP in one dot JSP I am trying to give three links one for adding the fourth one for updating the fourth one for deleting the form that is we are going to see the CRUD operations to be performed the hibernate creating updating sorry creating retrieving updating and deleting the training I will take it later.
So first we will be starting with this only. Okay when I click Add from where it will go it will go to this book controller for add form I have given add book form dot JSP. I have also created this ad group from the JSP. To add a book to the database. The method is Fourth an action is adverb and here and trying to have the form fields similar to the model name book ID author price and category Okay. Now from here where it has to go again go to the book controller, let me open the book controller in the boot controller Class C for add mapping, I have given get mapping, because the default method is good.
Now, we are going to retrieve the form fields using force method. So, we will use force mapping let me add the code first I have added a method with a direct force mapping because the method is post okay what actually are we going to do within this add book method. Till now in whatever application we have seen, we were trying to get the values from the form and we were sending into the next page. But in this application, we are going to retrieve the values from the form and then call the method of book service. So, that is data in which they're stored in the database table. So, for that what we need we need book service book service I am PL ba, ba, ba ba ba MPL with few methods for adding book retrieving book by category book by author retrieving all the books update book okay and also deleting the book okay.
So, I am just leaving this off assets after the book is added to which pages are to come, it has to come to the same home dot JSP page so that I can do other operations like editing the book, updating the book like that. Okay, so here, let me first go on at the backend latest book service book service I am PL ba, ba, ba, ba ba MP. So already we have worked with this book application earlier. So I'm just going to copy the code assets when you are working with spring JDBC also we have used the same book only I am explaining with books once it is done, you need to do a project with quotes Hotel st model Now, let me add the core packages first. Now my dear one service layer is ready always remember you need to add the classes in the appropriate packages that is controller should be added in controls package service and do one layer should be separate models should have only the model classes exception classes should be an exception package validator should be in validators package, don't try to put everything in a single package it will not look good and it is not the right way to work with any application.
Now, I will open service layer so, here I have got book service with few methods adding book guitar books retrieving get book by category get book by ID update and delete book. So, that is getting implemented in book service imp. In book service I am PL I have added at the rate service annotation. Together with that we can also add other a transactional Okay, so this is from Java or not from Java it's transection it is from wor d dot spring framework. So you have to be very careful with this input. Together with this I have auto wire ba Bo.
Already we have done in JD I mean JDBC spring JDBC same application only I have copied. And now here we have added all the methods. What if you want to add logger that also we can do here Just let me add the logger private logger Naga is equal to logger factory we're going to use yes or no for now, logger factory dot get logger and we need to pass a classmate waters book service I am PL dot class. Let me import logger. Again while importing logger you need to import s and a 4g. Okay, this is done.
Now in all the methods you can just do info logger dot info Inside ad and we are in service layer client is giving it a service colon inside ad group okay. So let me just copy this logger and all the methods. Now the service implementation layer is ready with all the logger statements. This is for the service layer. Next let us move on to the Bo layer. This is my book do layer which is having the same method as that of the service layer.
Now I need to go in for this book dry MPL. In Boothbay, why MPLS yada yada, we work with JDBC template. Now we are not going to use JDBC template we are going to use session factory we are going to auto wire session factory. So first what I will do is I don't want this much of coding. I will just remove everything. Okay, the class should be annotated with that great repository.
You must remember this because we are working with hibernate. We are writing the simple limitation Plus she wants spring boat to provide the implementation. In that scenario, you don't have to write the implementation class it will be taken care of by spring completely. Now I'll just keep the cursor here I will add unimplemented methods. Now within this first what is the first step I need to walk away? What session factory private session factory session factory.
Okay, let me import it. Now in this ad group method, what is that you are just going to do first using the factory open the session and save the book object, say the snake session factory dot open session which is going to return a session object. Then session dot save book you don't have to bother About the transaction you don't have to open a transaction because it is working in a transaction only. Just like in the service layer here also we can add logger. I are added logger to add book method. Let me add a logger for all the other methods also.
And then we will go back to boot controller and try to wire all the method call all the methods of boop DIY MPLS. We need to do few more configurations like adding data so cetacean application that properties files are those we will do in the next session. Thank you.