{"id":146,"date":"2019-03-18T16:27:54","date_gmt":"2019-03-18T16:27:54","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/spring-boot\/?p=146"},"modified":"2020-07-26T08:14:38","modified_gmt":"2020-07-26T08:14:38","slug":"spring-boot-extending-jpa-repository","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/spring-boot\/spring-boot-extending-jpa-repository\/","title":{"rendered":"Spring Boot &#8211; Extending JPA Repository"},"content":{"rendered":"<p>In <a href=\"https:\/\/kindsonthegenius.com\/spring-boot\/17-spring-boot-jpa-repositories-for-the-social-api\/\">the last chapter,<\/a> we wrote methods to return list of items. We also wrote a method to get an item by id. For example, we have<\/p>\n<ul>\n<li>getPostById<\/li>\n<li>getUserByID<\/li>\n<li>getLocationById<\/li>\n<\/ul>\n<p>Now what if we also want to get list of items based on other criteria. For example we want to:<\/p>\n<ul>\n<li>get Users with the same lastname<\/li>\n<li>get User based on an email address<\/li>\n<li>get user where firstname like a given string<\/li>\n<li>get Posts by date etc.<\/li>\n<\/ul>\n<p>Let&#8217;s see how we can do all of these<\/p>\n<p>&nbsp;<\/p>\n<h4><strong>The findBy&lt;VariableName&gt; method<\/strong><\/h4>\n<p>This is a generic method implemented in Spring Data JPA. The VariableName is the member variable you want to search by. So In case of the User class, you can use:<\/p>\n<ul>\n<li>findById,<\/li>\n<li>findByFirstname<\/li>\n<li>findByLastname<\/li>\n<li>findByLocation<\/li>\n<li>findByEmail<\/li>\n<\/ul>\n<p>To use any of these, you simply need to define, it in the repository interface you created.<\/p>\n<p>&nbsp;<\/p>\n<p>For now, let&#8217;s do this for the Location class. We would define findByName in the LocationRepository. Then we can use it in the LocationService and LocationController classes.<\/p>\n<p>So in the LocationRepository, add the following line:<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> List<span style=\"color: #333333;\">&lt;<\/span>Location<span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0066bb; font-weight: bold;\">findByName<\/span><span style=\"color: #333333;\">(<\/span>String name<span style=\"color: #333333;\">);<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>In the LocationService, write the following line:<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/returns list of locations base on a given name<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> List<span style=\"color: #333333;\">&lt;<\/span>Location<span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0066bb; font-weight: bold;\">getLocationsByName<\/span><span style=\"color: #333333;\">(<\/span>String name<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n    List<span style=\"color: #333333;\">&lt;<\/span>Location<span style=\"color: #333333;\">&gt;<\/span> locations  <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> ArrayList<span style=\"color: #333333;\">&lt;&gt;();<\/span>\r\n\t   \r\n    locationRepository<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">findByName<\/span><span style=\"color: #333333;\">(<\/span>name<span style=\"color: #333333;\">)<\/span>\r\n    <span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">forEach<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #997700; font-weight: bold;\">locations:<\/span><span style=\"color: #333333;\">:<\/span>add<span style=\"color: #333333;\">);<\/span>\r\n\t   \r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> locations<span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Then, in the LocationController file, write the following line:<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"> <span style=\"color: #555555; font-weight: bold;\">@RequestMapping<\/span><span style=\"color: #333333;\">(<\/span>value <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"\/locations\/name\/{name}\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n <span style=\"color: #008800; font-weight: bold;\">public<\/span> List<span style=\"color: #333333;\">&lt;<\/span>Location<span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0066bb; font-weight: bold;\">getLocationByName<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #555555; font-weight: bold;\">@PathVariable<\/span> String name<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n \t<span style=\"color: #008800; font-weight: bold;\">return<\/span> locationService<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getLocationsByName<\/span><span style=\"color: #333333;\">(<\/span>name<span style=\"color: #333333;\">);<\/span>\r\n <span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Notice the RequestMapping in the getLocationByName method in the LocationController. You can see it has a value of\u00a0<span style=\"background-color: #fff0f0;\">\/locations\/name\/{name}<\/span>. This is so that it can be distinguished from the getLocation method which have similar RequestMapping.<\/p>\n<p>&nbsp;<\/p>\n<p>I recommend you try every bit of this lesson yourself. If you have any challenges, then leave a comment below.<\/p>\n<p>Next, we would examine Query Methods.<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>In the last chapter, we wrote methods to return list of items. We also wrote a method to get an item by id. For example, &hellip; <!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":148,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-146","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-spring-boot-tutorials"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/posts\/146","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/comments?post=146"}],"version-history":[{"count":4,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/posts\/146\/revisions"}],"predecessor-version":[{"id":241,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/posts\/146\/revisions\/241"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/media\/148"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/media?parent=146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/categories?post=146"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/tags?post=146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}