<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jonathan Tavares</title>
    <description>The latest articles on DEV Community by Jonathan Tavares (@jonathantvrs).</description>
    <link>https://dev.to/jonathantvrs</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1022611%2Fa99f56cb-99b5-4f9d-a990-7d4f4fba0d6f.jpeg</url>
      <title>DEV Community: Jonathan Tavares</title>
      <link>https://dev.to/jonathantvrs</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jonathantvrs"/>
    <language>en</language>
    <item>
      <title>How to use JPA API Specification?</title>
      <dc:creator>Jonathan Tavares</dc:creator>
      <pubDate>Mon, 11 Mar 2024 12:40:43 +0000</pubDate>
      <link>https://dev.to/jonathantvrs/how-to-use-jpa-api-specification-12o8</link>
      <guid>https://dev.to/jonathantvrs/how-to-use-jpa-api-specification-12o8</guid>
      <description>&lt;p&gt;Hey, this is my first article here. I hope that I'll be able to post content about technology more consistently from now on. &lt;/p&gt;

&lt;p&gt;I've chosen to bring you an advanced JPA 2 topic: the Specification API.&lt;/p&gt;

&lt;p&gt;Specification was a concept that Eric Evans brought up in his book Domain Driven Design(DDD). Based on this, JPA brings this concept as a set of interfaces that facilitate the reuse of predicates, using the Criteria API. That's it, simples as that.&lt;/p&gt;

&lt;p&gt;Let's look an example:  &lt;/p&gt;

&lt;p&gt;We have a book model with id, author, title, isbn and publication year.   &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;and We have a book repository interface that extends JpaSpecificationExecutor&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The JpaSpecificationExecutor provides two methods: &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Now to build our query, We can define the class that implements the Specification interface. &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;With this static method &lt;em&gt;booksByAuthor&lt;/em&gt;, we can use this specification to filter for all books that have an author in common. Let's see how simple it is below: &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;One aspect of the specification that makes it more interesting is the fact that we can combine the queries. Let's look an example:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;And the specification class would look like this: &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;That's it. Thank you very much for reading this content. I usually bring content with a more hands-on approach and if something wasn't so clear, feel free to comment and suggest improvements. &lt;/p&gt;

&lt;p&gt;If you want to know more about this content, I recommend the official documentation: &lt;br&gt;
&lt;a href="https://docs.spring.io/spring-data/jpa/reference/jpa/specifications.html"&gt;JPA Specifications&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>spring</category>
      <category>jpa</category>
      <category>api</category>
    </item>
  </channel>
</rss>
