11. Integrating Spring with Hibernate - I

19 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€67.10
List Price:  €95.86
You save:  €28.76
£55.67
List Price:  £79.54
You save:  £23.86
CA$100.49
List Price:  CA$143.56
You save:  CA$43.07
A$111.96
List Price:  A$159.95
You save:  A$47.99
S$94.87
List Price:  S$135.54
You save:  S$40.66
HK$544.16
List Price:  HK$777.41
You save:  HK$233.24
CHF 62.56
List Price:  CHF 89.37
You save:  CHF 26.81
NOK kr792.29
List Price:  NOK kr1,131.89
You save:  NOK kr339.60
DKK kr500.54
List Price:  DKK kr715.08
You save:  DKK kr214.54
NZ$123.74
List Price:  NZ$176.78
You save:  NZ$53.04
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.19
৳8,330.24
List Price:  ৳11,900.85
You save:  ৳3,570.61
₹5,945.56
List Price:  ₹8,494.03
You save:  ₹2,548.46
RM315.51
List Price:  RM450.75
You save:  RM135.24
₦108,149.19
List Price:  ₦154,505.46
You save:  ₦46,356.27
₨19,403.53
List Price:  ₨27,720.51
You save:  ₨8,316.98
฿2,393.75
List Price:  ฿3,419.79
You save:  ฿1,026.04
₺2,454.67
List Price:  ₺3,506.82
You save:  ₺1,052.15
B$425.95
List Price:  B$608.53
You save:  B$182.58
R1,282.09
List Price:  R1,831.63
You save:  R549.54
Лв131.15
List Price:  Лв187.37
You save:  Лв56.21
₩101,234.93
List Price:  ₩144,627.53
You save:  ₩43,392.60
₪254.67
List Price:  ₪363.83
You save:  ₪109.16
₱4,117.86
List Price:  ₱5,882.91
You save:  ₱1,765.05
¥10,949.58
List Price:  ¥15,642.93
You save:  ¥4,693.35
MX$1,405.49
List Price:  MX$2,007.92
You save:  MX$602.43
QR254.12
List Price:  QR363.05
You save:  QR108.92
P963.49
List Price:  P1,376.48
You save:  P412.98
KSh8,999.72
List Price:  KSh12,857.29
You save:  KSh3,857.57
E£3,561.31
List Price:  E£5,087.81
You save:  E£1,526.49
ብር8,689.79
List Price:  ብር12,414.52
You save:  ብር3,724.72
Kz64,250.82
List Price:  Kz91,790.82
You save:  Kz27,540
CLP$69,143.42
List Price:  CLP$98,780.55
You save:  CLP$29,637.13
CN¥510.67
List Price:  CN¥729.56
You save:  CN¥218.89
RD$4,244.94
List Price:  RD$6,064.47
You save:  RD$1,819.52
DA9,440.04
List Price:  DA13,486.35
You save:  DA4,046.31
FJ$162.13
List Price:  FJ$231.62
You save:  FJ$69.49
Q537.12
List Price:  Q767.35
You save:  Q230.22
GY$14,584.29
List Price:  GY$20,835.60
You save:  GY$6,251.30
ISK kr9,693.35
List Price:  ISK kr13,848.23
You save:  ISK kr4,154.88
DH701.59
List Price:  DH1,002.31
You save:  DH300.72
L1,285.64
List Price:  L1,836.70
You save:  L551.06
ден4,127.89
List Price:  ден5,897.23
You save:  ден1,769.34
MOP$558.06
List Price:  MOP$797.27
You save:  MOP$239.20
N$1,283.39
List Price:  N$1,833.49
You save:  N$550.10
C$2,565.21
List Price:  C$3,664.75
You save:  C$1,099.53
रु9,482.30
List Price:  रु13,546.73
You save:  रु4,064.42
S/259.58
List Price:  S/370.84
You save:  S/111.26
K282.68
List Price:  K403.85
You save:  K121.16
SAR262.90
List Price:  SAR375.59
You save:  SAR112.68
ZK1,929.21
List Price:  ZK2,756.13
You save:  ZK826.92
L333.95
List Price:  L477.10
You save:  L143.14
Kč1,686.22
List Price:  Kč2,408.98
You save:  Kč722.76
Ft27,781.83
List Price:  Ft39,690.03
You save:  Ft11,908.20
SEK kr772.17
List Price:  SEK kr1,103.14
You save:  SEK kr330.97
ARS$71,242.69
List Price:  ARS$101,779.64
You save:  ARS$30,536.94
Bs481.71
List Price:  Bs688.19
You save:  Bs206.47
COP$305,135.87
List Price:  COP$435,927.07
You save:  COP$130,791.20
₡35,171.10
List Price:  ₡50,246.58
You save:  ₡15,075.48
L1,769.55
List Price:  L2,528.04
You save:  L758.48
₲543,563.42
List Price:  ₲776,552.46
You save:  ₲232,989.03
$U3,109.25
List Price:  $U4,441.97
You save:  $U1,332.72
zł286.15
List Price:  zł408.81
You save:  zł122.65
Already have an account? Log In

Transcript

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.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.