14. Spring with JPA Repositories

17 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
€66.52
List Price:  €95.03
You save:  €28.51
£55.40
List Price:  £79.15
You save:  £23.74
CA$97.77
List Price:  CA$139.67
You save:  CA$41.90
A$107.48
List Price:  A$153.55
You save:  A$46.07
S$94.07
List Price:  S$134.39
You save:  S$40.32
HK$544.73
List Price:  HK$778.22
You save:  HK$233.49
CHF 61.85
List Price:  CHF 88.36
You save:  CHF 26.51
NOK kr773.61
List Price:  NOK kr1,105.21
You save:  NOK kr331.59
DKK kr496.19
List Price:  DKK kr708.87
You save:  DKK kr212.68
NZ$119.31
List Price:  NZ$170.45
You save:  NZ$51.14
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.19
৳8,370.95
List Price:  ৳11,959.01
You save:  ৳3,588.06
₹5,916.42
List Price:  ₹8,452.40
You save:  ₹2,535.97
RM312.50
List Price:  RM446.45
You save:  RM133.95
₦117,624.49
List Price:  ₦168,042.19
You save:  ₦50,417.70
₨19,473.08
List Price:  ₨27,819.88
You save:  ₨8,346.79
฿2,426.48
List Price:  ฿3,466.55
You save:  ฿1,040.07
₺2,418.28
List Price:  ₺3,454.83
You save:  ₺1,036.55
B$406.67
List Price:  B$580.99
You save:  B$174.31
R1,268.60
List Price:  R1,812.36
You save:  R543.76
Лв130.09
List Price:  Лв185.85
You save:  Лв55.76
₩98,070.61
List Price:  ₩140,106.88
You save:  ₩42,036.26
₪261.37
List Price:  ₪373.41
You save:  ₪112.03
₱4,133.88
List Price:  ₱5,905.80
You save:  ₱1,771.92
¥10,806.63
List Price:  ¥15,438.71
You save:  ¥4,632.07
MX$1,423.91
List Price:  MX$2,034.25
You save:  MX$610.33
QR255.44
List Price:  QR364.93
You save:  QR109.49
P956.94
List Price:  P1,367.12
You save:  P410.17
KSh9,046.20
List Price:  KSh12,923.70
You save:  KSh3,877.50
E£3,475.04
List Price:  E£4,964.56
You save:  E£1,489.51
ብር8,733.50
List Price:  ብር12,476.97
You save:  ብር3,743.46
Kz63,853.79
List Price:  Kz91,223.61
You save:  Kz27,369.81
CLP$68,157.66
List Price:  CLP$97,372.26
You save:  CLP$29,214.60
CN¥506.84
List Price:  CN¥724.09
You save:  CN¥217.25
RD$4,224.90
List Price:  RD$6,035.83
You save:  RD$1,810.93
DA9,349.94
List Price:  DA13,357.64
You save:  DA4,007.69
FJ$158.89
List Price:  FJ$226.99
You save:  FJ$68.10
Q540.76
List Price:  Q772.55
You save:  Q231.78
GY$14,655.30
List Price:  GY$20,937.04
You save:  GY$6,281.74
ISK kr9,679.61
List Price:  ISK kr13,828.61
You save:  ISK kr4,149
DH699.78
List Price:  DH999.72
You save:  DH299.94
L1,274.52
List Price:  L1,820.82
You save:  L546.30
ден4,093.48
List Price:  ден5,848.08
You save:  ден1,754.59
MOP$561.60
List Price:  MOP$802.33
You save:  MOP$240.72
N$1,270.52
List Price:  N$1,815.10
You save:  N$544.58
C$2,577.69
List Price:  C$3,682.57
You save:  C$1,104.88
रु9,455.06
List Price:  रु13,507.81
You save:  रु4,052.75
S/265.82
List Price:  S/379.77
You save:  S/113.94
K281.99
List Price:  K402.86
You save:  K120.87
SAR262.75
List Price:  SAR375.38
You save:  SAR112.62
ZK1,938.58
List Price:  ZK2,769.51
You save:  ZK830.93
L331.04
List Price:  L472.94
You save:  L141.89
Kč1,685.42
List Price:  Kč2,407.85
You save:  Kč722.43
Ft27,354.11
List Price:  Ft39,078.98
You save:  Ft11,724.86
SEK kr771.99
List Price:  SEK kr1,102.90
You save:  SEK kr330.90
ARS$70,181.20
List Price:  ARS$100,263.16
You save:  ARS$30,081.95
Bs485.08
List Price:  Bs693
You save:  Bs207.92
COP$308,906.07
List Price:  COP$441,313.30
You save:  COP$132,407.23
₡35,572.52
List Price:  ₡50,820.06
You save:  ₡15,247.54
L1,770.13
List Price:  L2,528.86
You save:  L758.73
₲548,589.81
List Price:  ₲783,733.33
You save:  ₲235,143.51
$U2,994.50
List Price:  $U4,278.05
You save:  $U1,283.54
zł289.22
List Price:  zł413.19
You save:  zł123.97
Already have an account? Log In

