<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ spring data - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ spring data - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Sat, 27 Jun 2026 22:33:36 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/spring-data/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ Learn Spring Boot and Spring Data JPA ]]>
                </title>
                <description>
                    <![CDATA[ By mastering Spring Boot and Spring Data JPA, you'll be equipped to build efficient, scalable, and secure applications with ease, making you a valuable asset in the job market and a more effective developer. We just published a comprehensive video co... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/learn-spring-boot-and-spring-data-jpa/</link>
                <guid isPermaLink="false">66b2050d39b555ffda8bfea8</guid>
                
                    <category>
                        <![CDATA[ spring-boot ]]>
                    </category>
                
                    <category>
                        <![CDATA[ spring data ]]>
                    </category>
                
                    <category>
                        <![CDATA[ youtube ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Beau Carnes ]]>
                </dc:creator>
                <pubDate>Tue, 06 Feb 2024 16:05:20 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2024/02/springboot.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By mastering Spring Boot and Spring Data JPA, you'll be equipped to build efficient, scalable, and secure applications with ease, making you a valuable asset in the job market and a more effective developer.</p>
<p>We just published a comprehensive video course on the freeCodeCamp.org YouTube channel that teaches Spring Boot and Spring Data JPA, two pivotal technologies in the Java ecosystem. Bouali Ali created this course. Bouali is an experienced developer and teacher.</p>
<p>Spring Boot is an open-source, micro-framework from the larger Spring Framework, designed to simplify the bootstrapping and development of new Spring applications. Its primary goal is to ease the development process by offering a range of out-of-the-box features for configuration, along with embedded servers to facilitate a straightforward setup of web applications. Spring Boot's convention over configuration approach significantly reduces the amount of manual configuration required, making it a preferred choice for developers aiming to deploy applications quickly.</p>
<p>Spring Data JPA, on the other hand, is a part of the larger Spring Data family, aiming to simplify data access within SQL databases. It abstracts boilerplate CRUD operations, providing a more straightforward way to interact with databases through the Java Persistence API (JPA). Spring Data JPA integrates seamlessly with Spring Boot, offering an intuitive approach to handling database operations, reducing the complexity and the amount of code developers need to write.</p>
<p>This course is designed for both beginners and experienced developers who wish to deepen their understanding of Spring Boot and Spring Data JPA.  Here's an overview of the key topics covered in the course:</p>
<h4 id="heading-introduction-to-spring-framework-and-spring-boot">Introduction to Spring Framework and Spring Boot</h4>
<ul>
<li>Overview of Spring Framework</li>
<li>Introduction to Spring Boot</li>
<li>Setting up a Spring Boot project</li>
<li>Understanding Spring Boot auto-configuration</li>
</ul>
<h4 id="heading-spring-boot-basics">Spring Boot Basics</h4>
<ul>
<li>Building RESTful web services with Spring Boot</li>
<li>Spring Boot application properties</li>
<li>Logging with Spring Boot</li>
<li>Building a CRUD API</li>
</ul>
<h4 id="heading-database-access-with-spring-data-jpa">Database Access with Spring Data JPA</h4>
<ul>
<li>Configuring a data source in Spring Boot</li>
<li>Introduction to Spring Data JPA</li>
<li>Implementing repositories</li>
<li>Entity relationships and cascading</li>
<li>Transactions and locking</li>
</ul>
<h4 id="heading-advanced-spring-boot-features">Advanced Spring Boot Features</h4>
<ul>
<li>Securing Spring Boot applications with Spring Security</li>
<li>Token-based authentication</li>
<li>Spring Boot with OAuth2</li>
<li>Microservices with Spring Boot</li>
<li>Deploying Spring Boot applications</li>
</ul>
<h4 id="heading-testing">Testing</h4>
<ul>
<li>Writing unit tests for Spring Boot applications</li>
<li>Integration testing with Spring Boot</li>
</ul>
<h4 id="heading-spring-boot-best-practices">Spring Boot Best Practices</h4>
<ul>
<li>Effective logging practices</li>
<li>Exception handling</li>
<li>Application monitoring with Actuator</li>
<li>Tips for production-ready applications</li>
</ul>
<p>With a blend of theoretical concepts and practical demonstrations, the course aims to equip you with the knowledge and skills required to build robust, efficient applications. Watch the full course <a target="_blank" href="https://www.youtube.com/watch?v=5rNk7m_zlAg">on the freeCodeCamp.org YouTube channel</a> (13-hour watch).</p>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/5rNk7m_zlAg" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ An introduction to Reactive Relational Database Access with Spring and R2DBC ]]>
                </title>
                <description>
                    <![CDATA[ By Daniel Newton Not too long ago, a reactive variant of the JDBC driver was released, known as R2DBC. It allows data to be streamed asynchronously to any endpoints that have subscribed to it. Using a reactive driver like R2DBC together with Spring, ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/an-introduction-to-reactive-relational-database-access-with-spring-and-r2dbc-1a9447d4b122/</link>
                <guid isPermaLink="false">66c3445452e2abc555bfcc03</guid>
                
                    <category>
                        <![CDATA[ Java ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Kotlin ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ spring data ]]>
                    </category>
                
                    <category>
                        <![CDATA[ tech  ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Sat, 16 Feb 2019 10:28:54 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*Y5vRk0tztWvhhbWrvJrm-g.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Daniel Newton</p>
<p>Not too long ago, a reactive variant of the JDBC driver was released, known as R2DBC. It allows data to be streamed asynchronously to any endpoints that have subscribed to it. Using a reactive driver like R2DBC together with Spring, WebFlux allows you to write a full application that handles receiving and sending of data asynchronously.</p>
<p>In this post, we will focus on the database, from connecting to the database and then finally saving and retrieving data. To do this, we will be using Spring Data. As with all Spring Data modules, it provides us with out of the box configuration. This decreases the amount of boilerplate code that we need to write to get our application setup. On top of that, it provides a layer upon the database driver that makes doing the simple tasks easier and the more difficult tasks a little less painful.</p>
<p>For the content of this post, I am making use of a Postgres database. At the time of writing, only Postgres, H2 and Microsoft SQL Server have their own implementations of R2DBC drivers.</p>
<p>I have previously written two posts about reactive Spring Data libraries, one on <a target="_blank" href="https://lankydanblog.com/2017/07/16/a-quick-look-into-reactive-streams-with-spring-data-and-mongodb/">Mongo</a> and another about <a target="_blank" href="https://lankydanblog.com/2017/12/11/reactive-streams-with-spring-data-cassandra/">Cassandra</a>. You might have noticed that neither of these databases are RDBMS databases. Now there are other reactive drivers available for a long time (I wrote the Mongo post nearly 2 years ago) but at the time of writing a reactive driver for a RDBMS database is still a pretty new thing. This post will follow a similar format to those.</p>
<p>Furthermore, I have also written a post about using <a target="_blank" href="https://lankydanblog.com/2018/03/15/doing-stuff-with-spring-webflux/">Spring WebFlux</a> which I mentioned in the introduction. Feel free to have a look at that if you are interested in producing a fully reactive web application.</p>
<h3 id="heading-dependencies">Dependencies</h3>
<p>There are a few things to point out here.</p>
<p>The more you use Spring Boot, the more you will get used to importing a single <code>spring-boot-starter</code> dependency for the cool thing that you want to do. For example, I hoped that there would have been a <code>spring-boot-starter-r2dbc</code> dependency, but unfortunately, there is not one. Yet.</p>
<p>Simply put, this library is on the newer side and at the time of writing, does not have its own Spring Boot module that contains any dependencies it needs along with faster setup via auto-configuration. I am sure these things will come at some point and make setting up a R2DBC driver even easier.</p>
<p>For now, we will need to fill in a few extra dependencies manually.</p>
<p>Furthermore, the R2DBC libraries only have Milestone releases (more proof of them being new) so we need to make sure we bring in the Spring Milestone repository. I will probably need to update this post in the future when it gets a release version.</p>
<h3 id="heading-connecting-to-the-database">Connecting to the database</h3>
<p>Thanks to Spring Data doing a lot of the work for us, the only Bean that needs to be created manually is the <code>ConnectionFactory</code> that contains the database’s connection details:</p>
<p>The first thing to notice here is the extension of <code>AbstractR2dbcConfiguration</code>. This class contains a load of Beans that we no longer need to manually create. Implementing <code>connectionFactory</code> is the only requirement of the class as it is required to create the <code>DatabaseClient</code> Bean. This sort of structure is typical of Spring Data modules, so it feels quite familiar when trying out a different one. Furthermore, I’d expect this manual configuration to be removed once auto-configuration is available and be solely driven via the <code>application.properties</code>.</p>
<p>I have included the <code>port</code> property here, but if you have not played around with your Postgres configuration then you can rely on the default value of <code>5432</code>.</p>
<p>The four properties: <code>host</code>, <code>database</code>, <code>username</code> and <code>password</code> defined by the <code>PostgresqlConnectionFactory</code> are the bare minimum to get it working. Any less and you will experience exceptions during startup.</p>
<p>Using this configuration, Spring is able to connect to a running Postgres instance.</p>
<p>The final piece of noteworthy information from this example is the use of <code>@EnableR2dbcRepositories</code>. This annotation instructs Spring to find any repository interfaces that extend Spring’s <code>Repository</code> interface. This is used as the base interface for instrumenting Spring Data repositories. We will look at this a little closer in the next section. The main piece of information to take away from here is that you need to use the <code>@EnableR2dbcRepositories</code> annotation to fully leverage Spring Data’s capabilities.</p>
<h3 id="heading-creating-a-spring-data-repository">Creating a Spring Data Repository</h3>
<p>As touched on above, in this section we will look at adding a Spring Data Repository. These repositories are a nice feature of Spring Data, meaning that you don’t need to write out a load of extra code to simply write a query.</p>
<p>Unfortunately, at least for now, Spring R2DBC cannot infer queries in the same way that other Spring Data modules currently do (I am sure this will be added at some point). This means that you will need to use the <code>@Query</code> annotation and write the SQL by hand. Let’s take a look:</p>
<p>This interface extends <code>R2dbcRepository</code>. This in turn extends <code>ReactiveCrudRepository</code> and then down to <code>Repository</code>. <code>ReactiveCrudRepository</code> provides the standard CRUD functions and from what I understand, <code>R2dbcRepository</code> does not provide any extra functions and is instead an interface created for better situational naming.</p>
<p><code>R2dbcRepository</code> takes in two generic parameters, one being the entity class that it takes as input and produces as output. The second being the type of the Primary Key. Therefore in this situation, the <code>Person</code> class is being managed by the <code>PersonRepository</code> (makes sense) and the Primary Key field inside <code>Person</code> is an <code>Int</code>.</p>
<p>The return types of functions in this class and the ones provided by <code>ReactiveCrudRepository</code> are <code>Flux</code> and <code>Mono</code> (not seen here). These are Project Reactor types that Spring makes use of as the default Reactive Stream types. <code>Flux</code> represents a stream of multiple elements whereas a <code>Mono</code> is a single result.</p>
<p>Finally, as I mentioned before the example, each function is annotated with <code>@Query</code>. The syntax is quite straight forward, with the SQL being a string inside the annotation. The <code>$1</code> (<code>$2</code>, <code>$3</code>, etc… for more inputs) represents the value input into the function. Once you have done this, Spring will handle the rest and pass the input(s) into their respective input parameter, gather the results and map it to the repository’s designated entity class.</p>
<h3 id="heading-a-very-quick-look-at-the-entity">A very quick look at the entity</h3>
<p>Not going to say much here but simply show the <code>Person</code> class used by the <code>PersonRepository</code>.</p>
<p>Actually, there is one point to make here. <code>id</code> has been made nullable and provided a default value of <code>null</code> to allow Postgres to generate the next suitable value itself. If this is not nullable and an <code>id</code> value is provided, Spring will actually try to run an update instead of an insert upon saving. There are other ways around this, but I think this is good enough.</p>
<p>This entity will map to the <code>people</code> table defined below:</p>
<h3 id="heading-seeing-it-all-in-action">Seeing it all in action</h3>
<p>Now let’s have a look at it actually doing something. Below is some code that inserts a few records and retrieves them in a few different ways:</p>
<p>One thing I will mention about this code. There is a very real possibility that it executes without actually inserting or reading some of the records. But, when you think about it, it makes sense. Reactive applications are meant to do things asynchronously and therefore this application has started processing the function calls in different threads. Without blocking the main thread, these asynchronous processes might never fully execute. For this reason, there are some <code>Thread.sleep</code> calls in this code, but I removed them from the example to keep everything tidy.</p>
<p>The output for running the code above would look something like the below:</p>
<pre><code>[ main] onSubscribe(FluxConcatMap.ConcatMapImmediate)[ main] request(unbounded)[actor-tcp-nio<span class="hljs-number">-1</span>] onNext(Person(id=<span class="hljs-number">35</span>, name=Dan Newton, age=<span class="hljs-number">25</span>))[actor-tcp-nio<span class="hljs-number">-1</span>] onNext(Person(id=<span class="hljs-number">36</span>, name=Laura So, age=<span class="hljs-number">23</span>))[actor-tcp-nio<span class="hljs-number">-1</span>] onComplete()[actor-tcp-nio<span class="hljs-number">-2</span>] findAll - Person(id=<span class="hljs-number">35</span>, name=Dan Newton, age=<span class="hljs-number">25</span>)[actor-tcp-nio<span class="hljs-number">-2</span>] findAll - Person(id=<span class="hljs-number">36</span>, name=Laura So, age=<span class="hljs-number">23</span>)[actor-tcp-nio<span class="hljs-number">-4</span>] findAllByName - Person(id=<span class="hljs-number">36</span>, name=Laura So, age=<span class="hljs-number">23</span>)[actor-tcp-nio<span class="hljs-number">-5</span>] findAllByAge - Person(id=<span class="hljs-number">35</span>, name=Dan Newton, age=<span class="hljs-number">25</span>)
</code></pre><p>A few things to take away here:</p>
<ul>
<li><code>onSubscribe</code> and <code>request</code> occur on the main thread where the <code>Flux</code> was called from. Only <code>saveAll</code> outputs this since it has included the <code>log</code> function. Adding this to the other calls would have lead to the same result of logging to the main thread.</li>
<li>The execution contained within the subscribe function and the internal steps of the <code>Flux</code> are run on separate threads.</li>
</ul>
<p>This is not anywhere close to a real representation of how you would use Reactive Streams in an actual application but hopefully demonstrates how to use them and gives a bit of insight into how they execute.</p>
<h3 id="heading-conclusion">Conclusion</h3>
<p>In conclusion, Reactive Streams have come to some RDBMS databases thanks to the R2DBC driver and Spring Data that builds a layer on top to make everything a bit tidier. By using Spring Data R2DBC we are able to create a connection to a database and start querying it without the need of too much code.</p>
<p>Although Spring is already doing a lot for us, it could be doing more. Currently, it does not have Spring Boot auto-configuration support. Which is a bit annoying. But, I am sure that someone will get around to doing it soon and make everything even better than it already is.</p>
<p>The code used in this post can be found on my <a target="_blank" href="https://github.com/lankydan/spring-data-r2dbc">GitHub</a>.</p>
<p>If you found this post helpful, you can follow me on Twitter at <a target="_blank" href="https://twitter.com/LankyDanDev">@LankyDanDev</a> to keep up with my new posts.</p>
<p><a target="_blank" href="https://lankydanblog.com/author/danknewton/">View all posts by Dan Newton</a></p>
<p><em>Originally published at <a target="_blank" href="https://lankydanblog.com/2019/02/16/asynchronous-rdbms-access-with-spring-data-r2dbc/">lankydanblog.com</a> on February 16, 2019.</em></p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
