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
€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

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.