Transcript

Now that we have learned how to integrate a spring MVC application with hibernate. In this session, let us learn how to use spring framework to provide the implementation for the do layer. I have given a code snippet over here. This is my dmo interface. Here I have named a test book repository. I'm annotating the interface itself using our great repository.

And this interface is extending crud repository, you know what this crud, create, retrieve, update and delete. So, this crud repository interface is providing methods for performing all these operations. There are few other interfaces available in spring framework. And you have options to choose any of these. If you are using JPS ecosystems provided you can go in for JPA repository Are you can use the top level repository interface that the name itself is repository. So depending upon your requirement, you can choose any of these repositories to create repository, we are going to create, retrieve, update and delete operation.

What a member method in this repository, you don't have to specify the method because there are few methods available in the crud repository. This is an interface it is extending a super interface. The Super interface is having few methods, we are going to use both methods only. Whatever the implementation class you don't have to give any implementation class spring we'll provide the implementation class and perform the operation to process the data in the database. Rather than seeing it theoretically, let us go back to eclipse and try this out. I created a spring MVC application using the spring starter project, the application name of spring MVC JPA.

I have added web GPA and my SQL connector Java dependencies. Now, the application is ready it is just the same application that we have seen in the previous session that is a spring MVC application with hibernate. I have used the same example the only layer that changes to see do layer let us go through the flow first. So, this is the application class which is extending this Spring Boot servlet initializer then I have a book controller it is the same code that we have done in the previous application spring with hibernate, okay, no modification. Then I have got a model class the model is book dot java annotated with at the rate entity and we don't have generated value because we don't want hibernate to do what we are going to provide the ID Okay. Then, I have got the service interface with few methods and implementation which is implemented properly.

That is the service layer is going to call the dealer one layer as usual. Now, instead of having the interface name of boop da vo, I'm having the interface name as book repository. And I'm making the interface extend crud repository. This is also an interface, it takes two parameters. The first parameter is the entity class name. The second parameter is the data type of T ID.

In the previous session, when we were working, the ID was of type int, here I have changed it to integer. Already we have learned when we are working in a spring application, it's always better to go in for wrapper classes add data type, rather than going in for end use integer instead of using double lowercase going for the double uppercase, which is a wrapper class, okay. So just because I have copied it assets from one other application, which we have done in Java, here we have used it assets even in hibernate, we are used to assets. But in this case, I have changed To integer right, the reason is this repository expects two parameters. The first parameter is the entity classmate. The second parameter is the serializable ID data type.

So this is integer, you can just have a look there are no methods written inside this dmo layer and we are not going to add any implementation spring is going to provide our create the implementation class for this book repository. Now, let me go back to this book service I am PL in this book service I am PL I have got all the methods from this implementation only I need to call the method of book repository. So what I have to do first I have to wear it at the rate auto wires, private book repository, book repository. It's always better to have the variable name similar to that of the cat classmates button camel case That's what I have given here. Now, inside this add book method, I need to call the method of book repository, but book repository is not having any method, it is going to use a method of crud repository, let me show you that book repository dot Can you see the methods it is adding these methods delete find out find out is just similar to get our books find one is just similar to get by Id like a we have use get method is similar to that then say sales just to save the object in the database.

So, we are not calling the method of boop da vo, but we are trying to call the method of the crud repository. So, for adding a book, what is a method that I need to use? Say here are these methods on create repository only what I have to say book. Now, next, let me come to get our books method. So here again, it is book a possibly find here they have a convention is fine, it is not get find out find one then if you want to give your one method it is like find by category find by author, you are not supposed to use any other name. The naming convention is something like fine by your instance variable name by if you want to retrieve all the books have one book then methods are available already.

So it is from crud repository. So, let me just add that the return type this is writable. So, it is giving me an error. Let me just keep the cursor and I will add the cap. Okay, let's get to by category. You don't have any method like find by category, find the author.

What you need to do is you have to add this method alone in the book repository. We will do that later. Next get by ID when you are working with hibernate. We used method like get session dot get book dot class commodity ID Probably gave right here the method is find one. So again I will just go and add it over here. Book repository dot find one which is taking book IDs parameter is of type integer here we are passed to primitive types automatically it will be auto box this update group so for update book again what is the book repository dot you don't have any specific say update methods you need to you you need to use the same method.

So, I hope you say and I need to pass Bucharest parameter and then delete book repository dot delete wherein I can pass in delete methods you can just have a look I've got two methods sorry two three methods. One is taking the book cost parameters The other one is taking the idea parameter The third one is taking the iframe double pass as parameter. Now for me I want to pass only ID scientists going in for the second method only. So, here we can just use book ID. So, using the credit repository, we can perform create, retrieve, update and delete, create a SWAT adding book to the database. So, using save method retrieve the method is find all then update for update also the method is saved then delete for delete the method is delete okay.

