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

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.