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
€64.99
List Price:  €92.84
You save:  €27.85
£55.80
List Price:  £79.72
You save:  £23.91
CA$95.74
List Price:  CA$136.78
You save:  CA$41.04
A$105.97
List Price:  A$151.39
You save:  A$45.42
S$94.71
List Price:  S$135.31
You save:  S$40.59
HK$547.38
List Price:  HK$782
You save:  HK$234.62
CHF 63.50
List Price:  CHF 90.72
You save:  CHF 27.21
NOK kr760.18
List Price:  NOK kr1,086.02
You save:  NOK kr325.83
DKK kr484.74
List Price:  DKK kr692.51
You save:  DKK kr207.77
NZ$116.49
List Price:  NZ$166.43
You save:  NZ$49.93
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,679.15
List Price:  ৳10,970.69
You save:  ৳3,291.53
₹5,844.24
List Price:  ₹8,349.28
You save:  ₹2,505.03
RM331.61
List Price:  RM473.75
You save:  RM142.14
₦86,437.65
List Price:  ₦123,487.65
You save:  ₦37,050
₨19,466.20
List Price:  ₨27,810.05
You save:  ₨8,343.85
฿2,579.91
List Price:  ฿3,685.75
You save:  ฿1,105.83
₺2,258.19
List Price:  ₺3,226.13
You save:  ₺967.93
B$355.28
List Price:  B$507.56
You save:  B$152.28
R1,291.06
List Price:  R1,844.45
You save:  R553.39
Лв127.20
List Price:  Лв181.73
You save:  Лв54.52
₩95,163.27
List Price:  ₩135,953.36
You save:  ₩40,790.08
₪260.34
List Price:  ₪371.93
You save:  ₪111.59
₱4,006.12
List Price:  ₱5,723.27
You save:  ₱1,717.15
¥10,811.89
List Price:  ¥15,446.23
You save:  ¥4,634.33
MX$1,180.56
List Price:  MX$1,686.59
You save:  MX$506.03
QR255.22
List Price:  QR364.61
You save:  QR109.39
P950.05
List Price:  P1,357.27
You save:  P407.22
KSh9,308.67
List Price:  KSh13,298.67
You save:  KSh3,990
E£3,339.92
List Price:  E£4,771.52
You save:  E£1,431.60
ብር4,017.22
List Price:  ብር5,739.13
You save:  ብር1,721.91
Kz58,559.69
List Price:  Kz83,660.29
You save:  Kz25,100.60
CLP$65,083
List Price:  CLP$92,979.70
You save:  CLP$27,896.70
CN¥496.09
List Price:  CN¥708.73
You save:  CN¥212.64
RD$4,059.13
List Price:  RD$5,799
You save:  RD$1,739.87
DA9,404.13
List Price:  DA13,435.05
You save:  DA4,030.92
FJ$157.14
List Price:  FJ$224.49
You save:  FJ$67.35
Q543.86
List Price:  Q776.98
You save:  Q233.11
GY$14,638.84
List Price:  GY$20,913.53
You save:  GY$6,274.68
ISK kr9,768.50
List Price:  ISK kr13,955.60
You save:  ISK kr4,187.10
DH701.39
List Price:  DH1,002.03
You save:  DH300.64
L1,239.86
List Price:  L1,771.31
You save:  L531.44
ден4,006.46
List Price:  ден5,723.76
You save:  ден1,717.29
MOP$563.24
List Price:  MOP$804.66
You save:  MOP$241.42
N$1,288.69
List Price:  N$1,841.06
You save:  N$552.37
C$2,575.52
List Price:  C$3,679.48
You save:  C$1,103.95
रु9,351.66
List Price:  रु13,360.08
You save:  रु4,008.42
S/260.79
List Price:  S/372.58
You save:  S/111.78
K270.67
List Price:  K386.69
You save:  K116.01
SAR262.50
List Price:  SAR375.01
You save:  SAR112.51
ZK1,901.47
List Price:  ZK2,716.50
You save:  ZK815.03
L323.43
List Price:  L462.07
You save:  L138.63
Kč1,625.98
List Price:  Kč2,322.93
You save:  Kč696.95
Ft25,280.20
List Price:  Ft36,116.11
You save:  Ft10,835.91
SEK kr759.20
List Price:  SEK kr1,084.61
You save:  SEK kr325.41
ARS$61,608.98
List Price:  ARS$88,016.60
You save:  ARS$26,407.62
Bs483.53
List Price:  Bs690.78
You save:  Bs207.25
COP$273,394.26
List Price:  COP$390,579.97
You save:  COP$117,185.71
₡35,797.53
List Price:  ₡51,141.53
You save:  ₡15,343.99
L1,728.55
List Price:  L2,469.47
You save:  L740.91
₲523,227.64
List Price:  ₲747,500.10
You save:  ₲224,272.45
$U2,673.53
List Price:  $U3,819.50
You save:  $U1,145.96
zł280.44
List Price:  zł400.65
You save:  zł120.20
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.