So, we have not added any code or any implementation class in the DA will layer What about fine but you I mean fine Get up get books by category as I told you, if you want to retrieve items by a particular instance variable, you need to give the method a method naming convention or find by category find by price find by Arthur okay. So, you have to be very specific in the naming convention of the method. Now I will go to this book repository. Here, I can add that method alone. The faculty if you want to find by category and author that is also possible, wherein you can use a direct query to give your own query or you can go in for named query. There are different ways of connecting to the database using hibernate.

Basically how actually the flow says your application is using spring data JPA to connect to the database internally spring data JPA uses JPA with JPA we are using hibernate as the JPA provider. hibernate implements this JPA and uses JDBC to connect to the database. JDBC is using that data source to get the database details. The VM was a database a site URL username, password, the driver All these are retrieved from the data source by JDBC. hibernate uses JDBC to connect to the database mean you know that hibernate uses the patch statement to process the data in the database. Now, let me just go in for this method, this book fine bye category and I need to pass a parameter a string category, I will just show you the difference.

Now, let me just try to add one other method list to book find books by category to the semi cola. When I add the second method, this will not be recognized by spring framework. The naming convention should be fine by followed by the property name our instance variable name for in our entity we have got category as instance variables. So, with that this will be recognized so this is not the right way of adding the methods So, let me just remove this I will save it, it will not give you an error now, but when you try to run this application or call the method it will give you an error. Now I'll go to this book service imp. Now in this get book by category, I need to call book repository dot find a category sync category.

So the service layer is complete. Now we are trying to call the methods of crud repository by extending the crud repository. We will call the appropriate methods for saving, updating, deleting and retrieving the books. What about the application dot properties file? In application dot properties file, you don't have to specify anything later to hibernate you Everything will be taken care of by spring framework only. What you need to give us that the details that is needed for connecting to the appropriate database URL username and password.

Now it is a same application. I have got the same All the JSP pages also inside web app web by end of use folder see you can have a look it is the same JSP pages. Now I will come to this book controller in boot controller also I have given the same methods. Let me try running this application, spring MVC JPA, right click run as run on server. Click Next Click Finish application has started. I have got the output as I told you, it is just the same application that we have done with the hibernate spring MVC application with the hibernate.

In case of that application. We created the dmo interface and we gave the implementation for that particular interface. In this one, we have not gone for the implementation class. Everything is taken care by spring. Even we didn't create a bean for entity manager. I mean yeah, for entity manager or for creating the session factory in the application class.

It is a very simple application wherein everything is taken care of by spring boots that is advantage of going in for spring boots. Spring mode helps helps you to create a ready to run application whether it is a spring normal spring application or a spring MVC application are using Spring Boot You can also create spring RESTful web service. already we have seen how to create a spring RESTful web service as a jar. In the next session we will be learning how to create a spring RESTful web service as a war file. Okay, now I have got the application let me just try running everything first. This I will check in for the category and just giving it a section search by category just giving the category properly if I'm trying to give a category which is not available.

No books in this category bar actually we have mapped this in book controller. Now if the book cluster is empty, when you can check it for a check for empty only after The object is created. So, for that you need to create this ArrayList and then only you need to call get books by category Yes, if the book list is not available it will return null. In that case you can check for null condition also but I am taking in foreign empty condition and checking whether the list is empty the object is created and then only you can check for ecmp if the object itself is not created, you need to check for null condition okay then I'll go here okay. Now, let me try adding a book spring data book ID 14 Arthur Rajan prices 890 category. Click on that to come back to the same homepage that a check whether it is added Yeah, here it is spring data Rajan category tech and price 890.

Let me check in database also select star from books shared last line string data it is there. Okay, now we will try to update the book the ID is 14 so I'm just clicking update book. Let me give the ideas 14 Click Get book I have got the book, it may just change it I'm changing the author name. And here I'm changing the title also. And let me click on updated you can see that okay, and delete book for deleting book let me just select some other ID and check in the database. I will go and select to last one to not owners new hibernate, click Delete.

Okay Here it is difficult to identify, see the right approaches you please add jst l while creating the application itself so that you can just show it in a tabular form. So let me check it in the database Boop name is new hibernate here it is remote. So, based on the ID you are removing one particular row I hope it is very clear what actually we have seen in this particular session is how to create a spring MVC application and connect to the database using spring data JPA because we are using spring boots most of the work is taken care of by spring framework only, we have not created the DA vo layer, the do part of the implementation part is completely taken care by spring framework. If you want to go in for some other methods like find book by location, I mean find book by author and category.

In that scenario you can go in for each school queries. There are few advanced SQL queries available, which can be added in the entity class itself. And it can be used by the methods that are available in the book reports in the repository. Sorry, in the DEA earlier in our case, this book repository So, whatever shown you is a simple example of connecting to the database using spring data JPA. 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.