<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Petri Kainulainen &#187; Programming</title>
	<atom:link href="http://www.petrikainulainen.net/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.petrikainulainen.net</link>
	<description>The Never-Ending Quest of Searching for Improvement</description>
	<lastBuildDate>Wed, 09 May 2012 17:00:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Spring Data JPA Tutorial Part Nine: Conclusions</title>
		<link>http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-nine-conclusions/</link>
		<comments>http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-nine-conclusions/#comments</comments>
		<pubDate>Wed, 09 May 2012 17:00:02 +0000</pubDate>
		<dc:creator>Petri Kainulainen</dc:creator>
				<category><![CDATA[Spring Framework]]></category>
		<category><![CDATA[spring data]]></category>
		<category><![CDATA[spring data jpa]]></category>

		<guid isPermaLink="false">http://www.petrikainulainen.net/?p=2788</guid>
		<description><![CDATA[This is the ninth and the last part of my Spring Data JPA tutorial. Now it is time to take a look of what we have learned, and how we should use it to build better software. Table of Contents The contents of my Spring Data JPA tutorial is given in following: Part One: Configuration [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>This is the ninth and the last part of my Spring Data JPA tutorial. Now it is time to take a look of what we have learned, and how we should use it to build better software.</p>
<h2>Table of Contents</h2>
<p>The contents of my Spring Data JPA tutorial is given in following:</p>
<ul>
<li><a href="http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-one-configuration/">Part One: Configuration</a></li>
<li><a href="http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-two-crud/">Part Two: CRUD</a></li>
<li><a href="http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-three-custom-queries-with-query-methods/">Part Three: Custom Queries with Query Methods</a></li>
<li><a href="http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-four-jpa-criteria-queries/">Part Four: JPA Criteria Queries</a></li>
<li><a href="http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-five-querydsl/">Part Five: Querydsl</a></li>
<li><a href="http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-six-sorting/">Part Six: Sorting</a></li>
<li><a href="http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-seven-pagination/">Part Seven: Pagination</a></li>
<li><a href="http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-eight-adding-functionality-to-a-repository/">Part Eight: Adding Functionality to a Repository</a></li>
<li>Part Nine: Conclusions</li>
</ul>
<p>The next step is to take a look of the advantages provided by Spring Data JPA and learn how we can use it in effective manner.</p>
<h2>Promises Kept</h2>
<p>The goal of <a href="http://www.springsource.org/spring-data/jpa">the Spring Data JPA project</a> is stated:</p>
<blockquote><p>Implementing a data access layer of an application has been cumbersome for quite a while. Too much boilerplate code has to be written to execute simple queries as well as perform pagination, and auditing. Spring JPA aims to significantly improve the implementation of data access layers by reducing the effort to the amount that&#8217;s actually needed. As a developer you write your repository interfaces, including custom finder methods, and Spring will provide the implementation automatically.</p></blockquote>
<p>This is a lot to promise. The question is, has Spring Data JPA achieved its goal. As you have learned from my tutorial, Spring Data JPA has following advantages over the &#8220;old school&#8221; method of building JPA repositories:</p>
<ul>
<li>It provides CRUD capabilities to any domain object without the need of any boilerplate code. </li>
<li>It minimizes the amount of source code needed to write custom queries.</li>
<li>It offers simple abstractions for performing common tasks like sorting an pagination.</li>
</ul>
<p>The thing is that implementing these functions have forced the developers to write a lot of boilerplate code in the past. Spring Data JPA changes all this. It minimizes the amount of code needed for implementing repositories. </p>
<h2>Making It Work for You</h2>
<p>I hate the term best practices because it has a negative effect on continuous improvement. However, I still feel that it is my responsibility to give you some guidance concerning the usage of Spring Data JPA. Here are my five cents about this matter:</p>
<h3>Creating Queries</h3>
<p>Your goal should be to use the Spring Data JPA to reduce the amount of code you have to write. With this goal in mind, I  will you give some guidelines for creating queries with Spring Data JPA:</p>
<ul>
<li>If the query can be build by using the <a href="http://static.springsource.org/spring-data/data-jpa/docs/1.0.x/reference/html/#jpa.query-methods.query-creation">query generation from method name strategy</a>, I think you should use it. However, if the method name will become long and messy, I would consider using the @Query annotation in order to make the source code more readable.</li>
<li>Your second option for creating queries should be the @Query annotation and JPQL. This approach ensures that the you will not have to write more code than it is necessary.</li>
<li>Use JPA Criteria API or Querydsl only when you have no other options. Remember to extract the query generation logic into separate classes which creates Specification or Predicate objects (Depending on your technology selection).</li>
</ul>
<h3>JPA Criteria API Versus Querydsl</h3>
<p>This is a question which should be asked by each developer.  The usage of JPA Criteria API has been argued by claiming that you can use it to build type safe queries. Even though this is true, you can achieve the same goal by using the Querydsl. The first round ends in a draw, and we need to look for the answer from a bit deeper. </p>
<p>I will compare these two options in following categories: readability and testability.</p>
<h4>Readability</h4>
<blockquote><p>Programs must be written for people to read, and only incidentally for machines to execute</p></blockquote>
<p> &#8211; Abelson  and Sussman on Programming.</p>
<p>With this guideline in mind, lets take a look of the implementations, which I created for my previous blog entries. The requirements of the search function are following:</p>
<ul>
<li>It must be possible to search persons by using their last name as a search criteria.</li>
<li>The search function must return only such persons whose last name begins with the given search term.</li>
<li>The search must be case insensitive.</li>
</ul>
<p><strong>First</strong>, lets take a look of the implementation which is using the JPA Criteria API. The source code of my static meta model is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">@StaticMetamodel<span style="color: #009900;">&#40;</span>Person.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Person_ <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">volatile</span> SingularAttribute<span style="color: #339933;">&lt;</span>Person, String<span style="color: #339933;">&gt;</span> lastName<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>The source code of my specification builder class is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PersonSpecifications <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Creates a specification used to find persons whose last name begins with<br />
&nbsp; &nbsp; &nbsp;* the given search term. This search is case insensitive.<br />
&nbsp; &nbsp; &nbsp;* @param searchTerm<br />
&nbsp; &nbsp; &nbsp;* @return<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> Specification<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> lastNameIsLike<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> searchTerm<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Specification<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> Predicate toPredicate<span style="color: #009900;">&#40;</span>Root<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> personRoot, CriteriaQuery<span style="color: #339933;">&lt;?&gt;</span> query, CriteriaBuilder cb<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> likePattern <span style="color: #339933;">=</span> getLikePattern<span style="color: #009900;">&#40;</span>searchTerm<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> cb.<span style="color: #006633;">like</span><span style="color: #009900;">&#40;</span>cb.<span style="color: #006633;">lower</span><span style="color: #009900;">&#40;</span>personRoot.<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span>get<span style="color: #009900;">&#40;</span>Person_.<span style="color: #006633;">lastName</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>, likePattern<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> getLikePattern<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> searchTerm<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StringBuilder pattern <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StringBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pattern.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>searchTerm.<span style="color: #006633;">toLowerCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pattern.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> pattern.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p><strong>Second</strong>, the source code of the implementations which uses Querydsl is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PersonPredicates <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> Predicate lastNameIsLike<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> searchTerm<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; QPerson person <span style="color: #339933;">=</span> QPerson.<span style="color: #006633;">person</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> person.<span style="color: #006633;">lastName</span>.<span style="color: #006633;">startsWithIgnoreCase</span><span style="color: #009900;">&#40;</span>searchTerm<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>This use case is pretty simple but it can still be used for demonstrating the differences of the JPA Criteria API and the Querydsl. The source code written by using Querydsl is clearly more readable than the one using the JPA Criteria API. Also, when the queries become more complex, the difference will be much bigger. </p>
<p>I would say that this round goes to Querydsl.</p>
<h4>Testability</h4>
<blockquote><p>Software testability is the degree to which a software artifact (i.e. a software system, software module, requirements or design document) supports testing  in a given context.</p></blockquote>
<p> &#8211; <a href="http://en.wikipedia.org/wiki/Software_testability">Wikipedia</a>.</p>
<p>In other words, the testability of your code defines the amount and quality of tests you can write at the same cost. If the testability of your code is high, you can write more tests with better quality than in a situation where the testability of your code is low.</p>
<p>Lets keep this measurement in mind when we will compare the unit tests written for implementations which were presented earlier.</p>
<p><strong>First</strong>, lets check out the unit test for the implementation which uses the JPA Criteria API:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:550px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PersonSpecificationsTest <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> SEARCH_TERM <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Foo&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> SEARCH_TERM_LIKE_PATTERN <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;foo%&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> CriteriaBuilder criteriaBuilderMock<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> CriteriaQuery criteriaQueryMock<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> Root<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> personRootMock<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Before<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; criteriaBuilderMock <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>CriteriaBuilder.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; criteriaQueryMock <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>CriteriaQuery.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; personRootMock <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>Root.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> lastNameIsLike<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Path lastNamePathMock <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>Path.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>personRootMock.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>Person_.<span style="color: #006633;">lastName</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>lastNamePathMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; Expression lastNameToLowerExpressionMock <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>Expression.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>criteriaBuilderMock.<span style="color: #006633;">lower</span><span style="color: #009900;">&#40;</span>lastNamePathMock<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>lastNameToLowerExpressionMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; Predicate lastNameIsLikePredicateMock <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>Predicate.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>criteriaBuilderMock.<span style="color: #006633;">like</span><span style="color: #009900;">&#40;</span>lastNameToLowerExpressionMock, SEARCH_TERM_LIKE_PATTERN<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>lastNameIsLikePredicateMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; Specification<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> actual <span style="color: #339933;">=</span> PersonSpecifications.<span style="color: #006633;">lastNameIsLike</span><span style="color: #009900;">&#40;</span>SEARCH_TERM<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Predicate actualPredicate <span style="color: #339933;">=</span> actual.<span style="color: #006633;">toPredicate</span><span style="color: #009900;">&#40;</span>personRootMock, criteriaQueryMock, criteriaBuilderMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; verify<span style="color: #009900;">&#40;</span>personRootMock, times<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>Person_.<span style="color: #006633;">lastName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verifyNoMoreInteractions<span style="color: #009900;">&#40;</span>personRootMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; verify<span style="color: #009900;">&#40;</span>criteriaBuilderMock, times<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">lower</span><span style="color: #009900;">&#40;</span>lastNamePathMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verify<span style="color: #009900;">&#40;</span>criteriaBuilderMock, times<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">like</span><span style="color: #009900;">&#40;</span>lastNameToLowerExpressionMock, SEARCH_TERM_LIKE_PATTERN<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verifyNoMoreInteractions<span style="color: #009900;">&#40;</span>criteriaBuilderMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; verifyZeroInteractions<span style="color: #009900;">&#40;</span>criteriaQueryMock, lastNamePathMock, lastNameIsLikePredicateMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>lastNameIsLikePredicateMock, actualPredicate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p><strong>Second</strong>, the unit test for the implementation using Querydsl is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PersonPredicatesTest <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> SEARCH_TERM <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Foo&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> EXPECTED_PREDICATE_STRING <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;startsWithIgnoreCase(person.lastName,Foo)&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> lastNameLike<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Predicate predicate <span style="color: #339933;">=</span> PersonPredicates.<span style="color: #006633;">lastNameIsLike</span><span style="color: #009900;">&#40;</span>SEARCH_TERM<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> predicateAsString <span style="color: #339933;">=</span> predicate.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>EXPECTED_PREDICATE_STRING, predicateAsString<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>After seeing the unit tests for both implementations, it should be obvious that writing unit tests for Querydsl is much easier than writing unit tests for the JPA Criteria API. Also, the unit test written to test the Querydsl predicate builder is much easier to understand. This is valuable because unit tests should also be used to document the behavior of the system.</p>
<p>At this point it should be clear that the winner of this round is Querydsl</p>
<p>PS. I am aware that unit tests do no ensure that the results returned by the created query are correct. However, I believe that they are still valuable because running unit tests is typically dramatically faster than running integration tests. It is still good to understand that in the context of integration testing, the testability of both implementations is equal.</p>
<h4>Conclusions</h4>
<p>The question is:</p>
<blockquote><p>Should I use the JPA Criteria API or Querydsl?</p></blockquote>
<p>It depends. If you are starting from scratch and you have a total control over your technology selections, you should at least consider using Querydsl. It makes your code easier to write and read. It also means that writing unit tests for your code is simpler and faster.</p>
<p>On the other hand, if you are modifying an existing system to use Spring Data JPA, and the existing code is using the JPA Criteria API, you might want to continue using it for the sake of consistency.</p>
<p>The fact is that there is no right answer for this question. The answer depends always from external requirements. The only thing you can do, is to ensure that you are aware of the different options, which are available to you. Only then you can choose the right tool for the task in hand.</p>
<h2>There is Still More to Learn</h2>
<p>The truth is that I have only scratched the surface of implementing JPA based repositories. I hope that the recommendations given in this blog entry will help you to take the first step, but I have to admit that there is a lot more to learn. I hope that the following resources will help you in your journey:</p>
<h3>Reference Documentation</h3>
<ul>
<li><a href="http://www.springsource.org/spring-data/jpa">Spring Data JPA</a></li>
<li><a href="http://www.hibernate.org/docs">Hibernate documentation</a></li>
<li><a href="http://www.querydsl.com/documentation">Querydsl  documentation</a></li>
</ul>
<h3>JPA Criteria API 2.0</h3>
<ul>
<li><a href="http://www.ibm.com/developerworks/java/library/j-typesafejpa/">Dynamic, Typesafe Query in JPA 2.0</a></li>
<li>JPA Criteria API by Samples <a href="http://www.altuure.com/2010/09/23/jpa-criteria-api-by-samples-part-i/">Part I</a> and <a href="http://www.altuure.com/2010/09/23/jpa-criteria-api-by-samples—part-ii/">Part II</a></li>
<li><a href="http://docs.oracle.com/javaee/6/tutorial/doc/gjitv.html">Using the Criteria API to Create Queries &#8211; The Java EE 6 Tutorial</a></li>
</ul>
<h3>Querydsl</h3>
<ul>
<li><a href="http://blog.mysema.com/2010/04/querydsl-as-alternative-to-jpa-2.html">Querydsl as an alternative to the JPA 2 Criteria  API</a></li>
<li><a href="http://blog.mysema.com/2010/07/querying-hibernate-with-querydsl.html">Querying Hibernate with Querydsl</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-nine-conclusions/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Spring Data JPA Tutorial Part Eight: Adding Functionality to a Repository</title>
		<link>http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-eight-adding-functionality-to-a-repository/</link>
		<comments>http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-eight-adding-functionality-to-a-repository/#comments</comments>
		<pubDate>Sat, 28 Apr 2012 16:37:45 +0000</pubDate>
		<dc:creator>Petri Kainulainen</dc:creator>
				<category><![CDATA[Spring Framework]]></category>
		<category><![CDATA[spring data]]></category>
		<category><![CDATA[spring data jpa]]></category>

		<guid isPermaLink="false">http://www.petrikainulainen.net/?p=2699</guid>
		<description><![CDATA[The previous part of my tutorial described how you can paginate query results with Spring Data JPA. The example application of this blog entry has the same functional requirements, but it will use Querydsl instead of JPA criteria API. This blog entry assumes that you have got experience from both Querydsl And Spring Data JPA. [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>The previous part of my tutorial described <a href="http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-seven-pagination/">how you can paginate query results with Spring Data JPA</a>. The example application of this blog entry has the same functional requirements, but it will use Querydsl instead of JPA criteria API. This blog entry assumes that you have got experience from both Querydsl And Spring Data JPA. If this is not the case, please read my <a href="http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-five-querydsl/">Spring Data JPA and Querydsl tutorial</a>.</p>
<p>If you have read the previous parts of my Spring Data JPA tutorial, you might have noticed that the described solutions introduced a dependency between the service layer and the Spring Data JPA. This blog entry describes how you can eliminate that dependency (or at least most of it) by adding custom functionality to a Spring Data repository.</p>
<h2>Adding Functionality to a Spring Data Repository</h2>
<p>You can add <a href="http://static.springsource.org/spring-data/data-jpa/docs/1.0.x/reference/html/#repositories.single-repository-behaviour">custom functionality to a Spring Data repository</a> by following these steps:</p>
<ul>
<li>Create an interface for the custom functionality.</li>
<li>Implement the created interface.</li>
<li>Create the Spring Data JPA repository.</li>
</ul>
<p>Enough with the theory. It is time to take a look, how I added custom functionality to the person repository of my example application.</p>
<h2>Implementing The Example Application</h2>
<p>This Section describes how I added the custom functionality to the person repository of my example application. The steps described previously are discussed with more details in following Subsections.</p>
<h3>Creating an interface for the custom functionality.</h3>
<p>In order to eliminate the dependency between the service layer and the Spring Data JPA, I created an interface called <em>PaginatingPersonRepository</em>.  My goal was to move all pagination and sorting logic behind this interface. The source code of my custom interface is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:450px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> PaginatingPersonRepository <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Finds all persons stored in the database.<br />
&nbsp; &nbsp; &nbsp;* @return<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> findAllPersons<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Finds the count of persons stored in the database.<br />
&nbsp; &nbsp; &nbsp;* @param searchTerm<br />
&nbsp; &nbsp; &nbsp;* @return<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">long</span> findPersonCount<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> searchTerm<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Finds persons for the requested page whose last name starts with the given search term.<br />
&nbsp; &nbsp; &nbsp;* @param searchTerm &nbsp; &nbsp;The used search term.<br />
&nbsp; &nbsp; &nbsp;* @param page &nbsp;The number of the requested page.<br />
&nbsp; &nbsp; &nbsp;* @return &nbsp;A list of persons belonging to the requested page.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> findPersonsForPage<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> searchTerm, <span style="color: #000066; font-weight: bold;">int</span> page<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<h3>Implementing the Declared Interface</h3>
<p>The next step is to create an implementation for the <em>PaginatingPersonRepository</em> interface.  This implementation has following responsibilities:</p>
<ul>
<li>Creating a new <a href="http://static.springsource.org/spring-data/data-jpa/docs/1.0.x/api/org/springframework/data/jpa/repository/support/QueryDslJpaRepository.html"><em>QueryDslJPARepository</em></a> instance and delegating the method calls to it.</li>
<li>Obtaining the required Querydsl predicate when a search is performed.</li>
<li>Creating the <a href="http://static.springsource.org/spring-data/data-commons/docs/1.1.0.RELEASE/api/org/springframework/data/domain/Sort.html"><em>Sort</em></a> object which is used to specify the sort order of query results.</li>
<li>Creating the <a href="http://static.springsource.org/spring-data/data-commons/docs/1.1.0.RELEASE/api/org/springframework/data/domain/PageRequest.html"><em>PageRequest</em></a> object which id used the specify the wanted page and sort order of the search results.</li>
</ul>
<p>The implementation of the <em>PaginatingPersonRepository</em> interface is using the <em>PersonPredicates</em> class for creating the required Querydsl predicates. The source code of this class is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PersonPredicates <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> Predicate lastNameIsLike<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> searchTerm<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; QPerson person <span style="color: #339933;">=</span> QPerson.<span style="color: #006633;">person</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> person.<span style="color: #006633;">lastName</span>.<span style="color: #006633;">startsWithIgnoreCase</span><span style="color: #009900;">&#40;</span>searchTerm<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>The repository architecture of Spring Data JPA is looking for the custom implementation from the package where the repository was found. The name of the class implementing the custom interface must be constructed by adding a special postfix to the name of the custom interface. The default value of this postfix is <em>Impl</em>, but you can change the postfix by adding a <em>repository-impl-postfix</em> attribute to the namespace configuration element of Spring Data JPA. An example of this is given in following:</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">&lt;!-- Declares the base package for repositories and states that postfix used to identify custom implementations is FooBar. --&gt;</span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jpa:repositories</span> <span style="color: #000066;">base-package</span>=<span style="color: #ff0000;">&quot;net.petrikainulainen.spring.datajpa.repository&quot;</span> <span style="color: #000066;">repository-impl-postfix</span>=<span style="color: #ff0000;">&quot;FooBar&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></div></div>
<p>However, I am going to use the default configuration. Thus, the name of my custom repository class must be <em>PaginatingPersonRepositoryImpl</em>. Its source code is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:450px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">@<span style="color: #003399;">Repository</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PaginatingPersonRepositoryImpl <span style="color: #000000; font-weight: bold;">implements</span> PaginatingPersonRepository <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> Logger LOGGER <span style="color: #339933;">=</span> LoggerFactory.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>PaginatingPersonRepositoryImpl.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> NUMBER_OF_PERSONS_PER_PAGE <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @PersistenceContext<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> EntityManager entityManager<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> QueryDslJpaRepository<span style="color: #339933;">&lt;</span>Person, Long<span style="color: #339933;">&gt;</span> personRepository<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> PaginatingPersonRepositoryImpl<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> findAllPersons<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; LOGGER.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Finding all persons&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Passes the Sort object to the repository</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> personRepository.<span style="color: #006633;">findAll</span><span style="color: #009900;">&#40;</span>sortByLastNameAsc<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">long</span> findPersonCount<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> searchTerm<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; LOGGER.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Finding person count with search term: &quot;</span> <span style="color: #339933;">+</span> searchTerm<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Passes the predicate to the repository</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> personRepository.<span style="color: #006633;">count</span><span style="color: #009900;">&#40;</span>lastNameIsLike<span style="color: #009900;">&#40;</span>searchTerm<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> findPersonsForPage<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> searchTerm, <span style="color: #000066; font-weight: bold;">int</span> page<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; LOGGER.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Finding persons for page &quot;</span> <span style="color: #339933;">+</span> page <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; with search term: &quot;</span> <span style="color: #339933;">+</span> searchTerm<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Passes the predicate and the page specification to the repository</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Page requestedPage <span style="color: #339933;">=</span> &nbsp;personRepository.<span style="color: #006633;">findAll</span><span style="color: #009900;">&#40;</span>lastNameIsLike<span style="color: #009900;">&#40;</span>searchTerm<span style="color: #009900;">&#41;</span>, constructPageSpecification<span style="color: #009900;">&#40;</span>page<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> requestedPage.<span style="color: #006633;">getContent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Returns a new object which specifies the the wanted result page.<br />
&nbsp; &nbsp; &nbsp;* @param pageIndex The index of the wanted result page<br />
&nbsp; &nbsp; &nbsp;* @return<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Pageable</span> constructPageSpecification<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> pageIndex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">Pageable</span> pageSpecification <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PageRequest<span style="color: #009900;">&#40;</span>pageIndex, NUMBER_OF_PERSONS_PER_PAGE, sortByLastNameAsc<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> pageSpecification<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Returns a Sort object which sorts persons in ascending order by using the last name.<br />
&nbsp; &nbsp; &nbsp;* @return<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> Sort sortByLastNameAsc<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Sort<span style="color: #009900;">&#40;</span>Sort.<span style="color: #006633;">Direction</span>.<span style="color: #006633;">ASC</span>, <span style="color: #0000ff;">&quot;lastName&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* An initialization method which is run after the bean has been constructed.<br />
&nbsp; &nbsp; &nbsp;* This ensures that the entity manager is injected before we try to use it.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; @PostConstruct<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; JpaEntityInformation<span style="color: #339933;">&lt;</span>Person, Long<span style="color: #339933;">&gt;</span> personEntityInfo <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JpaMetamodelEntityInformation<span style="color: #339933;">&lt;</span>Person, Long<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span>Person.<span style="color: #000000; font-weight: bold;">class</span>, entityManager.<span style="color: #006633;">getMetamodel</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; personRepository <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> QueryDslJpaRepository<span style="color: #339933;">&lt;</span>Person, Long<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span>personEntityInfo, entityManager<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* This setter method should be used only by unit tests<br />
&nbsp; &nbsp; &nbsp;* @param personRepository<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> setPersonRepository<span style="color: #009900;">&#40;</span>QueryDslJpaRepository<span style="color: #339933;">&lt;</span>Person, Long<span style="color: #339933;">&gt;</span> personRepository<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">personRepository</span> <span style="color: #339933;">=</span> personRepository<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>We also have to verify that the created repository implementation is working as expected. This means that unit tests must be written. The source code of the unit tests is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:450px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PaginatingPersonRepositoryImplTest <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> PAGE_INDEX <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> PERSON_COUNT <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> PROPERTY_LASTNAME <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;lastName&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> SEARCH_TERM <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;searchTerm&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> PaginatingPersonRepositoryImpl repository<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> QueryDslJpaRepository personRepositoryMock<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Before<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; repository <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PaginatingPersonRepositoryImpl<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; personRepositoryMock <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>QueryDslJpaRepository.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; repository.<span style="color: #006633;">setPersonRepository</span><span style="color: #009900;">&#40;</span>personRepositoryMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> findAllPersons<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; repository.<span style="color: #006633;">findAllPersons</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; ArgumentCaptor<span style="color: #339933;">&lt;</span>Sort<span style="color: #339933;">&gt;</span> sortArgument <span style="color: #339933;">=</span> ArgumentCaptor.<span style="color: #006633;">forClass</span><span style="color: #009900;">&#40;</span>Sort.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verify<span style="color: #009900;">&#40;</span>personRepositoryMock, times<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">findAll</span><span style="color: #009900;">&#40;</span>sortArgument.<span style="color: #006633;">capture</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; Sort sort <span style="color: #339933;">=</span> sortArgument.<span style="color: #006633;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>Sort.<span style="color: #006633;">Direction</span>.<span style="color: #006633;">ASC</span>, sort.<span style="color: #006633;">getOrderFor</span><span style="color: #009900;">&#40;</span>PROPERTY_LASTNAME<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getDirection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> findPersonCount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>personRepositoryMock.<span style="color: #006633;">count</span><span style="color: #009900;">&#40;</span>any<span style="color: #009900;">&#40;</span>Predicate.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>PERSON_COUNT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">long</span> actual <span style="color: #339933;">=</span> repository.<span style="color: #006633;">findPersonCount</span><span style="color: #009900;">&#40;</span>SEARCH_TERM<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; verify<span style="color: #009900;">&#40;</span>personRepositoryMock, times<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">count</span><span style="color: #009900;">&#40;</span>any<span style="color: #009900;">&#40;</span>Predicate.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verifyNoMoreInteractions<span style="color: #009900;">&#40;</span>personRepositoryMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>PERSON_COUNT, actual<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> findPersonsForPage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> expected <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Page foundPage <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PageImpl<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span>expected<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>personRepositoryMock.<span style="color: #006633;">findAll</span><span style="color: #009900;">&#40;</span>any<span style="color: #009900;">&#40;</span>Predicate.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>, any<span style="color: #009900;">&#40;</span><span style="color: #003399;">Pageable</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>foundPage<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> actual <span style="color: #339933;">=</span> repository.<span style="color: #006633;">findPersonsForPage</span><span style="color: #009900;">&#40;</span>SEARCH_TERM, PAGE_INDEX<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; ArgumentCaptor<span style="color: #339933;">&lt;</span>Pageable<span style="color: #339933;">&gt;</span> pageSpecificationArgument <span style="color: #339933;">=</span> ArgumentCaptor.<span style="color: #006633;">forClass</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Pageable</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verify<span style="color: #009900;">&#40;</span>personRepositoryMock, times<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">findAll</span><span style="color: #009900;">&#40;</span>any<span style="color: #009900;">&#40;</span>Predicate.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>, pageSpecificationArgument.<span style="color: #006633;">capture</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verifyNoMoreInteractions<span style="color: #009900;">&#40;</span>personRepositoryMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">Pageable</span> pageSpecification <span style="color: #339933;">=</span> pageSpecificationArgument.<span style="color: #006633;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>PAGE_INDEX, pageSpecification.<span style="color: #006633;">getPageNumber</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>PaginatingPersonRepositoryImpl.<span style="color: #006633;">NUMBER_OF_PERSONS_PER_PAGE</span>, pageSpecification.<span style="color: #006633;">getPageSize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>Sort.<span style="color: #006633;">Direction</span>.<span style="color: #006633;">ASC</span>, pageSpecification.<span style="color: #006633;">getSort</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getOrderFor</span><span style="color: #009900;">&#40;</span>PROPERTY_LASTNAME<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getDirection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>expected, actual<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<h3>Creating the Spring Data JPA repository</h3>
<p>Now it is time to make the custom functionality available to the users of the repository. This is done by making the repository interface extend the created custom interface. As you might remember, the repository interface of my example application is called <em>PersonRepository</em>. Its source code is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> PersonRepository <span style="color: #000000; font-weight: bold;">extends</span> JpaRepository<span style="color: #339933;">&lt;</span>Person, Long<span style="color: #339933;">&gt;</span>, PaginatingPersonRepository, QueryDslPredicateExecutor<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<h2>Using the Custom Functionality</h2>
<p>Now when the logic related to pagination and sorting has been moved to the custom repository implementation, the service layer will basically just delegate the method calls forward to the repository and act as a transaction boundary. The <em>PersonService</em> interface has stayed intact, but there has been some changes to the following methods of the <em>RepositoryPersonService</em> class:</p>
<ul>
<li><em>public long count(String searchTerm)</em></li>
<li><em>public List&lt;Person&gt; findAll()</em></li>
<li><em>public List&lt;Person&gt; search(String searchTerm, int pageIndex)</em></li>
</ul>
<p>All these methods are simply delegating the method call forward to the repository. The source code of the relevant parts of the <em>RepositoryPersonService</em> class is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:450px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">@Service<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RepositoryPersonService <span style="color: #000000; font-weight: bold;">implements</span> PersonService <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> Logger LOGGER <span style="color: #339933;">=</span> LoggerFactory.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>RepositoryPersonService.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> NUMBER_OF_PERSONS_PER_PAGE <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Resource<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> PersonRepository personRepository<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Transactional<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">long</span> count<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> searchTerm<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; LOGGER.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Getting person count for search term: &quot;</span> <span style="color: #339933;">+</span> searchTerm<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> personRepository.<span style="color: #006633;">findPersonCount</span><span style="color: #009900;">&#40;</span>searchTerm<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Transactional<span style="color: #009900;">&#40;</span>readOnly <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> findAll<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; LOGGER.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Finding all persons&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> personRepository.<span style="color: #006633;">findAllPersons</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Transactional<span style="color: #009900;">&#40;</span>readOnly <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> search<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> searchTerm, <span style="color: #000066; font-weight: bold;">int</span> pageIndex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; LOGGER.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Searching persons with search term: &quot;</span> <span style="color: #339933;">+</span> searchTerm<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> personRepository.<span style="color: #006633;">findPersonsForPage</span><span style="color: #009900;">&#40;</span>searchTerm, pageIndex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Changes made to the <em>RepositoryPersonService</em> class means that its unit tests must be changed as well. The source code of the changed unit tests is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:450px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RepositoryPersonServiceTest <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> PAGE_INDEX <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> SEARCH_TERM <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;foo&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> RepositoryPersonService personService<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> PersonRepository personRepositoryMock<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Before<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; personService <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> RepositoryPersonService<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; personRepositoryMock <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>PersonRepository.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; personService.<span style="color: #006633;">setPersonRepository</span><span style="color: #009900;">&#40;</span>personRepositoryMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> count<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; personService.<span style="color: #006633;">count</span><span style="color: #009900;">&#40;</span>SEARCH_TERM<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; verify<span style="color: #009900;">&#40;</span>personRepositoryMock, times<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">findPersonCount</span><span style="color: #009900;">&#40;</span>SEARCH_TERM<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verifyNoMoreInteractions<span style="color: #009900;">&#40;</span>personRepositoryMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> findAll<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> persons <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>personRepositoryMock.<span style="color: #006633;">findAllPersons</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>persons<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> returned <span style="color: #339933;">=</span> personService.<span style="color: #006633;">findAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; verify<span style="color: #009900;">&#40;</span>personRepositoryMock, times<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">findAllPersons</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verifyNoMoreInteractions<span style="color: #009900;">&#40;</span>personRepositoryMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>persons, returned<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> search<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; personService.<span style="color: #006633;">search</span><span style="color: #009900;">&#40;</span>SEARCH_TERM, PAGE_INDEX<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; verify<span style="color: #009900;">&#40;</span>personRepositoryMock, times<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">findPersonsForPage</span><span style="color: #009900;">&#40;</span>SEARCH_TERM, PAGE_INDEX<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verifyNoMoreInteractions<span style="color: #009900;">&#40;</span>personRepositoryMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<h2>What is Next?</h2>
<p>I have now described to you how you can add custom functionality to a Spring Data repository. Even though this can be a handy in some situations, I think that the situation described in this blog entry is not one of them. To be honest, the benefits obtained from the proposed solution are purely theoretical, and I cannot recommend using this approach for eliminating the dependency between the service layer and Spring Data JPA. You simply don’t get enough bang for your buck. </p>
<p>Sometimes adding functionality to a single repository is not enough. You can also <a href="http://static.springsource.org/spring-data/data-jpa/docs/1.0.x/reference/html/#repositories.custom-behaviour-for-all-repositories">add custom functionality to all repositories</a>. The implementation of this is left as an exercise for the reader. </p>
<p>The last part of my Spring Data JPA tutorial will summarize what we have learned and give some suggestions about the usage of Spring Data JPA.</p>
<p>PS. If you want to a take a closer look of my example application, you can <a href="https://github.com/pkainulainen/Examples/tree/master/Spring/data-jpa/tutorial-part-eight">get it from Github</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-eight-adding-functionality-to-a-repository/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spring Data JPA Tutorial Part Seven: Pagination</title>
		<link>http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-seven-pagination/</link>
		<comments>http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-seven-pagination/#comments</comments>
		<pubDate>Mon, 23 Apr 2012 15:45:54 +0000</pubDate>
		<dc:creator>Petri Kainulainen</dc:creator>
				<category><![CDATA[Spring Framework]]></category>
		<category><![CDATA[spring data]]></category>
		<category><![CDATA[spring data jpa]]></category>

		<guid isPermaLink="false">http://www.petrikainulainen.net/?p=2639</guid>
		<description><![CDATA[The previous part of my Spring Data JPA tutorial described how you can sort query results with Spring Data JPA. This blog entry will describe how you can paginate the query results by using Spring Data JPA. In order to demonstrate the pagination support of Spring Data JPA, I will add two new requirements for [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>The previous part of my Spring Data JPA tutorial <a href="http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-six-sorting/">described how you can sort query results with Spring Data JPA</a>. This blog entry will describe how you can paginate the query results by using Spring Data JPA. In order to demonstrate the pagination support of Spring Data JPA, I will add two new requirements for my example application:</p>
<ul>
<li>The person search results should be paginated.</li>
<li>The maximum number of persons shown on a single search result page is five.</li>
</ul>
<p>I will explain the pagination support of Spring Data JPA and my example implementation in following Sections.</p>
<h2>Pagination with Spring Data JPA</h2>
<p>The key of component of the Spring Data JPA pagination support is the <a href="http://static.springsource.org/spring-data/data-commons/docs/1.1.0.RELEASE/api/org/springframework/data/domain/Pageable.html"><em>Pageable</em></a> interface which is implemented by <a href="http://static.springsource.org/spring-data/data-commons/docs/1.1.0.RELEASE/api/org/springframework/data/domain/PageRequest.html"><em>PageRequest</em></a> class. You can get a specific page of your query results by following these steps:</p>
<ul>
<li>Create an instance of the <a href="http://static.springsource.org/spring-data/data-commons/docs/1.1.0.RELEASE/api/org/springframework/data/domain/PageRequest.html"><em>PageRequest</em></a> class.</li>
<li>Pass the created object as a parameter to the correct repository method.</li>
</ul>
<p>The available repository methods are described in following:</p>
<ul>
<li>If you want to simply paginate all objects found from the database, you should use the <em>public Page&lt;T&gt; findAll(Pageable pageable)</em> method of the <a href="http://static.springsource.org/spring-data/data-jpa/docs/1.0.x/api/org/springframework/data/jpa/repository/JpaRepository.html"><em>JpaRepository&lt;T&gt;</em></a> interface.</li>
<li>If you are building your queries by using the <a href="http://static.springsource.org/spring-data/data-jpa/docs/1.0.x/reference/html/#jpa.query-methods.query-creation">query generation from method name strategy</a>, you can paginate the query results by passing the <em>Pageable</em> instance as a parameter of your method. Check the Spring Data JPA reference manual for more details (<a href="http://static.springsource.org/spring-data/data-jpa/docs/1.0.x/reference/html/#repositories.special-parameters">1.3.2.3 Special Parameter Handling</a>).</li>
<li> If you are <a href="http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-four-jpa-criteria-queries/">using the JPA criteria API for building your query</a>, you should use the <em>public Page&lt;T&gt; findAll(Specification specification, Pageable pageable)</em> method of the <a href="http://static.springsource.org/spring-data/data-jpa/docs/1.0.x/api/org/springframework/data/jpa/repository/JpaSpecificationExecutor.html"><em>JpaSpecificationExecutor&lt;T&gt;</em></a> interface.</li>
<li>If you are <a href="http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-five-querydsl/">building your queries with Querydsl</a>, you should use the <em>public Page&lt;T&gt; findAll(Predicate predicate, Pageable pageable)</em> method of the <a href="http://static.springsource.org/spring-data/data-commons/docs/1.1.0.RELEASE/api/org/springframework/data/querydsl/QueryDslPredicateExecutor.html"><em>QueryDslPredicateExercutor&lt;T&gt;</em></a> interface.</li>
</ul>
<p>After you have obtained the requested page, you can get a list of entities by calling the <em>getContent()</em> method of the <a href="http://static.springsource.org/spring-data/data-commons/docs/1.1.0.RELEASE/api/org/springframework/data/domain/Page.html"><em>Page&lt;T&gt;</em></a> interface.</p>
<p>Enough with the theory, lets take a look how the given requirements can be implemented with JPA criteria API.</p>
<h2>Adding Pagination to Person Search Results</h2>
<p>The given requirements can be implemented with JPA criteria API by following these steps:</p>
<ul>
<li>Obtain the wanted page number.</li>
<li>Create the needed <em>Specification</em> instance.</li>
<li>Create the instance of a <em>PageRequest</em> class.</li>
<li>Pass the created <em>Specification</em> and <em>PageRequest</em> objects to the person repository.</li>
</ul>
<p>First, I modified the <em>search()</em> method of the <em>PersonService</em> interface. In order to obtain the wanted page from the database, the repository needs to know what page it should be looking for. Thus, I had to add a new parameter to the <em>search()</em> method. The name of this parameter is <em>pageIndex</em> and it specifies the index of the wanted page. The declaration of the new <em>search()</em> methods is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> PersonService <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Searches persons for a given page by using the given search term.<br />
&nbsp; &nbsp; &nbsp;* @param searchTerm<br />
&nbsp; &nbsp; &nbsp;* @param pageIndex<br />
&nbsp; &nbsp; &nbsp;* @return &nbsp;A list of persons whose last name begins with the given search term and who are belonging to the given page.<br />
&nbsp; &nbsp; &nbsp;* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;If no persons is found, this method returns an empty list. This search is case insensitive.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> search<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> searchTerm, <span style="color: #000066; font-weight: bold;">int</span> pageIndex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Second, since I am using the JPA criteria API for building the actual query, the <em>RepositoryPersonService</em> will obtain the needed specification by calling the static <em>lastNameIsLike()</em> method of <em>PersonSpecifications</em> class. The source code of the <em>PersonSpecifications</em> class is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PersonSpecifications <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> Specification<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> lastNameIsLike<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> searchTerm<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Specification<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> Predicate toPredicate<span style="color: #009900;">&#40;</span>Root<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> personRoot, CriteriaQuery<span style="color: #339933;">&lt;?&gt;</span> query, CriteriaBuilder cb<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> likePattern <span style="color: #339933;">=</span> getLikePattern<span style="color: #009900;">&#40;</span>searchTerm<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> cb.<span style="color: #006633;">like</span><span style="color: #009900;">&#40;</span>cb.<span style="color: #006633;">lower</span><span style="color: #009900;">&#40;</span>personRoot.<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span>get<span style="color: #009900;">&#40;</span>Person_.<span style="color: #006633;">lastName</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>, likePattern<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> getLikePattern<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> searchTerm<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StringBuilder pattern <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StringBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pattern.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>searchTerm.<span style="color: #006633;">toLowerCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pattern.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> pattern.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Third, I need to create an instance of a <em>PageRequest</em> class and pass this instance to the <em>PersonRepository</em>. I created a private method called <em>constructPageSpecification()</em> to the <em>RepositoryPersonService</em>. This method simply creates a new instance of the <em>PageRequest</em> object and returns the created instance. The search method obtains the <em>PageRequest</em> instance by calling the <em>constructPageSpecification()</em> method.</p>
<p>The last step is to pass  the created objects forward to the <em>PersonRepository</em>. The source code of the relevant parts of the <em>RepositoryPersonService</em> is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:480px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">@Service<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RepositoryPersonService <span style="color: #000000; font-weight: bold;">implements</span> PersonService <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> Logger LOGGER <span style="color: #339933;">=</span> LoggerFactory.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>RepositoryPersonService.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> NUMBER_OF_PERSONS_PER_PAGE <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Resource<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> PersonRepository personRepository<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Transactional<span style="color: #009900;">&#40;</span>readOnly <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> search<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> searchTerm, <span style="color: #000066; font-weight: bold;">int</span> pageIndex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; LOGGER.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Searching persons with search term: &quot;</span> <span style="color: #339933;">+</span> searchTerm<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Passes the specification created by PersonSpecifications class and the page specification to the repository.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Page requestedPage <span style="color: #339933;">=</span> personRepository.<span style="color: #006633;">findAll</span><span style="color: #009900;">&#40;</span>lastNameIsLike<span style="color: #009900;">&#40;</span>searchTerm<span style="color: #009900;">&#41;</span>, constructPageSpecification<span style="color: #009900;">&#40;</span>pageIndex<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> requestedPage.<span style="color: #006633;">getContent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Returns a new object which specifies the the wanted result page.<br />
&nbsp; &nbsp; &nbsp;* @param pageIndex The index of the wanted result page<br />
&nbsp; &nbsp; &nbsp;* @return<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Pageable</span> constructPageSpecification<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> pageIndex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">Pageable</span> pageSpecification <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PageRequest<span style="color: #009900;">&#40;</span>pageIndex, NUMBER_OF_PERSONS_PER_PAGE, sortByLastNameAsc<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> pageSpecification<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Returns a Sort object which sorts persons in ascending order by using the last name.<br />
&nbsp; &nbsp; &nbsp;* @return<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> Sort sortByLastNameAsc<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Sort<span style="color: #009900;">&#40;</span>Sort.<span style="color: #006633;">Direction</span>.<span style="color: #006633;">ASC</span>, <span style="color: #0000ff;">&quot;lastName&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>I also had to modify the unit tests of the <em>RepositoryPersonService</em> class. The source code of the modified unit test is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:480px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RepositoryPersonServiceTest <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> PERSON_COUNT <span style="color: #339933;">=</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> PAGE_INDEX <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Long</span> PERSON_ID <span style="color: #339933;">=</span> <span style="color: #003399;">Long</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> FIRST_NAME <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Foo&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> FIRST_NAME_UPDATED <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;FooUpdated&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> LAST_NAME <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Bar&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> LAST_NAME_UPDATED <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;BarUpdated&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> SEARCH_TERM <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;foo&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> RepositoryPersonService personService<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> PersonRepository personRepositoryMock<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Before<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; personService <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> RepositoryPersonService<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; personRepositoryMock <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>PersonRepository.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; personService.<span style="color: #006633;">setPersonRepository</span><span style="color: #009900;">&#40;</span>personRepositoryMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> search<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> expected <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Page expectedPage <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PageImpl<span style="color: #009900;">&#40;</span>expected<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>personRepositoryMock.<span style="color: #006633;">findAll</span><span style="color: #009900;">&#40;</span>any<span style="color: #009900;">&#40;</span>Specification.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>, any<span style="color: #009900;">&#40;</span><span style="color: #003399;">Pageable</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>expectedPage<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> actual <span style="color: #339933;">=</span> personService.<span style="color: #006633;">search</span><span style="color: #009900;">&#40;</span>SEARCH_TERM, PAGE_INDEX<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; ArgumentCaptor<span style="color: #339933;">&lt;</span>Pageable<span style="color: #339933;">&gt;</span> pageArgument <span style="color: #339933;">=</span> ArgumentCaptor.<span style="color: #006633;">forClass</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Pageable</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verify<span style="color: #009900;">&#40;</span>personRepositoryMock, times<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">findAll</span><span style="color: #009900;">&#40;</span>any<span style="color: #009900;">&#40;</span>Specification.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>, pageArgument.<span style="color: #006633;">capture</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verifyNoMoreInteractions<span style="color: #009900;">&#40;</span>personRepositoryMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">Pageable</span> pageSpecification <span style="color: #339933;">=</span> pageArgument.<span style="color: #006633;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>PAGE_INDEX, pageSpecification.<span style="color: #006633;">getPageNumber</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>RepositoryPersonService.<span style="color: #006633;">NUMBER_OF_PERSONS_PER_PAGE</span>, pageSpecification.<span style="color: #006633;">getPageSize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>Sort.<span style="color: #006633;">Direction</span>.<span style="color: #006633;">ASC</span>, pageSpecification.<span style="color: #006633;">getSort</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getOrderFor</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;lastName&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getDirection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>expected, actual<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>I have now described the parts of the source code which are using Spring Data JPA for implementing the new requirements. However, my example application has a lot of web application specific &#8220;plumbing&#8221; code in it. I recommend that you take a look of the <a href="https://github.com/pkainulainen/Examples/tree/master/Spring/data-jpa/tutorial-part-seven">source code</a> because it can help you to get a better view of the big picture.</p>
<h2>What is Next?</h2>
<p>I have now demonstrated to you how you can paginate your query results with Spring Data JPA. If you are interested of seeing my example application in action, you can <a href="https://github.com/pkainulainen/Examples/tree/master/Spring/data-jpa/tutorial-part-seven">get it from Github</a>. The next part of my Spring Data JPA tutorial will describe <a href="http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-eight-adding-functionality-to-a-repository/">how you can add custom functionality to your repository</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-seven-pagination/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Spring Data JPA Tutorial Part Six: Sorting</title>
		<link>http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-six-sorting/</link>
		<comments>http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-six-sorting/#comments</comments>
		<pubDate>Sun, 08 Apr 2012 11:00:19 +0000</pubDate>
		<dc:creator>Petri Kainulainen</dc:creator>
				<category><![CDATA[Spring Framework]]></category>
		<category><![CDATA[spring data]]></category>
		<category><![CDATA[spring data jpa]]></category>

		<guid isPermaLink="false">http://www.petrikainulainen.net/?p=2575</guid>
		<description><![CDATA[The fifth part of my Spring Data JPA tutorial described how you can create advanced queries with Spring Data JPA and Querydsl. This blog entry will describe how you can use Spring Data JPA for sorting the query results. As an example I will be adding two new requirements for my example application: The list [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>The fifth part of my Spring Data JPA tutorial described how you can <a href="http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-five-querydsl/">create advanced queries with Spring Data JPA and Querydsl</a>. This blog entry will describe how you can use Spring Data JPA for sorting the query results. As an example I will be adding two new requirements for my example application:</p>
<ul>
<li>The list of persons shown in the front page should be sorted in ascending order by using the last name of a person.</li>
<li>The search results should be sorted in ascending order by using the last name of a person.</li>
</ul>
<p>Spring Data JPA offers four different ways  to sort the query results. Each of these approaches is described with more details in the following Sections.</p>
<h2>Sorting with Method Name</h2>
<p>If you are building your queries by using the <a href="http://static.springsource.org/spring-data/data-jpa/docs/1.0.x/reference/html/#jpa.query-methods.query-creation">query generation from method name strategy</a>, you can sort the query results by using the <em>OrderBy</em> keyword in the name of your query method. If you want to search for persons whose last name matches with the given parameter and sort the query results, you can use the following query methods:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">//Results are sorted in ascending order</span><br />
<span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> findByLastNameOrderByLastNameAsc<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> lastName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">//Results are sorted in descending order</span><br />
<span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> findByLastNameOrderByLastNameDesc<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> lastName<span style="color: #009900;">&#41;</span></div></div>
<h2>Sorting with JPQL</h2>
<p>If you are creating query method by using either <em>@NamedQuery</em> or <em>@Query</em> annotation as explained <a href="http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-three-custom-queries-with-query-methods/">in the third part of this tutorial</a>, you can sort the query results by using the <em>ORDER BY</em> keyword of the JPQL. For example, if you want to get all persons and sort the query results in ascending order by using the last name of a person, you can use the following query:</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> p <span style="color: #993333; font-weight: bold;">FROM</span> Person p <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> p<span style="color: #66cc66;">.</span>lastName <span style="color: #993333; font-weight: bold;">ASC</span></div></div>
<p>You can now consult <a href="http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-three-custom-queries-with-query-methods/">the third part of this tutorial</a> for finding out how you can use the <em>@NamedQuery</em> or <em>@Query</em> annotation to implement the actual query method.</p>
<h2>Sorting with Sort Class</h2>
<p>The second way to sort query results with Spring Data JPA is to use the <a href="http://static.springsource.org/spring-data/data-commons/docs/1.1.0.RELEASE/api/org/springframework/data/domain/Sort.html"><em>org.springframework.data.domain.Sort</em></a> class. You can use this approach for sorting the query results by following these steps:</p>
<ul>
<li>Create an instance of the Sort class.</li>
<li>Pass the created instance as a parameter to the correct method.</li>
</ul>
<p>I am going to describe to you three different ways for sorting the query results by using the Sort class. After that I will choose one of those methods and implement my first requirement with the selected method.</p>
<h3>Query Generation by Method Name</h3>
<p>You can pass the <em>Sort</em> object as a parameter to a query method, which uses the query generation by method name strategy. For instance, if you want search persons by last name and sort the results of the created query, you should add following method to your repository interface:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> findByLastName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> lastName, Sort sort<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>If you are interested of finding out more information about this query generation strategy, you should read the <a href="http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-three-custom-queries-with-query-methods/">third part of this tutorial</a>.</p>
<h3>JpaRepository</h3>
<p>As you might remember from the previous parts of this tutorial, Spring Data JPA repositories are just interfaces which extend other interfaces. One of those special interfaces is the <a href="http://static.springsource.org/spring-data/data-jpa/docs/current/api/org/springframework/data/jpa/repository/JpaRepository.html"><em>JpaRepository&lt;T, ID extends Serializable&gt;</em></a> interface. This interface declares the <em>public List&lt;T&gt; findAll(Sort sort)</em> method, which returns a list of all entities and sorts the entities in the order specified by the Sort object given as a parameter. If you are interested of obtaining a sorted list of all entities, using this method is the best way to do it.</p>
<p>The second part of my Spring Data JPA tutorial <a href="http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-two-crud/">provides more information about the usage of the <em>JpaRepository</em> interface</a>.</p>
<h3>JPA  Criteria API</h3>
<p>If you are creating your queries with the JPA criteria API, your repository interface must extend the <a href="http://static.springsource.org/spring-data/data-jpa/docs/current/api/org/springframework/data/jpa/repository/JpaSpecificationExecutor.html"><em>JpaSpecificationExecutor&lt;T&gt;</em></a> interface. This interface declares the <em>public List&lt;T&gt; findAll(Specification&lt;T&gt; spec, Sort sort)</em> method, which returns a list of entities matching with the Specification given as a parameter. The returned entities are sorted in the order specified by the Sort object given as a parameter. </p>
<p>So, if you are interested of sorting the results of a query constructed by using the JPA criteria API, you can do it by using the described method. If you need more information about Spring Data JPA and the JPA criteria API, you can read <a href="http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-four-jpa-criteria-queries/">the fourth part</a> of this tutorial.</p>
<h3>Example</h3>
<p>I am going to describe next how you can implement the first requirement by using the <em>findAll(Sort sort)</em> method provided by the <em>JpaRepository</em> interface.</p>
<p>First, my repository interface must extend the <em>JpaRepository</em> interface. The source code of the <em>PersonRepository</em> is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> PersonRepository <span style="color: #000000; font-weight: bold;">extends</span> JpaRepository<span style="color: #339933;">&lt;</span>Person, Long<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Second, I created a private method called <em>sortByLastNameAsc()</em> to <em>RepositoryPersonService</em> class. This method is used in the <em>findAll()</em> method to obtain an instance of the <em>Sort</em> class. Third, I passed the obtained <em>Sort</em> object to the <em>findAll()</em> method of the <em>JpaRepository</em> interface. The source code of the relevant parts of the <em>RepositoryPersonService</em> is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:480px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">@Service<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RepositoryPersonService <span style="color: #000000; font-weight: bold;">implements</span> PersonService <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> Logger LOGGER <span style="color: #339933;">=</span> LoggerFactory.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>RepositoryPersonService.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @Resource<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> PersonRepository personRepository<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Transactional<span style="color: #009900;">&#40;</span>readOnly <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> findAll<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; LOGGER.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Finding all persons&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Passes the Sort object to the repository.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> personRepository.<span style="color: #006633;">findAll</span><span style="color: #009900;">&#40;</span>sortByLastNameAsc<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Returns a Sort object which sorts persons in ascending order by using the last name.<br />
&nbsp; &nbsp; &nbsp;* @return<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> Sort sortByLastNameAsc<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Sort<span style="color: #009900;">&#40;</span>Sort.<span style="color: #006633;">Direction</span>.<span style="color: #006633;">ASC</span>, <span style="color: #0000ff;">&quot;lastName&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>The last step was to fix the unit test of the findAll() method. The source code of the fixed unit test is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:550px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RepositoryPersonServiceTest <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> RepositoryPersonService personService<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> PersonRepository personRepositoryMock<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Before<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; personService <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> RepositoryPersonService<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; personRepositoryMock <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>PersonRepository.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; personService.<span style="color: #006633;">setPersonRepository</span><span style="color: #009900;">&#40;</span>personRepositoryMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> findAll<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> persons <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>personRepositoryMock.<span style="color: #006633;">findAll</span><span style="color: #009900;">&#40;</span>any<span style="color: #009900;">&#40;</span>Sort.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>persons<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> returned <span style="color: #339933;">=</span> personService.<span style="color: #006633;">findAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; ArgumentCaptor<span style="color: #339933;">&lt;</span>Sort<span style="color: #339933;">&gt;</span> sortArgument <span style="color: #339933;">=</span> ArgumentCaptor.<span style="color: #006633;">forClass</span><span style="color: #009900;">&#40;</span>Sort.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verify<span style="color: #009900;">&#40;</span>personRepositoryMock, times<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">findAll</span><span style="color: #009900;">&#40;</span>sortArgument.<span style="color: #006633;">capture</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; verifyNoMoreInteractions<span style="color: #009900;">&#40;</span>personRepositoryMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; Sort actualSort <span style="color: #339933;">=</span> sortArgument.<span style="color: #006633;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>Sort.<span style="color: #006633;">Direction</span>.<span style="color: #006633;">ASC</span>, actualSort.<span style="color: #006633;">getOrderFor</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;lastName&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getDirection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>persons, returned<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>I have now demonstrated to you, how you can sort the persons presented in the front page of my example application in ascending order by using the last name of a person.</p>
<h2>Sorting with Querydsl</h2>
<p>If you are using Querydsl for building your queries, you can sort the query results by following these steps:</p>
<ul>
<li>Create an instance of  the <a href="http://www.querydsl.com/static/querydsl/2.1.2/apidocs/com/mysema/query/types/OrderSpecifier.html"><em>com.mysema.query.types.OrderSpecifier</em></a> class.</li>
<li>Pass the created instance as a parameter to the <em>findAll(Predicate predicate, OrderSpecifier order)</em> method of the <a href="http://static.springsource.org/spring-data/data-commons/docs/1.1.0.RELEASE/api/org/springframework/data/querydsl/QueryDslPredicateExecutor.html"><em>QueryDslPredicateExecutor&lt;T&gt;</em></a> interface.</li>
</ul>
<p>I will now describe to you how you can implement my second requirement with Querydsl.</p>
<p>First, I am ensuring that my repository extends the <em>QueryDslPredicateExecutor</em> interface. The source code of the <em>PersonRepository</em> is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> PersonRepository <span style="color: #000000; font-weight: bold;">extends</span> JpaRepository<span style="color: #339933;">&lt;</span>Person, Long<span style="color: #339933;">&gt;</span>, QueryDslPredicateExecutor<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Second, I created a class called <em>PersonPredicates</em> which has one method: <em>lastNameIsLike()</em>. This method is used to build the required query predicate. The source code of this class is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PersonPredicates <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> Predicate lastNameIsLike<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> searchTerm<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; QPerson person <span style="color: #339933;">=</span> QPerson.<span style="color: #006633;">person</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> person.<span style="color: #006633;">lastName</span>.<span style="color: #006633;">startsWithIgnoreCase</span><span style="color: #009900;">&#40;</span>searchTerm<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>The next step was to made some changes to the <em>search()</em> method of the <em>RepositoryPersonService</em> class. This method is explained with more details in following:</p>
<ul>
<li>The predicate is still obtained by calling the <em>lastNameIsLike()</em> method of the <em>PersonPredicates</em> class.</li>
<li>The <em>OrderSpecifier</em> instance is obtained by calling a private <em>orderByLastNameAsc()</em> method of <em>RepositoryPersonService</em> class.</li>
<li>The predicate and the <em>OrderSpecifier</em> instance are passed as a parameter to the <em>findAll(Predicate predicate, OrderSpecifier order)</em> method of my repository implementation.</li>
</ul>
<p>The source code of the relevant parts of the <em>RepositoryPersonService</em> class is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:480px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">@Service<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RepositoryPersonService <span style="color: #000000; font-weight: bold;">implements</span> PersonService <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> Logger LOGGER <span style="color: #339933;">=</span> LoggerFactory.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>RepositoryPersonService.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @Resource<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> PersonRepository personRepository<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @Transactional<span style="color: #009900;">&#40;</span>readOnly <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> search<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> searchTerm<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; LOGGER.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Searching persons with search term: &quot;</span> <span style="color: #339933;">+</span> searchTerm<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Passes the specification created by PersonPredicates class and the OrderSpecifier object to the repository.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Iterable<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> persons <span style="color: #339933;">=</span> personRepository.<span style="color: #006633;">findAll</span><span style="color: #009900;">&#40;</span>lastNameIsLike<span style="color: #009900;">&#40;</span>searchTerm<span style="color: #009900;">&#41;</span>, orderByLastNameAsc<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> constructList<span style="color: #009900;">&#40;</span>persons<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Returns an OrderSpecifier object which sorts person in ascending order by using the last name.<br />
&nbsp; &nbsp; &nbsp;* @return<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> OrderSpecifier<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> orderByLastNameAsc<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> QPerson.<span style="color: #006633;">person</span>.<span style="color: #006633;">lastName</span>.<span style="color: #006633;">asc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> constructList<span style="color: #009900;">&#40;</span>Iterable<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> persons<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> list <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>Person person<span style="color: #339933;">:</span> persons<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>person<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> list<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>The last step was to the fix the unit test of the <em>search()</em> method. The source code of the fixed unit test is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:550px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RepositoryPersonServiceTest <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> SEARCH_TERM <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;foo&quot;</span><span style="color: #339933;">;</span> &nbsp; &nbsp;<br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> RepositoryPersonService personService<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> PersonRepository personRepositoryMock<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Before<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; personService <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> RepositoryPersonService<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; personRepositoryMock <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>PersonRepository.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; personService.<span style="color: #006633;">setPersonRepository</span><span style="color: #009900;">&#40;</span>personRepositoryMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> search<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> expected <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>personRepositoryMock.<span style="color: #006633;">findAll</span><span style="color: #009900;">&#40;</span>any<span style="color: #009900;">&#40;</span>BooleanExpression.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>, any<span style="color: #009900;">&#40;</span>OrderSpecifier.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>expected<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> actual <span style="color: #339933;">=</span> personService.<span style="color: #006633;">search</span><span style="color: #009900;">&#40;</span>SEARCH_TERM<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; ArgumentCaptor<span style="color: #339933;">&lt;</span>OrderSpecifier<span style="color: #339933;">&gt;</span> orderArgument <span style="color: #339933;">=</span> ArgumentCaptor.<span style="color: #006633;">forClass</span><span style="color: #009900;">&#40;</span>OrderSpecifier.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verify<span style="color: #009900;">&#40;</span>personRepositoryMock, times<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">findAll</span><span style="color: #009900;">&#40;</span>any<span style="color: #009900;">&#40;</span>BooleanExpression.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>, orderArgument.<span style="color: #006633;">capture</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; verifyNoMoreInteractions<span style="color: #009900;">&#40;</span>personRepositoryMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; OrderSpecifier actualOrder <span style="color: #339933;">=</span> orderArgument.<span style="color: #006633;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>Order.<span style="color: #006633;">ASC</span>, actualOrder.<span style="color: #006633;">getOrder</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>QPerson.<span style="color: #006633;">person</span>.<span style="color: #006633;">lastName</span>, actualOrder.<span style="color: #006633;">getTarget</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>expected, actual<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>I have now demonstrated to you how you can you use Querydsl to sort the person search results in ascending order by using the last name of a person.</p>
<h2>What is Next?</h2>
<p>I have now described to you three different ways to sort the query results with Spring Data JPA. I have also implemented a fully functional example application, which is available at <a href="https://github.com/pkainulainen/Examples/tree/master/Spring/data-jpa/tutorial-part-six">Github</a>. The next part of my tutorial describes <a href="http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-seven-pagination/">how you can implement pagination with Spring Data JPA</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-six-sorting/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Spring Data JPA Tutorial Part Five: Querydsl</title>
		<link>http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-five-querydsl/</link>
		<comments>http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-five-querydsl/#comments</comments>
		<pubDate>Tue, 28 Feb 2012 20:20:56 +0000</pubDate>
		<dc:creator>Petri Kainulainen</dc:creator>
				<category><![CDATA[Spring Framework]]></category>
		<category><![CDATA[spring data]]></category>
		<category><![CDATA[spring data jpa]]></category>

		<guid isPermaLink="false">http://www.petrikainulainen.net/?p=2532</guid>
		<description><![CDATA[The fourth part of my Spring Data JPA tutorial described how you can implement more advanced queries with the JPA criteria API. As you might remember, the goal of the previous part of this tutorial was to implement a search function which returns only such persons whose last name begins with the given search term. [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>The fourth part of my Spring Data JPA tutorial <a href="http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-four-jpa-criteria-queries/">described how you can implement more advanced queries with the JPA criteria API</a>. As you might remember,  the goal of the previous part of this tutorial was to implement a search function which returns only such persons whose last name begins with the given search term.  This blog entry will describe how you use <a href="http://www.querydsl.com/">Querydsl</a> and <a href="http://www.springsource.org/spring-data/jpa">Spring Data JPA</a> for the same purpose.</p>
<p>Lets get to work and see how you can fulfill this requirement.</p>
<h2>Required Steps</h2>
<p>The steps needed to implement the given requirement are following:</p>
<ul>
<li>Configuring the Maven integration of QueryDSL</li>
<li>Generating the Querydsl query type</li>
<li>Implementing the predicate builder class</li>
<li>Extending the repository to support Querydsl predicates</li>
<li>Using the created repository</li>
</ul>
<p>Each of these steps is described with more details in following Sections.</p>
<h2>Configuring the Maven Integration of Querydsl</h2>
<p>The configuration of the maven integration of Querydsl consists two smaller phases:</p>
<p>First, you need to add the Querydsl dependencies to your <em>pom.xml</em> file.  The needed dependencies are:</p>
<ul>
<li>Querydsl core which provides the core functions of Querydsl.</li>
<li>Querydsl APT integration which provides support for the APT based code generation.</li>
<li>Querydsl JPA which provides support for the JPA annotations.</li>
</ul>
<p>The relevant part of the pom.xml is given in following:</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.mysema.querydsl<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>querydsl-core<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2.3.2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.mysema.querydsl<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>querydsl-apt<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2.3.2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.mysema.querydsl<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>querydsl-jpa<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2.3.2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p>Querydsl uses the Annotation Processing Tool of Java 6 for code generation.  Thus, the next phase is to add the the configuration of the Maven APT plugin to the plugins section of your <em>pom.xml</em> file. The relevant part of the <em>pom.xml</em> looks following:</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.mysema.maven<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>maven-apt-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0.2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;executions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;execution<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;phase<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>generate-sources<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/phase<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;goals<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>process<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/goals<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!-- Specifies the directory in which the query types are generated --&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;outputDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>target/generated-sources<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/outputDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!-- States that the APT code generator should look for JPA annotations --&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;processor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.mysema.query.apt.jpa.JPAAnnotationProcessor<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/processor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/execution<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/executions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<h2>Generating the Querydsl Query Type</h2>
<p>The next step is to generate the Querydsl query type which is used to construct queries with Querydsl. All you have to do is to build your project and the query type is generated under the <em>target/generated-sources</em> directory. If you open this directory,  you should notice that the query type class is created in the same package than the <em>Person</em> class. Since the name of the model class is called <em>Person</em>, the name of the Querydsl query type is <em>QPerson</em>.</p>
<p><strong>NOTE</strong>: Remember to add the <em>target/generated-sources</em> directory as a source directory for your project before moving forward (Check the documentation of your IDE for more details about this).</p>
<h2>Implementing the Predicate Builder Class</h2>
<p>In my previous part of this tutorial, I used a builder class with static methods to build the actual <em>Specification</em> instances. I am following the same principle when building the Querydsl predicates by using the generated <em>QPerson</em> query type. The source code of my predicate builder class is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* A class which is used to create Querydsl predicates.<br />
&nbsp;* @author Petri Kainulainen<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PersonPredicates <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> Predicate lastNameIsLike<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> searchTerm<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; QPerson person <span style="color: #339933;">=</span> QPerson.<span style="color: #006633;">person</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> person.<span style="color: #006633;">lastName</span>.<span style="color: #006633;">startsWithIgnoreCase</span><span style="color: #009900;">&#40;</span>searchTerm<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>At first I had no idea how my builder class could be tested, because the information about unit testing Querydsl is kind of hard to find. Luckily I was able to find a google groups thread, <a href="https://groups.google.com/forum/#!msg/querydsl/mgNozpKdUUU/ERZimS1QKdMJ">which discusses about this matter</a>. After reading that thread I decided that testing the return value of <em>toString()</em> method of the created predicate is enough because this scenario is quite simple. The source code of my unit test is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PersonPredicatesTest <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> SEARCH_TERM <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Foo&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> EXPECTED_PREDICATE_STRING <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;startsWithIgnoreCase(person.lastName,Foo)&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> lastNameLike<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Predicate predicate <span style="color: #339933;">=</span> PersonPredicates.<span style="color: #006633;">lastNameIsLike</span><span style="color: #009900;">&#40;</span>SEARCH_TERM<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> predicateAsString <span style="color: #339933;">=</span> predicate.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>EXPECTED_PREDICATE_STRING, predicateAsString<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>If you have read my blog entry about <a href="http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-four-jpa-criteria-queries/">using the JPA criteria API with Spring Data JPA</a>, you might remember that the unit test of my <em>lastNameLike()</em> method was rather long and looked a bit messy. The problem is that mocking the JPA criteria API becomes a lot more complex when you are building more complex queries. This means that writing pure unit tests for your code takes longer and longer.</p>
<p>My unit test for the Querydsl implementation of the same method is the exact opposite. It is short and looks quite clean. And more importantly, it is much faster to write. This means that you can concentrate on adding value to your application instead of verifying its behavior.</p>
<h2>Extending the Repository to Support Querydsl Predicates</h2>
<p>Extending the repository to support Querydsl predicates is quite straightforward process. All you have to do is to extend the <a href="http://static.springsource.org/spring-data/data-commons/docs/1.1.0.RELEASE/api/org/springframework/data/querydsl/QueryDslPredicateExecutor.html"><em>QueryDslPredicateExecutor</em></a> interface. This gives you access to <em>findAll(Predicate predicate)</em> method. This returns all entities fulfilling the search conditions which are specified by the predicate. The source code of my <em>PersonRepository</em> interface is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* Specifies methods used to obtain and modify person related information<br />
&nbsp;* which is stored in the database.<br />
&nbsp;* @author Petri Kainulainen<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> PersonRepository <span style="color: #000000; font-weight: bold;">extends</span> JpaRepository<span style="color: #339933;">&lt;</span>Person, Long<span style="color: #339933;">&gt;</span>, QueryDslPredicateExecutor<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<h2>Using the Created Repository</h2>
<p>The last step is to implement the service class which uses the created predicate builder and the repository. The <em>PersonService</em> interface contains a method <em>search(String searchTerm)</em> which returns a list of persons matching with the given search term. The relevant part of the <em>PersonService</em> interface is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* Declares methods used to obtain and modify person information.<br />
&nbsp;* @author Petri Kainulainen<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> PersonService <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Searches persons by using the given search term as a parameter.<br />
&nbsp; &nbsp; &nbsp;* @param searchTerm<br />
&nbsp; &nbsp; &nbsp;* @return &nbsp;A list of persons whose last name begins with the given search term. If no persons is found, this method<br />
&nbsp; &nbsp; &nbsp;* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;returns an empty list. This search is case insensitive.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> search<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> searchTerm<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Implementing the search method is pretty straightforward. My implementation uses the <em>PersonPredicates</em> class to obtain the Querydsl predicate and passes the received predicate forward to the <em>PersonRepository</em>. Since the <em>findAll()</em> method returns <em>Iterable</em> instead of <em>List</em>, I had to add an extra method which converts the returned <em>Iterable</em> into a <em>List</em>. The source code of the <em>search(String searchTerm)</em> method is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:550px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* This implementation of the PersonService interface communicates with<br />
&nbsp;* the database by using a Spring Data JPA repository.<br />
&nbsp;* @author Petri Kainulainen<br />
&nbsp;*/</span><br />
@Service<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RepositoryPersonService <span style="color: #000000; font-weight: bold;">implements</span> PersonService <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> Logger LOGGER <span style="color: #339933;">=</span> LoggerFactory.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>RepositoryPersonService.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @Resource<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> PersonRepository personRepository<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Transactional<span style="color: #009900;">&#40;</span>readOnly <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> search<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> searchTerm<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; LOGGER.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Searching persons with search term: &quot;</span> <span style="color: #339933;">+</span> searchTerm<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Passes the specification created by PersonPredicates class to the repository.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Iterable<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> persons <span style="color: #339933;">=</span> personRepository.<span style="color: #006633;">findAll</span><span style="color: #009900;">&#40;</span>lastNameIsLike<span style="color: #009900;">&#40;</span>searchTerm<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> constructList<span style="color: #009900;">&#40;</span>persons<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> constructList<span style="color: #009900;">&#40;</span>Iterable<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> persons<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> list <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>Person person<span style="color: #339933;">:</span> persons<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>person<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> list<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>The same architectural remarks which I made <a href="http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-four-jpa-criteria-queries/">in my last blog entry</a> are also valid in this case. However, the end result looks pretty clean and simple. The only thing which is left, is to write a unit test for the search method. The source code of the unit test is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:550px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RepositoryPersonServiceTest <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> SEARCH_TERM <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;foo&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> RepositoryPersonService personService<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> PersonRepository personRepositoryMock<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Before<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; personService <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> RepositoryPersonService<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; personRepositoryMock <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>PersonRepository.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; personService.<span style="color: #006633;">setPersonRepository</span><span style="color: #009900;">&#40;</span>personRepositoryMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> search<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> expected <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>personRepositoryMock.<span style="color: #006633;">findAll</span><span style="color: #009900;">&#40;</span>any<span style="color: #009900;">&#40;</span>Predicate.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>expected<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> actual <span style="color: #339933;">=</span> personService.<span style="color: #006633;">search</span><span style="color: #009900;">&#40;</span>SEARCH_TERM<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; verify<span style="color: #009900;">&#40;</span>personRepositoryMock, times<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">findAll</span><span style="color: #009900;">&#40;</span>any<span style="color: #009900;">&#40;</span>Predicate.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verifyNoMoreInteractions<span style="color: #009900;">&#40;</span>personRepositoryMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>expected, actual<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<h2>What is Next?</h2>
<p>I have now demonstrated to you how you can build advanced queries with Spring Data JPA and Querydsl. As always, the example application described in this blog entry is available at <a href="https://github.com/pkainulainen/Examples/tree/master/Spring/data-jpa/tutorial-part-five">Github</a>. The next part of my tutorial <a href="http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-six-sorting/">describes the sorting capabilities of Spring Data JPA</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-five-querydsl/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Installing PostgreSQL 9.1 to Windows 7 from the Binary Zip Distribution</title>
		<link>http://www.petrikainulainen.net/programming/tips-and-tricks/installing-postgresql-9-1-to-windows-7-from-the-binary-zip-distribution/</link>
		<comments>http://www.petrikainulainen.net/programming/tips-and-tricks/installing-postgresql-9-1-to-windows-7-from-the-binary-zip-distribution/#comments</comments>
		<pubDate>Thu, 23 Feb 2012 17:41:10 +0000</pubDate>
		<dc:creator>Petri Kainulainen</dc:creator>
				<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[PostgreSQL]]></category>

		<guid isPermaLink="false">http://www.petrikainulainen.net/?p=2505</guid>
		<description><![CDATA[Today I was helping a colleague of mine to install and configure PostgreSQL 9.1 to his laptop which was running on Windows 7. We had several problems with the one click installer and after the installation process was finished, we could not start the PostgreSQL database. Since we could not solve this problem in a [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>Today I was helping a colleague of mine to install and configure PostgreSQL 9.1 to his laptop which was running on Windows 7. We had several problems with the one click installer and after the installation process was finished, we could not start the PostgreSQL database. </p>
<p>Since we could not solve this problem in a reasonable amount of time, we had to use the binary zip distribution. The installation process was quite straightforward but it took some time because I could not find a decent tutorial about it. This blog entry is written to fill that void. Also, even though this blog entry describes the installation process of PostgreSQL 9.1, these instructions should be valid for older versions of PostgreSQL as well.</p>
<p>The required steps are described with more details in later Sections of this blog entry.</p>
<h2>Preparing the PostgreSQL 9.1 Installation</h2>
<p>The first step is to <a href="http://www.enterprisedb.com/products-services-training/pgbindownload">download the binary zip distribution</a>.</p>
<p>After you have downloaded the binary zip distribution, you can unzip the binary distribution to the preferred directory. When this is done, the target directory should contain a directory called <em>&#8216;pgsql&#8217;</em>. This directory contains the binaries of PostgreSQL 9.1. I will refer this directory simply as <em>POSTGRESQL_ROOT</em> (Remember to replace the string <em>POSTGRESQL_ROOT</em> with the actual directory path of you installation when following these instructions). </p>
<p>The next step is to create the data and log directories for your PostgreSQL installation. This is done by creating the following directories to the <em>POSTGRESQL_ROOT</em> directory:</p>
<ul>
<li>The <em>&#8216;data&#8217;</em> directory contains the data files of your PostgreSQL installation.</li>
<li>The <em>&#8216;log&#8217;</em> directory contains the logs of your PostgreSQL installation.</li>
</ul>
<p>You have now finished the needed preparations and can move on to the next phase of the installation process.</p>
<h2>Configuring the PostgreSQL 9.1 Installation</h2>
<p>The next step is to create a new PostgreSQL database cluster. You can do this by using the <a href="http://www.postgresql.org/docs/9.1/static/app-initdb.html">initdb command</a> which is found from the <em>POSTGRESQL_ROOT\bin</em> directory. You can create the database cluster by running the following command from the <em>bin</em> directory of your PostgreSQL installation:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">initdb -U postgres -A password -E utf8 -W -d POSTGRESQL_ROOT\data</div></div>
<p>The command line parameters of the initdb command are described in following:</p>
<ul>
<li><em>-U postgres</em> means that the superuser account of your database is called <em>&#8216;postgres&#8217;</em>.</li>
<li><em>-A password</em> means that password authentication is used.</li>
<li><em>-E utf8</em> means that the default encoding will be UTF-8.</li>
<li><em>-W</em> means that you will enter the superuser password manually.</li>
<li><em>-d POSTGRESQL_ROOT\data</em> specifies the data directory of your PostgreSQL installation.</li>
</ul>
<p>After you have successfully created the database cluster, your PostgreSQL installation is ready to be used. You can start and stop your database instance by using the following commands:</p>
<p>The database can be started by running the following command:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&quot;POSTGRESQL_ROOT/bin/pg_ctl&quot; -D &quot;POSTGRESQL_ROOT/data&quot; -l &quot;POSTGRESQL_ROOT/log/pgsql.log&quot; start</div></div>
<p>The database can be stopped by running the following command:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&quot;POSTGRESQL_ROOT/bin/pg_ctl&quot; -D “POSTGRESQL_ROOT/data&quot; -l &quot;POSTGRESQL_ROOT/log/pgsql.log&quot; stop</div></div>
<p>To make things as easy as possible, you should create new shortcuts to your desktop and use them for starting and stopping the database server.</p>
<h2>Congratulations</h2>
<p>Your PostgreSQL 9.1 installation should now be functional. The next step is to start the database server by using the created shortcut  and verify that you can connect to your database instance. You can use the <a href="http://www.pgadmin.org/">PgAdmin database management tool</a> for this task and congratulate yourself for a job well done.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.petrikainulainen.net/programming/tips-and-tricks/installing-postgresql-9-1-to-windows-7-from-the-binary-zip-distribution/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Spring Data JPA Tutorial Part Four: JPA Criteria Queries</title>
		<link>http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-four-jpa-criteria-queries/</link>
		<comments>http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-four-jpa-criteria-queries/#comments</comments>
		<pubDate>Sun, 12 Feb 2012 18:45:37 +0000</pubDate>
		<dc:creator>Petri Kainulainen</dc:creator>
				<category><![CDATA[Spring Framework]]></category>
		<category><![CDATA[spring data]]></category>
		<category><![CDATA[spring data jpa]]></category>

		<guid isPermaLink="false">http://www.petrikainulainen.net/?p=2487</guid>
		<description><![CDATA[The third part of my Spring Data JPA tutorial described how you can create custom queries by using query methods. This blog entry will describe how you can implement more advanced queries by using the JPA criteria API. If you have read the previous part of this tutorial, you might remember that the search function [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>The third part of my Spring Data JPA tutorial described <a href="http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-three-custom-queries-with-query-methods/">how you can create custom queries by using query methods</a>.  This blog entry will describe how you can implement more advanced queries by using the JPA criteria API.</p>
<p>If you have read the previous part of this tutorial, you might remember that the search function which I used as an example returned all persons whose last name matched with the given search criteria.  This requirement is now replaced with a new one:</p>
<ul>
<li>The search function must return only such persons whose last name begins with the given search term.</li>
</ul>
<p>I am going to walk you through the implementation of this requirement next.</p>
<h2>Required Steps</h2>
<p>The steps required to implement the new search function are following:</p>
<ul>
<li>Creating the JPA criteria query.</li>
<li>Extending the repository to support JPA criteria queries.</li>
<li>Using the created criteria query and repository.</li>
</ul>
<p>Each of these steps is described with more details in following.</p>
<h2>Building the JPA Criteria Query</h2>
<p>Spring Data JPA uses the specification pattern for <a href="http://static.springsource.org/spring-data/data-jpa/docs/1.0.x/reference/html/#specifications">providing an API which is used to create queries with the JPA criteria API</a>. The heart of this API is the <a href="http://static.springsource.org/spring-data/data-jpa/docs/1.0.x/api/org/springframework/data/jpa/domain/Specification.html"><em>Specification</em></a> interface which contains a single method called <em>toPredicate()</em>. In order to build the required criteria query, you must create a new implementation of the <em>Specification</em> interface and build the predicate in the <em>toPredicate()</em> method.</p>
<p>Before going in to the details, I will introduce the source code of my <a href="http://docs.jboss.org/hibernate/core/4.0/hem/en-US/html/metamodel.html#metamodel-static">static metal model class</a> which is used to create type safe queries with the JPA criteria API. The source code of the <em>Person_</em> class is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* A &nbsp;meta model class used to create type safe queries from person<br />
&nbsp;* information.<br />
&nbsp;* @author Petri Kainulainen<br />
&nbsp;*/</span><br />
@StaticMetamodel<span style="color: #009900;">&#40;</span>Person.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Person_ <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">volatile</span> SingularAttribute<span style="color: #339933;">&lt;</span>Person, String<span style="color: #339933;">&gt;</span> lastName<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>A clean way to create specifications is to implement a specification builder class and use static methods to build the actual specification instances. My specification builder class is called <em>PersonSpecifications</em> and its source code given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:550px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* A class which is used to create Specification objects which are used<br />
&nbsp;* to create JPA criteria queries for person information.<br />
&nbsp;* @author Petri Kainulainen<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PersonSpecifications <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Creates a specification used to find persons whose last name begins with<br />
&nbsp; &nbsp; &nbsp;* the given search term. This search is case insensitive.<br />
&nbsp; &nbsp; &nbsp;* @param searchTerm<br />
&nbsp; &nbsp; &nbsp;* @return<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> Specification<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> lastNameIsLike<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> searchTerm<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Specification<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> Predicate toPredicate<span style="color: #009900;">&#40;</span>Root<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> personRoot, CriteriaQuery<span style="color: #339933;">&lt;?&gt;</span> query, CriteriaBuilder cb<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> likePattern <span style="color: #339933;">=</span> getLikePattern<span style="color: #009900;">&#40;</span>searchTerm<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> cb.<span style="color: #006633;">like</span><span style="color: #009900;">&#40;</span>cb.<span style="color: #006633;">lower</span><span style="color: #009900;">&#40;</span>personRoot.<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span>get<span style="color: #009900;">&#40;</span>Person_.<span style="color: #006633;">lastName</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>, likePattern<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> getLikePattern<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> searchTerm<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StringBuilder pattern <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StringBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pattern.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>searchTerm.<span style="color: #006633;">toLowerCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pattern.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> pattern.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Testing my specification builder implementation is pretty straightforward. I used <a href="http://code.google.com/p/mockito/">Mockito mocking framework</a> to mock the JPA criteria API. The source code of my test class is given following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:550px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* @author Petri Kainulainen<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PersonSpecificationsTest <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> SEARCH_TERM <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Foo&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> SEARCH_TERM_LIKE_PATTERN <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;foo%&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> CriteriaBuilder criteriaBuilderMock<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> CriteriaQuery criteriaQueryMock<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> Root<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> personRootMock<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Before<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; criteriaBuilderMock <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>CriteriaBuilder.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; criteriaQueryMock <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>CriteriaQuery.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; personRootMock <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>Root.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> lastNameIsLike<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Path lastNamePathMock <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>Path.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>personRootMock.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>Person_.<span style="color: #006633;">lastName</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>lastNamePathMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; Expression lastNameToLowerExpressionMock <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>Expression.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>criteriaBuilderMock.<span style="color: #006633;">lower</span><span style="color: #009900;">&#40;</span>lastNamePathMock<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>lastNameToLowerExpressionMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; Predicate lastNameIsLikePredicateMock <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>Predicate.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>criteriaBuilderMock.<span style="color: #006633;">like</span><span style="color: #009900;">&#40;</span>lastNameToLowerExpressionMock, SEARCH_TERM_LIKE_PATTERN<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>lastNameIsLikePredicateMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; Specification<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> actual <span style="color: #339933;">=</span> PersonSpecifications.<span style="color: #006633;">lastNameIsLike</span><span style="color: #009900;">&#40;</span>SEARCH_TERM<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Predicate actualPredicate <span style="color: #339933;">=</span> actual.<span style="color: #006633;">toPredicate</span><span style="color: #009900;">&#40;</span>personRootMock, criteriaQueryMock, criteriaBuilderMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; verify<span style="color: #009900;">&#40;</span>personRootMock, times<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>Person_.<span style="color: #006633;">lastName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verifyNoMoreInteractions<span style="color: #009900;">&#40;</span>personRootMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; verify<span style="color: #009900;">&#40;</span>criteriaBuilderMock, times<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">lower</span><span style="color: #009900;">&#40;</span>lastNamePathMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verify<span style="color: #009900;">&#40;</span>criteriaBuilderMock, times<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">like</span><span style="color: #009900;">&#40;</span>lastNameToLowerExpressionMock, SEARCH_TERM_LIKE_PATTERN<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verifyNoMoreInteractions<span style="color: #009900;">&#40;</span>criteriaBuilderMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; verifyZeroInteractions<span style="color: #009900;">&#40;</span>criteriaQueryMock, lastNamePathMock, lastNameIsLikePredicateMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>lastNameIsLikePredicateMock, actualPredicate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>However, if you have to build more complex queries by using this approach, testing your queries will become more troublesome because the JPA criteria API is not the easiest one to mock. In this case it is a good idea to divide the search conditions into multiple specifications and use the <a href="http://static.springsource.org/spring-data/data-jpa/docs/1.0.x/api/org/springframework/data/jpa/domain/Specifications.html"><em>Specifications</em></a> class to combine your specification instances. This way your unit tests don’t become so complex but you can still harness the power of the JPA criteria API in your application.</p>
<h2>Extending the Repository</h2>
<p>Extending your Spring Data JPA repository to support JPA criteria queries is quite easy.  All you have to do is to extend the <a href="http://static.springsource.org/spring-data/data-jpa/docs/1.0.x/api/org/springframework/data/jpa/repository/JpaSpecificationExecutor.html"><em>JpaSpecificationExecutor</em></a> interface. This gives you access to the <em>findAll(Specification spec)</em> method which returns all entities fulfilling the search conditions specified by the specification. The source code of my <em>PersonRepository</em> is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* Specifies methods used to obtain and modify person related information<br />
&nbsp;* which is stored in the database.<br />
&nbsp;* @author Petri Kainulainen<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> PersonRepository <span style="color: #000000; font-weight: bold;">extends</span> JpaRepository<span style="color: #339933;">&lt;</span>Person, Long<span style="color: #339933;">&gt;</span>, JpaSpecificationExecutor<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<h2>Using the Specification Builder and the Repository</h2>
<p>The last step is to implement the service class which uses the created specification builder and the repository. The <em>search()</em> method of the <em>PersonService</em> interface takes the used search term as a parameter. The relevant part of the <em>PersonService</em> interface is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* Declares methods used to obtain and modify person information.<br />
&nbsp;* @author Petri Kainulainen<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> PersonService <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Searches persons by using the given search term as a parameter.<br />
&nbsp; &nbsp; &nbsp;* @param searchTerm<br />
&nbsp; &nbsp; &nbsp;* @return &nbsp;A list of persons whose last name begins with the given search term. If no persons is found, this method<br />
&nbsp; &nbsp; &nbsp;* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;returns an empty list. This search is case insensitive.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> search<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> searchTerm<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>The implementation of the <em>search()</em> method is very simple. It simply passes the search term to the <em>lastNameIsLike()</em> method of the <em>PersonSpecifications</em> class and gives the created specification object to the <em>PersonRepository</em>. The source code of the implementation of the <em>search()</em> method is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:440px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* This implementation of the PersonService interface communicates with<br />
&nbsp;* the database by using a Spring Data JPA repository.<br />
&nbsp;* @author Petri Kainulainen<br />
&nbsp;*/</span><br />
@Service<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RepositoryPersonService <span style="color: #000000; font-weight: bold;">implements</span> PersonService <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> Logger LOGGER <span style="color: #339933;">=</span> LoggerFactory.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>RepositoryPersonService.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @Resource<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> PersonRepository personRepository<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Transactional<span style="color: #009900;">&#40;</span>readOnly <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> search<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> searchTerm<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; LOGGER.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Searching persons with search term: &quot;</span> <span style="color: #339933;">+</span> searchTerm<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Passes the specification created by PersonSpecifications class to the repository.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> personRepository.<span style="color: #006633;">findAll</span><span style="color: #009900;">&#40;</span>lastNameIsLike<span style="color: #009900;">&#40;</span>searchTerm<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>This solution is not perfect from the architectural point of view because it introduces a dependency between the service layer and the Spring Data JPA. A general guideline is that an upper layer should not have any knowledge about the implementation details of the layers located below it.</p>
<p>One solution to this problem would be to create a custom search method and integrate it with the generic repository abstraction as <a href="http://static.springsource.org/spring-data/data-jpa/docs/1.0.x/reference/html/#repositories.custom-implementations">described in the reference manual of Spring Data JPA</a>. However, this would mean that you would have to write a lot of boilerplate code which does not really add any value to your application. Also, since the goal of the Spring Data JPA is to reduce the amount of boilerplate code, I think that my solution is an acceptable compromise between over engineering and getting things done in a clean way.</p>
<p>Oh, this reminds me of something. We should not forget the unit test of the <em>search()</em> method. The source code of this unit test is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:440px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RepositoryPersonServiceTest <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> SEARCH_TERM <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;foo&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> RepositoryPersonService personService<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> PersonRepository personRepositoryMock<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Before<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; personService <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> RepositoryPersonService<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; personRepositoryMock <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>PersonRepository.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; personService.<span style="color: #006633;">setPersonRepository</span><span style="color: #009900;">&#40;</span>personRepositoryMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> search<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> expected <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>personRepositoryMock.<span style="color: #006633;">findAll</span><span style="color: #009900;">&#40;</span>any<span style="color: #009900;">&#40;</span>Specification.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>expected<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> actual <span style="color: #339933;">=</span> personService.<span style="color: #006633;">search</span><span style="color: #009900;">&#40;</span>SEARCH_TERM<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; verify<span style="color: #009900;">&#40;</span>personRepositoryMock, times<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">findAll</span><span style="color: #009900;">&#40;</span>any<span style="color: #009900;">&#40;</span>Specification.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verifyNoMoreInteractions<span style="color: #009900;">&#40;</span>personRepositoryMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>expected, actual<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<h2>What is Next?</h2>
<p>I have now demonstrated to you how you can use the JPA criteria API with Spring Data JPA. As always, the example application of this blog entry is available at <a href="https://github.com/pkainulainen/Examples/tree/master/Spring/data-jpa/tutorial-part-four">Github</a>. The next part of my Spring Data JPA tutorial describes <a href="http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-five-querydsl/">how you can use Querydsl for building queries with Spring Data JPA</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-four-jpa-criteria-queries/feed/</wfw:commentRss>
		<slash:comments>32</slash:comments>
		</item>
		<item>
		<title>Spring Data JPA Tutorial Part Three: Custom Queries with Query Methods</title>
		<link>http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-three-custom-queries-with-query-methods/</link>
		<comments>http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-three-custom-queries-with-query-methods/#comments</comments>
		<pubDate>Sun, 05 Feb 2012 17:55:56 +0000</pubDate>
		<dc:creator>Petri Kainulainen</dc:creator>
				<category><![CDATA[Spring Framework]]></category>
		<category><![CDATA[spring data]]></category>
		<category><![CDATA[spring data jpa]]></category>

		<guid isPermaLink="false">http://www.petrikainulainen.net/?p=2449</guid>
		<description><![CDATA[The second part of my Spring Data JPA tutorial described how you can create a simple CRUD application with Spring Data JPA. This blog entry will describe how you can use query methods for creating custom queries with Spring Data JPA. In order to have a reasonable example, I have created three new requirements for [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>The second part of my Spring Data JPA tutorial described <a href="http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-two-crud/">how you can create a simple CRUD application with Spring Data JPA</a>. This blog entry will describe how you can use query methods for creating custom queries with Spring Data JPA.  In order to have a reasonable example, I have created three new requirements for my example application:</p>
<ul>
<li>It must be possible to search persons by using their last name as a search criteria.</li>
<li>The search function must return only such persons which last name is an exact match with the given search criteria.</li>
<li>The search must be case insensitive.</li>
</ul>
<p>It is time to get to work and start extending the example application.</p>
<h2>Required Steps</h2>
<p>The steps required to fulfill the given requirements are following:</p>
<ul>
<li>Creating a query method.</li>
<li>Using the created query method.</li>
</ul>
<p>Spring Data JPA provides three different approaches for creating custom queries with query methods. Each of these approaches is described in following.</p>
<h2>Query Creation from Method Name</h2>
<p>Spring Data JPA has a built in query creation mechanism which can be used for parsing queries straight from the method name of a query method. This mechanism first removes common prefixes from the method name and parses the constraints of the query from the rest of the method name. The query builder mechanism is described with more details in <a href="http://static.springsource.org/spring-data/data-jpa/docs/current/reference/html/#repositories.query-methods.details">Defining Query Methods Subsection of Spring Data JPA reference documentation</a>.</p>
<p>Using this approach is quite simple. All you have to do is to ensure that the method names of your repository interface are created by combining the property names of an entity object and the supported keywords. The <a href="http://static.springsource.org/spring-data/data-jpa/docs/current/reference/html/#jpa.query-methods.query-creation">Query Creation Subsection of the Spring Data JPA reference documentation</a> has nice examples concerning the usage of supported keywords.</p>
<p>The source code of the repository method which is using this approach is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* Specifies methods used to obtain and modify person related information<br />
&nbsp;* which is stored in the database.<br />
&nbsp;* @author Petri Kainulainen<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> PersonRepository <span style="color: #000000; font-weight: bold;">extends</span> JpaRepository<span style="color: #339933;">&lt;</span>Person, Long<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Finds persons by using the last name as a search criteria.<br />
&nbsp; &nbsp; &nbsp;* @param lastName &nbsp;<br />
&nbsp; &nbsp; &nbsp;* @return &nbsp;A list of persons which last name is an exact match with the given last name.<br />
&nbsp; &nbsp; &nbsp;* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;If no persons is found, this method returns an empty list.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> findByLastName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> lastName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>The strength of this approach is that it is rather fast to implement simple queries. On the other hand,  if your query has many parameters, your method name will be rather long and ugly. Also, if the keyword you need is not supported by Spring Data JPA, you are out of luck.</p>
<p>A good example of this is the fact that at the moment you cannot use the <em>lower</em> keyword in your method names. This means that this approach cannot be used to fulfill the requirements which I specified in the beginning.</p>
<h2>JPA Named Queries</h2>
<p>Spring Data JPA provides also support for  the JPA Named Queries. You have got following alternatives for declaring the named queries:</p>
<ul>
<li>You can use either <em>named-query</em> XML element or <em>@NamedQuery</em> annotation to create named queries with the JPA query language.</li>
<li>You can use either <em>named-native-query</em> XML element or <em>@NamedNative</em> query annotation to create queries with SQL if you are ready to tie your application with a specific database platform.</li>
</ul>
<p>The only thing you have to do to use the created named queries is to name the query method of your repository interface to match with the name of your named query. I have chosen to specify the named query by using <em>@NamedQuery</em> annotation in my entity class.</p>
<p>The source code of the <em>Person</em> class is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:550px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* An entity class which contains the information of a single person.<br />
&nbsp;* @author Petri Kainulainen<br />
&nbsp;*/</span><br />
@<span style="color: #003399;">Entity</span><br />
@NamedQuery<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Person.findByName&quot;</span>, query <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT p FROM Person p WHERE LOWER(p.lastName) = LOWER(?1)&quot;</span><span style="color: #009900;">&#41;</span><br />
@Table<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;persons&quot;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Person <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @Id<br />
&nbsp; &nbsp; @GeneratedValue<span style="color: #009900;">&#40;</span>strategy <span style="color: #339933;">=</span> GenerationType.<span style="color: #006633;">AUTO</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Long</span> id<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @Column<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;creation_time&quot;</span>, nullable <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> creationTime<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @Column<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;first_name&quot;</span>, nullable <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> firstName<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Column<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;last_name&quot;</span>, nullable <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> lastName<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @Column<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;modification_time&quot;</span>, nullable <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> modificationTime<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @Version<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">long</span> version <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Long</span> getId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> id<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Gets a builder which is used to create Person objects.<br />
&nbsp; &nbsp; &nbsp;* @param firstName The first name of the created user.<br />
&nbsp; &nbsp; &nbsp;* @param lastName &nbsp;The last name of the created user.<br />
&nbsp; &nbsp; &nbsp;* @return &nbsp;A new Builder instance.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> Builder getBuilder<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> firstName, <span style="color: #003399;">String</span> lastName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Builder<span style="color: #009900;">&#40;</span>firstName, lastName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Date</span> getCreationTime<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> creationTime<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getFirstName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> firstName<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getLastName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> lastName<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Gets the full name of the person.<br />
&nbsp; &nbsp; &nbsp;* @return &nbsp;The full name of the person.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; @<span style="color: #000000; font-weight: bold;">Transient</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; StringBuilder name <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StringBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; name.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>firstName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; name.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; name.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>lastName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> name.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Date</span> getModificationTime<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> modificationTime<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">long</span> getVersion<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> version<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> update<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> firstName, <span style="color: #003399;">String</span> lastName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">firstName</span> <span style="color: #339933;">=</span> firstName<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">lastName</span> <span style="color: #339933;">=</span> lastName<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @PreUpdate<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> preUpdate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; modificationTime <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @PrePersist<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> prePersist<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">Date</span> now <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; creationTime <span style="color: #339933;">=</span> now<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; modificationTime <span style="color: #339933;">=</span> now<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> ToStringBuilder.<span style="color: #006633;">reflectionToString</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* A Builder class used to create new Person objects.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">class</span> Builder <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Person built<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Creates a new Builder instance.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* @param firstName The first name of the created Person object.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* @param lastName &nbsp;The last name of the created Person object.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Builder<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> firstName, <span style="color: #003399;">String</span> lastName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; built <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; built.<span style="color: #006633;">firstName</span> <span style="color: #339933;">=</span> firstName<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; built.<span style="color: #006633;">lastName</span> <span style="color: #339933;">=</span> lastName<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Builds the new Person object.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* @return &nbsp;The created Person object.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> Person build<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> built<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* This setter method should only be used by unit tests.<br />
&nbsp; &nbsp; &nbsp;* @param id<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> setId<span style="color: #009900;">&#40;</span><span style="color: #003399;">Long</span> id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">id</span> <span style="color: #339933;">=</span> id<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>The relevant part of my <em>PersonRepository</em> interface looks following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* Specifies methods used to obtain and modify person related information<br />
&nbsp;* which is stored in the database.<br />
&nbsp;* @author Petri Kainulainen<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> PersonRepository <span style="color: #000000; font-weight: bold;">extends</span> JpaRepository<span style="color: #339933;">&lt;</span>Person, Long<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Finds person by using the last name as a search criteria.<br />
&nbsp; &nbsp; &nbsp;* @param lastName<br />
&nbsp; &nbsp; &nbsp;* @return &nbsp;A list of persons whose last name is an exact match with the given last name.<br />
&nbsp; &nbsp; &nbsp;* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;If no persons is found, this method returns null.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> findByName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> lastName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Using named queries  is valid option if your application is small or if you have to use native queries. If your application has a lot of custom queries, this approach will litter the code of your entity class with query declarations (You can of course use the XML configuration to avoid this but in my opinion this approach is even more horrible).</p>
<h2>@Query Annotation</h2>
<p>The <em>@Query</em> annotation can be used to create queries by using the JPA query language and to bind these queries directly to the methods of your repository interface. When the query method is called, Spring Data JPA will execute the query specified by the <em>@Query</em> annotation (If there is a collision between the <em>@Query</em> annotation and the named queries, the query specified by using <em>@Query</em> annotation will be executed).</p>
<p>The source code of the repository method which is implemented by using this approach is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* Specifies methods used to obtain and modify person related information<br />
&nbsp;* which is stored in the database.<br />
&nbsp;* @author Petri Kainulainen<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> PersonRepository <span style="color: #000000; font-weight: bold;">extends</span> JpaRepository<span style="color: #339933;">&lt;</span>Person, Long<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Finds a person by using the last name as a search criteria.<br />
&nbsp; &nbsp; &nbsp;* @param lastName<br />
&nbsp; &nbsp; &nbsp;* @return &nbsp;A list of persons whose last name is an exact match with the given last name.<br />
&nbsp; &nbsp; &nbsp;* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;If no persons is found, this method returns an empty list.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; @Query<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT p FROM Person p WHERE LOWER(p.lastName) = LOWER(:lastName)&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> find<span style="color: #009900;">&#40;</span>@Param<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;lastName&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #003399;">String</span> lastName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>This approach gives you access to the JPA query language and keeps your queries in the repository layer where they belong. On the other hand, you cannot use the <em>@Query</em> annotation (I will describe more advanced strategies in the next parts of this tutorial) if the JPA query language cannot be used to create the query you need.</p>
<h2>Using Created Query Methods</h2>
<p>I have now described you three ways to create query methods with Spring Data JPA. The next step is to take a look of the service class which uses the created query methods.</p>
<p>The <em>SearchType</em> enumeration identifies the used query method. Its source code is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* Describes the search type of the search. Legal values are:<br />
&nbsp;* &lt;ul&gt;<br />
&nbsp;* &nbsp; &nbsp; &lt;li&gt;METHOD_NAME which means that the query is obtained from the method name of the query method.&lt;/li&gt;<br />
&nbsp;* &nbsp; &nbsp; &lt;li&gt;NAMED_QUERY which means that a named query is used.&lt;/li&gt;<br />
&nbsp;* &nbsp; &nbsp; &lt;li&gt;QUERY_ANNOTATION which means that the query method annotated with @Query annotation is used.&lt;/li&gt;<br />
&nbsp;* &lt;/ul&gt;<br />
&nbsp;* @author Petri Kainulainen<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">enum</span> SearchType <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; METHOD_NAME,<br />
&nbsp; &nbsp; NAMED_QUERY,<br />
&nbsp; &nbsp; QUERY_ANNOTATION<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>The <em>SearchDTO</em> is a simple DTO object which contains the search term given by the user and identifies the used query method. Its source code is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:550px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* A DTO class which is used as a form object in the search form.<br />
&nbsp;* @author Petri Kainulainen<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SearchDTO <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> searchTerm<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> SearchType searchType<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> SearchDTO<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getSearchTerm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> searchTerm<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setSearchTerm<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> searchTerm<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">searchTerm</span> <span style="color: #339933;">=</span> searchTerm<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> SearchType getSearchType<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> searchType<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setSearchType<span style="color: #009900;">&#40;</span>SearchType searchType<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">searchType</span> <span style="color: #339933;">=</span> searchType<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> ToStringBuilder.<span style="color: #006633;">reflectionToString</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>The <em>PersonService</em> interface has got one new method . The relevant part of the <em>PersonService</em> interface is described in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* Declares methods used to obtain and modify person information.<br />
&nbsp;* @author Petri Kainulainen<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> PersonService <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Searches persons by using the search criteria given as a parameter.<br />
&nbsp; &nbsp; &nbsp;* @param searchCriteria<br />
&nbsp; &nbsp; &nbsp;* @return &nbsp;A list of persons matching with the search criteria. If no persons is found, this method<br />
&nbsp; &nbsp; &nbsp;* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;returns an empty list.<br />
&nbsp; &nbsp; &nbsp;* @throws IllegalArgumentException if search type is not given.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> search<span style="color: #009900;">&#40;</span>SearchDTO searchCriteria<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>The actual implementation of the <em>search()</em> method is responsible of selecting the correct query method and passing the given search term to it. The source code of my <em>search()</em> method implementation is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:550px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* This implementation of the PersonService interface communicates with<br />
&nbsp;* the database by using a Spring Data JPA repository.<br />
&nbsp;* @author Petri Kainulainen<br />
&nbsp;*/</span><br />
@Service<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RepositoryPersonService <span style="color: #000000; font-weight: bold;">implements</span> PersonService <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> Logger LOGGER <span style="color: #339933;">=</span> LoggerFactory.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>RepositoryPersonService.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @Resource<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> PersonRepository personRepository<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Transactional<span style="color: #009900;">&#40;</span>readOnly <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> search<span style="color: #009900;">&#40;</span>SearchDTO searchCriteria<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; LOGGER.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Searching persons with search criteria: &quot;</span> <span style="color: #339933;">+</span> searchCriteria<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> searchTerm <span style="color: #339933;">=</span> searchCriteria.<span style="color: #006633;">getSearchTerm</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SearchType searchType <span style="color: #339933;">=</span> searchCriteria.<span style="color: #006633;">getSearchType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>searchType <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">IllegalArgumentException</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> findPersonsBySearchType<span style="color: #009900;">&#40;</span>searchTerm, searchType<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> findPersonsBySearchType<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> searchTerm, SearchType searchType<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> persons<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>searchType <span style="color: #339933;">==</span> SearchType.<span style="color: #006633;">METHOD_NAME</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LOGGER.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Searching persons by using method name query creation.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; persons <span style="color: #339933;">=</span> personRepository.<span style="color: #006633;">findByLastName</span><span style="color: #009900;">&#40;</span>searchTerm<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>searchType <span style="color: #339933;">==</span> SearchType.<span style="color: #006633;">NAMED_QUERY</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LOGGER.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Searching persons by using named query&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; persons <span style="color: #339933;">=</span> personRepository.<span style="color: #006633;">findByName</span><span style="color: #009900;">&#40;</span>searchTerm<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LOGGER.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Searching persons by using query annotation&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; persons <span style="color: #339933;">=</span> personRepository.<span style="color: #006633;">find</span><span style="color: #009900;">&#40;</span>searchTerm<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> persons<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Naturally the created <em>search()</em> must be tested as well. The source code of the relevant unit tests is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:550px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* @author Petri Kainulainen<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RepositoryPersonServiceTest <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> LAST_NAME <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Bar&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> RepositoryPersonService personService<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> PersonRepository personRepositoryMock<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Before<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; personService <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> RepositoryPersonService<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; personRepositoryMock <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>PersonRepository.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; personService.<span style="color: #006633;">setPersonRepository</span><span style="color: #009900;">&#40;</span>personRepositoryMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> searchWhenSearchTypeIsMethodName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SearchDTO searchCriteria <span style="color: #339933;">=</span> createSearchDTO<span style="color: #009900;">&#40;</span>LAST_NAME, SearchType.<span style="color: #006633;">METHOD_NAME</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> expected <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>personRepositoryMock.<span style="color: #006633;">findByLastName</span><span style="color: #009900;">&#40;</span>searchCriteria.<span style="color: #006633;">getSearchTerm</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>expected<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> actual <span style="color: #339933;">=</span> personService.<span style="color: #006633;">search</span><span style="color: #009900;">&#40;</span>searchCriteria<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; verify<span style="color: #009900;">&#40;</span>personRepositoryMock, times<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">findByLastName</span><span style="color: #009900;">&#40;</span>searchCriteria.<span style="color: #006633;">getSearchTerm</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verifyNoMoreInteractions<span style="color: #009900;">&#40;</span>personRepositoryMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>expected, actual<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> searchWhenSearchTypeIsNamedQuery<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SearchDTO searchCriteria <span style="color: #339933;">=</span> createSearchDTO<span style="color: #009900;">&#40;</span>LAST_NAME, SearchType.<span style="color: #006633;">NAMED_QUERY</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> expected <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>personRepositoryMock.<span style="color: #006633;">findByName</span><span style="color: #009900;">&#40;</span>searchCriteria.<span style="color: #006633;">getSearchTerm</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>expected<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> actual <span style="color: #339933;">=</span> personService.<span style="color: #006633;">search</span><span style="color: #009900;">&#40;</span>searchCriteria<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; verify<span style="color: #009900;">&#40;</span>personRepositoryMock, times<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">findByName</span><span style="color: #009900;">&#40;</span>searchCriteria.<span style="color: #006633;">getSearchTerm</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verifyNoMoreInteractions<span style="color: #009900;">&#40;</span>personRepositoryMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>expected, actual<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> searchWhenSearchTypeIsQueryAnnotation<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SearchDTO searchCriteria <span style="color: #339933;">=</span> createSearchDTO<span style="color: #009900;">&#40;</span>LAST_NAME, SearchType.<span style="color: #006633;">QUERY_ANNOTATION</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> expected <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>personRepositoryMock.<span style="color: #006633;">find</span><span style="color: #009900;">&#40;</span>searchCriteria.<span style="color: #006633;">getSearchTerm</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>expected<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> actual <span style="color: #339933;">=</span> personService.<span style="color: #006633;">search</span><span style="color: #009900;">&#40;</span>searchCriteria<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; verify<span style="color: #009900;">&#40;</span>personRepositoryMock, times<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">find</span><span style="color: #009900;">&#40;</span>searchCriteria.<span style="color: #006633;">getSearchTerm</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verifyNoMoreInteractions<span style="color: #009900;">&#40;</span>personRepositoryMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>expected, actual<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Test<span style="color: #009900;">&#40;</span>expected <span style="color: #339933;">=</span> <span style="color: #003399;">IllegalArgumentException</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> searchWhenSearchTypeIsNull<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SearchDTO searchCriteria <span style="color: #339933;">=</span> createSearchDTO<span style="color: #009900;">&#40;</span>LAST_NAME, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; personService.<span style="color: #006633;">search</span><span style="color: #009900;">&#40;</span>searchCriteria<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; verifyZeroInteractions<span style="color: #009900;">&#40;</span>personRepositoryMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> SearchDTO createSearchDTO<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> searchTerm, SearchType searchType<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SearchDTO searchCriteria <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SearchDTO<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; searchCriteria.<span style="color: #006633;">setSearchTerm</span><span style="color: #009900;">&#40;</span>searchTerm<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; searchCriteria.<span style="color: #006633;">setSearchType</span><span style="color: #009900;">&#40;</span>searchType<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> searchCriteria<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<h2>What is Next?</h2>
<p>I have now described to you how you can use query methods for creating custom queries with Spring Data JPA. If you are interested of seeing my example application in action, you can get it from <a href="https://github.com/pkainulainen/Examples/tree/master/Spring/data-jpa/tutorial-part-three">Github</a>. The next part of my Spring Data JPA tutorial will describe <a href="http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-four-jpa-criteria-queries/">how you can create JPA criteria queries with Spring Data JPA</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-three-custom-queries-with-query-methods/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Spring Data JPA Tutorial Part Two: CRUD</title>
		<link>http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-two-crud/</link>
		<comments>http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-two-crud/#comments</comments>
		<pubDate>Tue, 24 Jan 2012 19:48:06 +0000</pubDate>
		<dc:creator>Petri Kainulainen</dc:creator>
				<category><![CDATA[Spring Framework]]></category>
		<category><![CDATA[spring data]]></category>
		<category><![CDATA[spring data jpa]]></category>

		<guid isPermaLink="false">http://www.petrikainulainen.net/?p=2422</guid>
		<description><![CDATA[The first part of my Spring Data JPA tutorial described, how you configure Spring Data JPA. This blog entry goes a bit deeper and describes how you can use Spring Data JPA for creating a simple CRUD application. The requirements of the application are following: The person must have a first name and last name. [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>The <a href="http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-one-configuration/">first part of my Spring Data JPA tutorial</a> described, how you configure Spring Data JPA. This blog entry goes a bit deeper and describes how you can use Spring Data JPA for creating a simple CRUD application. The requirements of the application are following:</p>
<ul>
<li>The person must have a first name and last name. Both of these properties are mandatory.</li>
<li>It must be possible to list persons.</li>
<li>It must be possible to add new persons.</li>
<li>It must be possible to edit the information of existing persons.</li>
<li>It must be possible to delete persons.</li>
</ul>
<p>I have now described the requirements of the created application. Now it is time to get to work and start implementing it.</p>
<h2>Required Steps</h2>
<p>The implementation of CRUD application can be divided to following steps:</p>
<ul>
<li>Implementing the Person model object</li>
<li>Creating a repository for the Person object</li>
<li>Using the created repository</li>
</ul>
<p>Each of these steps is explained with more details in following.</p>
<h2>Implementing the Model Object</h2>
<p>The implementation of the <em>Person</em> class is pretty simple. However, there are few issues which I would like to point out:</p>
<ul>
<li>A builder is used to to create new instances of <em>Person</em> class. This might seem like over engineering but I like this approach for two reasons: First, it makes code easier to read than using the <a href="http://theyjustdidit.com/telescopic-constructor-pattern/">telescopic constructor pattern</a>. Second, it ensures that you cannot create an object which in an inconsistent state during its construction (This is something that the common <a href="http://en.wikipedia.org/wiki/JavaBean">JavaBeans pattern</a> cannot guarantee).</li>
<li>The only way to change the information stored in a Person object is to call the <em>update()</em> method. I am fan of putting as much logic to the model objects as possible. This approach does not clutter the service layer with domain logic and and ensures that you do not end up with an <a href="http://martinfowler.com/bliki/AnemicDomainModel.html">anemic domain model</a>.</li>
</ul>
<p>The source code of my <em>Person</em> class is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:550px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* An entity class which contains the information of a single person.<br />
&nbsp;* @author Petri Kainulainen<br />
&nbsp;*/</span><br />
@<span style="color: #003399;">Entity</span><br />
@Table<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;persons&quot;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Person <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @Id<br />
&nbsp; &nbsp; @GeneratedValue<span style="color: #009900;">&#40;</span>strategy <span style="color: #339933;">=</span> GenerationType.<span style="color: #006633;">AUTO</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Long</span> id<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @Column<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;creation_time&quot;</span>, nullable <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> creationTime<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @Column<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;first_name&quot;</span>, nullable <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> firstName<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Column<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;last_name&quot;</span>, nullable <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> lastName<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @Column<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;modification_time&quot;</span>, nullable <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> modificationTime<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @Version<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">long</span> version <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Long</span> getId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> id<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Gets a builder which is used to create Person objects.<br />
&nbsp; &nbsp; &nbsp;* @param firstName The first name of the created user.<br />
&nbsp; &nbsp; &nbsp;* @param lastName &nbsp;The last name of the created user.<br />
&nbsp; &nbsp; &nbsp;* @return &nbsp;A new Builder instance.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> Builder getBuilder<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> firstName, <span style="color: #003399;">String</span> lastName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Builder<span style="color: #009900;">&#40;</span>firstName, lastName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Date</span> getCreationTime<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> creationTime<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getFirstName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> firstName<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getLastName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> lastName<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Gets the full name of the person.<br />
&nbsp; &nbsp; &nbsp;* @return &nbsp;The full name of the person.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; @<span style="color: #000000; font-weight: bold;">Transient</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; StringBuilder name <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StringBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; name.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>firstName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; name.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; name.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>lastName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> name.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Date</span> getModificationTime<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> modificationTime<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">long</span> getVersion<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> version<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> update<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> firstName, <span style="color: #003399;">String</span> lastName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">firstName</span> <span style="color: #339933;">=</span> firstName<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">lastName</span> <span style="color: #339933;">=</span> lastName<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @PreUpdate<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> preUpdate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; modificationTime <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @PrePersist<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> prePersist<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">Date</span> now <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; creationTime <span style="color: #339933;">=</span> now<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; modificationTime <span style="color: #339933;">=</span> now<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> ToStringBuilder.<span style="color: #006633;">reflectionToString</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* A Builder class used to create new Person objects.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">class</span> Builder <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Person built<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Creates a new Builder instance.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* @param firstName The first name of the created Person object.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* @param lastName &nbsp;The last name of the created Person object.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Builder<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> firstName, <span style="color: #003399;">String</span> lastName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; built <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; built.<span style="color: #006633;">firstName</span> <span style="color: #339933;">=</span> firstName<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; built.<span style="color: #006633;">lastName</span> <span style="color: #339933;">=</span> lastName<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Builds the new Person object.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* @return &nbsp;The created Person object.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> Person build<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> built<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* This setter method should only be used by unit tests.<br />
&nbsp; &nbsp; &nbsp;* @param id<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> setId<span style="color: #009900;">&#40;</span><span style="color: #003399;">Long</span> id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">id</span> <span style="color: #339933;">=</span> id<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<h2>Creating the Repository</h2>
<p>Implementing a repository which provides CRUD operations for the <em>Person</em> model object is pretty straightforward. All you have to do is to create an interface which extends the <a href="http://static.springsource.org/spring-data/data-jpa/docs/current/api/org/springframework/data/jpa/repository/JpaRepository.html"><em>JpaRepository</em></a> interface. The <em>JpaRepository</em> interface is a JPA specific extension to the <a href="http://static.springsource.org/spring-data/data-commons/docs/1.1.0.RELEASE/api/org/springframework/data/repository/Repository.html"><em>Repository</em></a> interface and it gives you the access to following methods which are used to implement the CRUD application:</p>
<ul>
<li><em>delete(T entity)</em> which deletes the entity given as a parameter.</li>
<li><em>findAll()</em> which returns a list of entities.</li>
<li><em>findOne(ID id)</em> which returns the entity using the id given a parameter as a search criteria.</li>
<li><em>save(T entity)</em> which saves the entity given as a parameter.</li>
</ul>
<p>The source code of my PersonRepository interface is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* Specifies methods used to obtain and modify person related information<br />
&nbsp;* which is stored in the database.<br />
&nbsp;* @author Petri Kainulainen<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> PersonRepository <span style="color: #000000; font-weight: bold;">extends</span> JpaRepository<span style="color: #339933;">&lt;</span>Person, Long<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<h2>Using the Created Repository</h2>
<p>You have now created both the model object and the repository which is needed to communicate with the database. The next step is to implement a service class which acts as an intermediary between the controllers and the implemented repository. The structure of the service layer is described next.</p>
<p>The <em>PersonDTO</em> is a simple DTO object which is used as a form object in my example application. Its source code is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:550px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* A DTO object which is used as a form object<br />
&nbsp;* in create person and edit person forms.<br />
&nbsp;* @author Petri Kainulainen<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PersonDTO <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Long</span> id<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @<span style="color: #003399;">NotEmpty</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> firstName<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @<span style="color: #003399;">NotEmpty</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> lastName<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> PersonDTO<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Long</span> getId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> id<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setId<span style="color: #009900;">&#40;</span><span style="color: #003399;">Long</span> id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">id</span> <span style="color: #339933;">=</span> id<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getFirstName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> firstName<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setFirstName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> firstName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">firstName</span> <span style="color: #339933;">=</span> firstName<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getLastName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> lastName<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setLastName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> lastName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">lastName</span> <span style="color: #339933;">=</span> lastName<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> ToStringBuilder.<span style="color: #006633;">reflectionToString</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>The <em>PersonService</em> interface specifies the methods which are provided by the actual implementation. Its source code is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:550px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* Declares methods used to obtain and modify person information.<br />
&nbsp;* @author Petri Kainulainen<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> PersonService <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Creates a new person.<br />
&nbsp; &nbsp; &nbsp;* @param created &nbsp; The information of the created person.<br />
&nbsp; &nbsp; &nbsp;* @return &nbsp;The created person.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> Person create<span style="color: #009900;">&#40;</span>PersonDTO created<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Deletes a person.<br />
&nbsp; &nbsp; &nbsp;* @param personId &nbsp;The id of the deleted person.<br />
&nbsp; &nbsp; &nbsp;* @return &nbsp;The deleted person.<br />
&nbsp; &nbsp; &nbsp;* @throws PersonNotFoundException &nbsp;if no person is found with the given id.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> Person delete<span style="color: #009900;">&#40;</span><span style="color: #003399;">Long</span> personId<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> PersonNotFoundException<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Finds all persons.<br />
&nbsp; &nbsp; &nbsp;* @return &nbsp;A list of persons.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> findAll<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Finds person by id.<br />
&nbsp; &nbsp; &nbsp;* @param id &nbsp; &nbsp;The id of the wanted person.<br />
&nbsp; &nbsp; &nbsp;* @return &nbsp;The found person. If no person is found, this method returns null.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> Person findById<span style="color: #009900;">&#40;</span><span style="color: #003399;">Long</span> id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Updates the information of a person.<br />
&nbsp; &nbsp; &nbsp;* @param updated &nbsp; The information of the updated person.<br />
&nbsp; &nbsp; &nbsp;* @return &nbsp;The updated person.<br />
&nbsp; &nbsp; &nbsp;* @throws PersonNotFoundException &nbsp;if no person is found with given id.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> Person update<span style="color: #009900;">&#40;</span>PersonDTO updated<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> PersonNotFoundException<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>The source code of the <em>RepositoryPersonService</em> class which implements the <em>PersonService</em> interface is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:550px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* This implementation of the PersonService interface communicates with<br />
&nbsp;* the database by using a Spring Data JPA repository.<br />
&nbsp;* @author Petri Kainulainen<br />
&nbsp;*/</span><br />
@Service<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RepositoryPersonService <span style="color: #000000; font-weight: bold;">implements</span> PersonService <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> Logger LOGGER <span style="color: #339933;">=</span> LoggerFactory.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>RepositoryPersonService.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @Resource<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> PersonRepository personRepository<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Transactional<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> Person create<span style="color: #009900;">&#40;</span>PersonDTO created<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; LOGGER.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Creating a new person with information: &quot;</span> <span style="color: #339933;">+</span> created<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; Person person <span style="color: #339933;">=</span> Person.<span style="color: #006633;">getBuilder</span><span style="color: #009900;">&#40;</span>created.<span style="color: #006633;">getFirstName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, created.<span style="color: #006633;">getLastName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">build</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> personRepository.<span style="color: #006633;">save</span><span style="color: #009900;">&#40;</span>person<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Transactional<span style="color: #009900;">&#40;</span>rollbackFor <span style="color: #339933;">=</span> PersonNotFoundException.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> Person delete<span style="color: #009900;">&#40;</span><span style="color: #003399;">Long</span> personId<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> PersonNotFoundException <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; LOGGER.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Deleting person with id: &quot;</span> <span style="color: #339933;">+</span> personId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; Person deleted <span style="color: #339933;">=</span> personRepository.<span style="color: #006633;">findOne</span><span style="color: #009900;">&#40;</span>personId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>deleted <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LOGGER.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;No person found with id: &quot;</span> <span style="color: #339933;">+</span> personId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> PersonNotFoundException<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; personRepository.<span style="color: #006633;">delete</span><span style="color: #009900;">&#40;</span>deleted<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> deleted<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Transactional<span style="color: #009900;">&#40;</span>readOnly <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> findAll<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; LOGGER.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Finding all persons&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> personRepository.<span style="color: #006633;">findAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Transactional<span style="color: #009900;">&#40;</span>readOnly <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> Person findById<span style="color: #009900;">&#40;</span><span style="color: #003399;">Long</span> id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; LOGGER.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Finding person by id: &quot;</span> <span style="color: #339933;">+</span> id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> personRepository.<span style="color: #006633;">findOne</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Transactional<span style="color: #009900;">&#40;</span>rollbackFor <span style="color: #339933;">=</span> PersonNotFoundException.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> Person update<span style="color: #009900;">&#40;</span>PersonDTO updated<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> PersonNotFoundException <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; LOGGER.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Updating person with information: &quot;</span> <span style="color: #339933;">+</span> updated<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; Person person <span style="color: #339933;">=</span> personRepository.<span style="color: #006633;">findOne</span><span style="color: #009900;">&#40;</span>updated.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>person <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LOGGER.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;No person found with id: &quot;</span> <span style="color: #339933;">+</span> updated.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> PersonNotFoundException<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; person.<span style="color: #006633;">update</span><span style="color: #009900;">&#40;</span>updated.<span style="color: #006633;">getFirstName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, updated.<span style="color: #006633;">getLastName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> person<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* This setter method should be used only by unit tests.<br />
&nbsp; &nbsp; &nbsp;* @param personRepository<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> setPersonRepository<span style="color: #009900;">&#40;</span>PersonRepository personRepository<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">personRepository</span> <span style="color: #339933;">=</span> personRepository<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>The last part of this step was to write unit tests for the <em>RepositoryPersonService</em> class. The source code of these unit tests is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:550px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RepositoryPersonServiceTest <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Long</span> PERSON_ID <span style="color: #339933;">=</span> <span style="color: #003399;">Long</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> FIRST_NAME <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Foo&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> FIRST_NAME_UPDATED <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;FooUpdated&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> LAST_NAME <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Bar&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> LAST_NAME_UPDATED <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;BarUpdated&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> RepositoryPersonService personService<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> PersonRepository personRepositoryMock<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Before<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; personService <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> RepositoryPersonService<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; personRepositoryMock <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>PersonRepository.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; personService.<span style="color: #006633;">setPersonRepository</span><span style="color: #009900;">&#40;</span>personRepositoryMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> create<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; PersonDTO created <span style="color: #339933;">=</span> PersonTestUtil.<span style="color: #006633;">createDTO</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span>, FIRST_NAME, LAST_NAME<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Person persisted <span style="color: #339933;">=</span> PersonTestUtil.<span style="color: #006633;">createModelObject</span><span style="color: #009900;">&#40;</span>PERSON_ID, FIRST_NAME, LAST_NAME<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>personRepositoryMock.<span style="color: #006633;">save</span><span style="color: #009900;">&#40;</span>any<span style="color: #009900;">&#40;</span>Person.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>persisted<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; Person returned <span style="color: #339933;">=</span> personService.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span>created<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; ArgumentCaptor<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> personArgument <span style="color: #339933;">=</span> ArgumentCaptor.<span style="color: #006633;">forClass</span><span style="color: #009900;">&#40;</span>Person.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verify<span style="color: #009900;">&#40;</span>personRepositoryMock, times<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">save</span><span style="color: #009900;">&#40;</span>personArgument.<span style="color: #006633;">capture</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verifyNoMoreInteractions<span style="color: #009900;">&#40;</span>personRepositoryMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; assertPerson<span style="color: #009900;">&#40;</span>created, personArgument.<span style="color: #006633;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>persisted, returned<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> delete<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> PersonNotFoundException <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Person deleted <span style="color: #339933;">=</span> PersonTestUtil.<span style="color: #006633;">createModelObject</span><span style="color: #009900;">&#40;</span>PERSON_ID, FIRST_NAME, LAST_NAME<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>personRepositoryMock.<span style="color: #006633;">findOne</span><span style="color: #009900;">&#40;</span>PERSON_ID<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>deleted<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; Person returned <span style="color: #339933;">=</span> personService.<span style="color: #006633;">delete</span><span style="color: #009900;">&#40;</span>PERSON_ID<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; verify<span style="color: #009900;">&#40;</span>personRepositoryMock, times<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">findOne</span><span style="color: #009900;">&#40;</span>PERSON_ID<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verify<span style="color: #009900;">&#40;</span>personRepositoryMock, times<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">delete</span><span style="color: #009900;">&#40;</span>deleted<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verifyNoMoreInteractions<span style="color: #009900;">&#40;</span>personRepositoryMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>deleted, returned<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @Test<span style="color: #009900;">&#40;</span>expected <span style="color: #339933;">=</span> PersonNotFoundException.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> deleteWhenPersonIsNotFound<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> PersonNotFoundException <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>personRepositoryMock.<span style="color: #006633;">findOne</span><span style="color: #009900;">&#40;</span>PERSON_ID<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; personService.<span style="color: #006633;">delete</span><span style="color: #009900;">&#40;</span>PERSON_ID<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; verify<span style="color: #009900;">&#40;</span>personRepositoryMock, times<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">findOne</span><span style="color: #009900;">&#40;</span>PERSON_ID<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verifyNoMoreInteractions<span style="color: #009900;">&#40;</span>personRepositoryMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> findAll<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> persons <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>personRepositoryMock.<span style="color: #006633;">findAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>persons<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> returned <span style="color: #339933;">=</span> personService.<span style="color: #006633;">findAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; verify<span style="color: #009900;">&#40;</span>personRepositoryMock, times<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">findAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verifyNoMoreInteractions<span style="color: #009900;">&#40;</span>personRepositoryMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>persons, returned<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> findById<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Person person <span style="color: #339933;">=</span> PersonTestUtil.<span style="color: #006633;">createModelObject</span><span style="color: #009900;">&#40;</span>PERSON_ID, FIRST_NAME, LAST_NAME<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>personRepositoryMock.<span style="color: #006633;">findOne</span><span style="color: #009900;">&#40;</span>PERSON_ID<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>person<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; Person returned <span style="color: #339933;">=</span> personService.<span style="color: #006633;">findById</span><span style="color: #009900;">&#40;</span>PERSON_ID<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; verify<span style="color: #009900;">&#40;</span>personRepositoryMock, times<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">findOne</span><span style="color: #009900;">&#40;</span>PERSON_ID<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verifyNoMoreInteractions<span style="color: #009900;">&#40;</span>personRepositoryMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>person, returned<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> update<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> PersonNotFoundException <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; PersonDTO updated <span style="color: #339933;">=</span> PersonTestUtil.<span style="color: #006633;">createDTO</span><span style="color: #009900;">&#40;</span>PERSON_ID, FIRST_NAME_UPDATED, LAST_NAME_UPDATED<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Person person <span style="color: #339933;">=</span> PersonTestUtil.<span style="color: #006633;">createModelObject</span><span style="color: #009900;">&#40;</span>PERSON_ID, FIRST_NAME, LAST_NAME<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>personRepositoryMock.<span style="color: #006633;">findOne</span><span style="color: #009900;">&#40;</span>updated.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>person<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; Person returned <span style="color: #339933;">=</span> personService.<span style="color: #006633;">update</span><span style="color: #009900;">&#40;</span>updated<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; verify<span style="color: #009900;">&#40;</span>personRepositoryMock, times<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">findOne</span><span style="color: #009900;">&#40;</span>updated.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verifyNoMoreInteractions<span style="color: #009900;">&#40;</span>personRepositoryMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; assertPerson<span style="color: #009900;">&#40;</span>updated, returned<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @Test<span style="color: #009900;">&#40;</span>expected <span style="color: #339933;">=</span> PersonNotFoundException.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> updateWhenPersonIsNotFound<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> PersonNotFoundException <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; PersonDTO updated <span style="color: #339933;">=</span> PersonTestUtil.<span style="color: #006633;">createDTO</span><span style="color: #009900;">&#40;</span>PERSON_ID, FIRST_NAME_UPDATED, LAST_NAME_UPDATED<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>personRepositoryMock.<span style="color: #006633;">findOne</span><span style="color: #009900;">&#40;</span>updated.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; personService.<span style="color: #006633;">update</span><span style="color: #009900;">&#40;</span>updated<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; verify<span style="color: #009900;">&#40;</span>personRepositoryMock, times<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">findOne</span><span style="color: #009900;">&#40;</span>updated.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verifyNoMoreInteractions<span style="color: #009900;">&#40;</span>personRepositoryMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> assertPerson<span style="color: #009900;">&#40;</span>PersonDTO expected, Person actual<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>expected.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, actual.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>expected.<span style="color: #006633;">getFirstName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, actual.<span style="color: #006633;">getFirstName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>expected.<span style="color: #006633;">getLastName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, expected.<span style="color: #006633;">getLastName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<h2>What is Next?</h2>
<p>I have now demonstrated to you how you can implement a simple CRUD application with Spring Data JPA. If you are interested of seeing my fully functional example in action, you can get it from <a href="https://github.com/pkainulainen/Examples/tree/master/Spring/data-jpa/tutorial-part-two">Github</a>. The third part of my Spring Data JPA tutorial describes <a href="http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-three-custom-queries-with-query-methods/">how you can create custom queries with query methods</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-two-crud/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Spring Data JPA Tutorial Part One: Configuration</title>
		<link>http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-one-configuration/</link>
		<comments>http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-one-configuration/#comments</comments>
		<pubDate>Sat, 21 Jan 2012 10:19:05 +0000</pubDate>
		<dc:creator>Petri Kainulainen</dc:creator>
				<category><![CDATA[Spring Framework]]></category>
		<category><![CDATA[spring data]]></category>
		<category><![CDATA[spring data jpa]]></category>

		<guid isPermaLink="false">http://www.petrikainulainen.net/?p=2376</guid>
		<description><![CDATA[Spring Data JPA is a project which aims both to simplify the creation of JPA based repositories and to reduce the amount of code needed to communicate with a database. I have been using it for a while at my work and in my personal hobby projects and it has indeed make things a lot [...]]]></description>
			<content:encoded><![CDATA[<p></p><p><a href="http://www.springsource.org/spring-data/jpa">Spring Data JPA</a> is a project which aims both to simplify the creation of JPA based repositories and to reduce the amount of code needed to communicate with a database. I have been using it for a while at my work and in my personal hobby projects and it has indeed make things a lot more simpler and cleaner. Now it is time to share my knowledge with you.</p>
<p>This is the first part of my Spring Data JPA tutorial and it will describe to you, how you can configure Spring Data JPA when you are using Hibernate as your JPA provider. Before we will get started, I want to make one thing straight: This tutorial is not an introductory level tutorial to Hibernate, JPA or Spring. You  must have some experience about these technologies if you want to understand the concepts described  in my Spring Data JPA tutorial. </p>
<p>The dependencies of this tutorial are following:</p>
<ul>
<li>BoneCP 0.7.1.RELEASE (You can use other data source implementations as well)</li>
<li>Hibernate 4.0.1.Final</li>
<li> Spring Framework 3.1.0.RELEASE</li>
<li>Spring Data JPA 1.0.2</li>
<li>Servlet API 3.0</li>
</ul>
<p>Also, since I am using Maven as a build tool, you must install it if you want to run my example application.</p>
<h2>Getting Started</h2>
<p>It is time to get started. You can configure the Spring Data JPA by following these steps:</p>
<ul>
<li>You have to obtain the needed dependencies.</li>
<li>You must configure the needed beans in your Spring application context configuration. The beans needed by the Spring Data JPA are: data source, transaction manager and entity manager factory.</li>
<li>You have to configure the Spring Data JPA.</li>
</ul>
<p>These steps are explained with more details in following:</p>
<h2>Obtaining the Needed Depedencies</h2>
<p>First, you need to obtain the needed dependencies. You can do this by configuring the needed dependencies in your <em>pom.xml</em> file. The <em>pom.xml</em> of my example looks following:</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:420px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;project</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://maven.apache.org/POM/4.0.0&quot;</span> <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;modelVersion<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>4.0.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/modelVersion<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>net.petrikainulainen.spring<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>data-jpa-tutorial-part-one<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;packaging<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>war<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/packaging<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>0.1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Spring Data JPA Tutorial Part One<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Spring Data JPA Tutorial Part One<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;licenses<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;license<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Apache License 2.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://www.apache.org/licenses/LICENSE-2.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/license<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/licenses<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://www.petrikainulainen.net<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;repositories<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;repository<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>repository.jboss.org-public<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>JBoss repository<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>https://repository.jboss.org/nexus/content/groups/public<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/repository<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/repositories<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;properties<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;hibernate.version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>4.0.1.Final<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/hibernate.version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mysql.connector.version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>5.1.18<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mysql.connector.version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;slf4j.version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.6.1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/slf4j.version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;spring.version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>3.1.0.RELEASE<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/spring.version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;project.build.sourceEncoding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>UTF-8<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/project.build.sourceEncoding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/properties<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!-- Spring Framework --&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.springframework<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>spring-beans<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${spring.version}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.springframework<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>spring-core<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${spring.version}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.springframework<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>spring-context-support<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${spring.version}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.springframework<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>spring-context<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${spring.version}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.springframework<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>spring-jdbc<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${spring.version}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.springframework<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>spring-orm<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${spring.version}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.springframework<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>spring-tx<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${spring.version}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!-- Spring MVC --&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.springframework<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>spring-web<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${spring.version}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.springframework<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>spring-webmvc<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${spring.version}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>cglib<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>cglib<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2.2.2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!-- Spring Data JPA --&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.springframework.data<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>spring-data-jpa<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0.2.RELEASE<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!-- Hibernate --&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.hibernate<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>hibernate-core<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${hibernate.version}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.hibernate<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>hibernate-entitymanager<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${hibernate.version}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!-- H2 Database --&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.h2database<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>h2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.3.160<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!-- MySQL JDBC connector --&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!-- If you want to use MySQL, uncomment this dependency declation. --&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!--</span><br />
<span style="color: #808080; font-style: italic;"> &nbsp; &nbsp; &nbsp; &nbsp;&lt;dependency&gt;</span><br />
<span style="color: #808080; font-style: italic;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;groupId&gt;mysql&lt;/groupId&gt;</span><br />
<span style="color: #808080; font-style: italic;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;</span><br />
<span style="color: #808080; font-style: italic;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;version&gt;${mysql.connector.version}&lt;/version&gt;</span><br />
<span style="color: #808080; font-style: italic;"> &nbsp; &nbsp; &nbsp; &nbsp;&lt;/dependency&gt;</span><br />
<span style="color: #808080; font-style: italic;"> &nbsp; &nbsp; &nbsp; &nbsp;--&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!-- PostgreSQL JDBC 4 --&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!-- If you don't want to use PostgreSQL, uncomment this dependency declaration. --&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!--</span><br />
<span style="color: #808080; font-style: italic;"> &nbsp; &nbsp; &nbsp; &nbsp;&lt;dependency&gt;</span><br />
<span style="color: #808080; font-style: italic;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;groupId&gt;postgresql&lt;/groupId&gt;</span><br />
<span style="color: #808080; font-style: italic;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;artifactId&gt;postgresql&lt;/artifactId&gt;</span><br />
<span style="color: #808080; font-style: italic;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;version&gt;9.0-801.jdbc4&lt;/version&gt;</span><br />
<span style="color: #808080; font-style: italic;"> &nbsp; &nbsp; &nbsp; &nbsp;&lt;/dependency&gt;</span><br />
<span style="color: #808080; font-style: italic;"> &nbsp; &nbsp; &nbsp; &nbsp;--&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!-- BoneCP --&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.jolbox<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>bonecp<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>0.7.1.RELEASE<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!-- Servlet API 3.0 --&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>javax.servlet<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>javax.servlet-api<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>3.0.1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>provided<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>javax.servlet<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>jstl<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!-- Logging dependencies --&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.slf4j<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>slf4j-api<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${slf4j.version}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.slf4j<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>slf4j-log4j12<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${slf4j.version}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>log4j<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>log4j<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.2.16<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!-- Testing Dependencies --&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>junit<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>junit<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>4.9<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>test<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;build<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;finalName<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>data-jpa-tutorial-part-one<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/finalName<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugins<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.apache.maven.plugins<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>maven-compiler-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2.3.2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;source<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.6<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/source<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.6<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.apache.maven.plugins<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>maven-war-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2.1.1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;failOnMissingWebXml<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>false<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/failOnMissingWebXml<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.mortbay.jetty<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>jetty-maven-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>8.1.0.RC2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scanIntervalSeconds<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scanIntervalSeconds<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;webAppConfig<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;defaultsDescriptor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>src/main/resources/webdefault.xml<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/defaultsDescriptor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/webAppConfig<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.apache.maven.plugins<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>maven-site-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>3.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;reportPlugins<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!-- Cobertura Plugin --&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.codehaus.mojo<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>cobertura-maven-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2.5.1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/reportPlugins<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugins<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/build<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/project<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<h2>Configuring the Spring Application Context</h2>
<p>Second, you must configure the Spring application context. As you might remember, you need to configure the data source, transaction manager and entity manager factory beans. If you are using Spring 3.1 and Servlet 3.0, you can do this by implementing a Java configuration class and loading that configuration class in your web application initializer. The content of my application context configuration class is given in following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:550px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* An application context Java configuration class. The usage of Java configuration<br />
&nbsp;* requires Spring Framework 3.0 or higher with following exceptions:<br />
&nbsp;* &lt;ul&gt;<br />
&nbsp;* &nbsp; &nbsp; &lt;li&gt;@EnableWebMvc annotation requires Spring Framework 3.1&lt;/li&gt;<br />
&nbsp;* &lt;/ul&gt;<br />
&nbsp;* @author Petri Kainulainen<br />
&nbsp;*/</span><br />
@Configuration<br />
@ComponentScan<span style="color: #009900;">&#40;</span>basePackages <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;net.petrikainulainen.spring.datajpa.controller&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><br />
@EnableWebMvc<br />
@ImportResource<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;classpath:applicationContext.xml&quot;</span><span style="color: #009900;">&#41;</span><br />
@PropertySource<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;classpath:application.properties&quot;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ApplicationContext <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> VIEW_RESOLVER_PREFIX <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/WEB-INF/jsp/&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> VIEW_RESOLVER_SUFFIX <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;.jsp&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> PROPERTY_NAME_DATABASE_DRIVER <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;db.driver&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> PROPERTY_NAME_DATABASE_PASSWORD <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;db.password&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> PROPERTY_NAME_DATABASE_URL <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;db.url&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> PROPERTY_NAME_DATABASE_USERNAME <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;db.username&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> PROPERTY_NAME_HIBERNATE_DIALECT <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;hibernate.dialect&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> PROPERTY_NAME_HIBERNATE_FORMAT_SQL <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;hibernate.format_sql&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> PROPERTY_NAME_HIBERNATE_NAMING_STRATEGY <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;hibernate.ejb.naming_strategy&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> PROPERTY_NAME_HIBERNATE_SHOW_SQL <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;hibernate.show_sql&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;entitymanager.packages.to.scan&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> PROPERTY_NAME_MESSAGESOURCE_BASENAME <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;message.source.basename&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> PROPERTY_NAME_MESSAGESOURCE_USE_CODE_AS_DEFAULT_MESSAGE <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;message.source.use.code.as.default.message&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Resource<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Environment</span> environment<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Bean<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> DataSource dataSource<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; BoneCPDataSource dataSource <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> BoneCPDataSource<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; dataSource.<span style="color: #006633;">setDriverClass</span><span style="color: #009900;">&#40;</span>environment.<span style="color: #006633;">getRequiredProperty</span><span style="color: #009900;">&#40;</span>PROPERTY_NAME_DATABASE_DRIVER<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; dataSource.<span style="color: #006633;">setJdbcUrl</span><span style="color: #009900;">&#40;</span>environment.<span style="color: #006633;">getRequiredProperty</span><span style="color: #009900;">&#40;</span>PROPERTY_NAME_DATABASE_URL<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; dataSource.<span style="color: #006633;">setUsername</span><span style="color: #009900;">&#40;</span>environment.<span style="color: #006633;">getRequiredProperty</span><span style="color: #009900;">&#40;</span>PROPERTY_NAME_DATABASE_USERNAME<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; dataSource.<span style="color: #006633;">setPassword</span><span style="color: #009900;">&#40;</span>environment.<span style="color: #006633;">getRequiredProperty</span><span style="color: #009900;">&#40;</span>PROPERTY_NAME_DATABASE_PASSWORD<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> dataSource<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Bean<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> JpaTransactionManager transactionManager<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">ClassNotFoundException</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; JpaTransactionManager transactionManager <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JpaTransactionManager<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; transactionManager.<span style="color: #006633;">setEntityManagerFactory</span><span style="color: #009900;">&#40;</span>entityManagerFactoryBean<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getObject</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> transactionManager<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Bean<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> LocalContainerEntityManagerFactoryBean entityManagerFactoryBean<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">ClassNotFoundException</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; LocalContainerEntityManagerFactoryBean entityManagerFactoryBean <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LocalContainerEntityManagerFactoryBean<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; entityManagerFactoryBean.<span style="color: #006633;">setDataSource</span><span style="color: #009900;">&#40;</span>dataSource<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; entityManagerFactoryBean.<span style="color: #006633;">setPackagesToScan</span><span style="color: #009900;">&#40;</span>environment.<span style="color: #006633;">getRequiredProperty</span><span style="color: #009900;">&#40;</span>PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; entityManagerFactoryBean.<span style="color: #006633;">setPersistenceProviderClass</span><span style="color: #009900;">&#40;</span>HibernatePersistence.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">Properties</span> jpaProterties <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Properties</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; jpaProterties.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>PROPERTY_NAME_HIBERNATE_DIALECT, environment.<span style="color: #006633;">getRequiredProperty</span><span style="color: #009900;">&#40;</span>PROPERTY_NAME_HIBERNATE_DIALECT<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; jpaProterties.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>PROPERTY_NAME_HIBERNATE_FORMAT_SQL, environment.<span style="color: #006633;">getRequiredProperty</span><span style="color: #009900;">&#40;</span>PROPERTY_NAME_HIBERNATE_FORMAT_SQL<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; jpaProterties.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>PROPERTY_NAME_HIBERNATE_NAMING_STRATEGY, environment.<span style="color: #006633;">getRequiredProperty</span><span style="color: #009900;">&#40;</span>PROPERTY_NAME_HIBERNATE_NAMING_STRATEGY<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; jpaProterties.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>PROPERTY_NAME_HIBERNATE_SHOW_SQL, environment.<span style="color: #006633;">getRequiredProperty</span><span style="color: #009900;">&#40;</span>PROPERTY_NAME_HIBERNATE_SHOW_SQL<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; entityManagerFactoryBean.<span style="color: #006633;">setJpaProperties</span><span style="color: #009900;">&#40;</span>jpaProterties<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> entityManagerFactoryBean<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Bean<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> MessageSource messageSource<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ResourceBundleMessageSource messageSource <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ResourceBundleMessageSource<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; messageSource.<span style="color: #006633;">setBasename</span><span style="color: #009900;">&#40;</span>environment.<span style="color: #006633;">getRequiredProperty</span><span style="color: #009900;">&#40;</span>PROPERTY_NAME_MESSAGESOURCE_BASENAME<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; messageSource.<span style="color: #006633;">setUseCodeAsDefaultMessage</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Boolean</span>.<span style="color: #006633;">parseBoolean</span><span style="color: #009900;">&#40;</span>environment.<span style="color: #006633;">getRequiredProperty</span><span style="color: #009900;">&#40;</span>PROPERTY_NAME_MESSAGESOURCE_USE_CODE_AS_DEFAULT_MESSAGE<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> messageSource<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Bean<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> ViewResolver viewResolver<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; InternalResourceViewResolver viewResolver <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> InternalResourceViewResolver<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; viewResolver.<span style="color: #006633;">setViewClass</span><span style="color: #009900;">&#40;</span>JstlView.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; viewResolver.<span style="color: #006633;">setPrefix</span><span style="color: #009900;">&#40;</span>VIEW_RESOLVER_PREFIX<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; viewResolver.<span style="color: #006633;">setSuffix</span><span style="color: #009900;">&#40;</span>VIEW_RESOLVER_SUFFIX<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> viewResolver<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>My web application initializer looks like this:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:440px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* Web application Java configuration class. The usage of web application<br />
&nbsp;* initializer requires Spring Framework 3.1 and Servlet 3.0.<br />
&nbsp;* @author Petri Kainulainen<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DataJPAExampleInitializer <span style="color: #000000; font-weight: bold;">implements</span> WebApplicationInitializer <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> DISPATCHER_SERVLET_NAME <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;dispatcher&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> DISPATCHER_SERVLET_MAPPING <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onStartup<span style="color: #009900;">&#40;</span>ServletContext servletContext<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> ServletException <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; AnnotationConfigWebApplicationContext rootContext <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AnnotationConfigWebApplicationContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; rootContext.<span style="color: #006633;">register</span><span style="color: #009900;">&#40;</span>ApplicationContext.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; ServletRegistration.<span style="color: #006633;">Dynamic</span> dispatcher <span style="color: #339933;">=</span> servletContext.<span style="color: #006633;">addServlet</span><span style="color: #009900;">&#40;</span>DISPATCHER_SERVLET_NAME, <span style="color: #000000; font-weight: bold;">new</span> DispatcherServlet<span style="color: #009900;">&#40;</span>rootContext<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; dispatcher.<span style="color: #006633;">setLoadOnStartup</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; dispatcher.<span style="color: #006633;">addMapping</span><span style="color: #009900;">&#40;</span>DISPATCHER_SERVLET_MAPPING<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; servletContext.<span style="color: #006633;">addListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ContextLoaderListener<span style="color: #009900;">&#40;</span>rootContext<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>You might have noticed that I used the <a href="http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/context/annotation/PropertySource.html">@PropertySource annotation</a> to specify the location of a properties file which contains the values of used configuration parameters. The content of my <em>application.properties</em> file is following:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:520px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"># The default database is H2 memory database but I have also<br />
# added configuration needed to use either MySQL and PostgreSQL.<br />
<br />
#Database Configuration<br />
db.driver=org.h2.Driver<br />
#db.driver=com.mysql.jdbc.Driver<br />
#db.driver=org.postgresql.Driver<br />
db.url=jdbc:h2:mem:datajpa<br />
#db.url=jdbc:mysql://localhost:3306/datajpa<br />
#db.url=jdbc:postgresql://localhost/datajpa<br />
db.username=sa<br />
db.password=<br />
<br />
#Hibernate Configuration<br />
hibernate.dialect=org.hibernate.dialect.H2Dialect<br />
#hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect<br />
#hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect<br />
hibernate.format_sql=true<br />
hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy<br />
hibernate.show_sql=true<br />
<br />
#MessageSource<br />
message.source.basename=i18n/messages<br />
message.source.use.code.as.default.message=true<br />
<br />
#EntityManager<br />
#Declares the base package of the entity classes<br />
entitymanager.packages.to.scan=net.petrikainulainen.spring.datajpa.model</div></div>
<h2>Configuring Spring Data JPA</h2>
<p>Third, you must configure the Spring Data JPA. If you were paying attention, you might have noticed that I used the <a href="http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/context/annotation/ImportResource.html">@ImportResource annotation</a> in my application context configuration class to import additional configuration from a XML configuration file. At the moment Spring Data JPA does not support Java configuration. Thus, the only way to configure it is to use a XML configuration file. My a<em>pplicationContext.xml</em> file looks following:</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:560px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; <span style="color: #000066;">xmlns:jpa</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/data/jpa&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; <span style="color: #000066;">xmlns:mvc</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/mvc&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;</span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp;http://www.springframework.org/schema/beans</span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp;http://www.springframework.org/schema/beans/spring-beans-3.1.xsd</span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp;http://www.springframework.org/schema/data/jpa</span><br />
<span style="color: #009900;"> &nbsp; &nbsp;http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd</span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp;http://www.springframework.org/schema/mvc</span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp;http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!--</span><br />
<span style="color: #808080; font-style: italic;"> &nbsp; &nbsp; &nbsp; &nbsp;Configures the location of static resources such as css files.</span><br />
<span style="color: #808080; font-style: italic;"> &nbsp; &nbsp; &nbsp; &nbsp;Requires Spring Framework 3.0 or higher.</span><br />
<span style="color: #808080; font-style: italic;"> &nbsp; &nbsp;--&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mvc:resources</span> <span style="color: #000066;">mapping</span>=<span style="color: #ff0000;">&quot;/static/**&quot;</span> <span style="color: #000066;">location</span>=<span style="color: #ff0000;">&quot;/static/&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!--</span><br />
<span style="color: #808080; font-style: italic;"> &nbsp; &nbsp; &nbsp; &nbsp;Ensures that dispatcher servlet can be mapped to '/' and static resources</span><br />
<span style="color: #808080; font-style: italic;"> &nbsp; &nbsp; &nbsp; &nbsp;are still served by the containers default servlet. Requires Spring Framework</span><br />
<span style="color: #808080; font-style: italic;"> &nbsp; &nbsp; &nbsp; &nbsp;3.0 or higher.</span><br />
<span style="color: #808080; font-style: italic;"> &nbsp; &nbsp;--&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mvc:default-servlet-handler</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!--</span><br />
<span style="color: #808080; font-style: italic;"> &nbsp; &nbsp; &nbsp; &nbsp;Configures Spring Data JPA and sets the base package of my DAOs.</span><br />
<span style="color: #808080; font-style: italic;"> &nbsp; &nbsp;--&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jpa:repositories</span> <span style="color: #000066;">base-package</span>=<span style="color: #ff0000;">&quot;net.petrikainulainen.spring.datajpa.repository&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<h2>You Are Done</h2>
<p>That is it. I have now demonstrated to you how you can configure the Spring Data JPA.  I also have created an example application to demonstrate that this configuration is actually working. You can test the configuration yourself by <a href="https://github.com/pkainulainen/Examples/tree/master/Spring/data-jpa/tutorial-part-one">getting my example application from Github</a> and running the example web application by using the Maven Jetty plugin (<strong>Note</strong>: Remember to create the <em>model</em> and <em>repository</em> packages first. <a href="https://git.wiki.kernel.org/articles/g/i/t/GitFaq_ebc3.html#Can_I_add_empty_directories.3F">Since it is not possible to add empty directories to the Git staging area</a>, the Github repository does not have them either). </p>
<p>The <a href="http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-two-crud/">second part of my Spring Data JPA tutorial</a> describes how you can create a simple CRUD web application by using Spring Data JPA. Stay tuned.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-one-configuration/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

