Skip to main content

Posts

Reactive Spring - CRUD with MongoDB

Greetings! When we learn something, it is good to have something to play with. Now is the time to connect with a database. Working with Reactive Programming is fun but it brings extra challenges. Connecting to a database is such a challenge as we can't use blocking drivers. Lucky for us, Spring comes with Data Repository for MongoDB. Assumptions: You are comfortable with Spring Boot REST You have installed or have access to the MongoDB database The completed project is here -  reactive-super-heroes What to expect? We will follow Spring's usual layers. The main difference is we are using a Reactive Repository instead of the usual blocking repository. We will build a simple REST application with CRUD operations to store superheroes. Create the project Add database connection Create entity (document), dtos Create repository Create service Create controller Demo Create the project Go to start.spring.io and create a project with the below dependencies. S...

Reactive Spring - Getting Started

Greetings! Reactive programming is very popular now. Every passionate Java developer wants to try out it with Webflux which is Spring's way of reactive web development.  In this first tutorial, I am not trying to explain Reactive Programming or Spring WebFlux or anything in detail. Instead, I would like to just play with it. Let's practice a little then come to theory on another day. Assumption : You know how to create and run a Spring Boot application. Warning : Do not try hard, just play with it. Full source code of this article -  reactive-spring-hello Create the project Go to Spring initializer ( https://start.spring.io/ ) and fill out project metadata. Add the below dependencies. Spring Reactive Web, Lombok Download and open the project with your desired IDE. Say hello Reactively Let's quickly create an endpoint (Controller) and add the below contents. Compile and run it. http://localhost:8080/mono, http://localhost:8080/flux package com.slmanju.reactivespringhell...

Understanding Hibernate @BatchSize

Greetings! @BatchSize is one of those misunderstood concepts in Hibernate. I myself thought it was for the collection but I was wrong. Not convinced yet? let's find it out. (Simple example repo can be found here https://github.com/slmanju/hibernate-batchsize ) Let's assume we have a one-to-many relationship with Foo and Bar where Foo is the parent. Whether this is lazy or eager does not matter but I'll use Lazy loading. @Entity @Table(name = "foo") public class Foo { @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL) @JoinColumn(name="foo_id") private List<Bar> bars = new ArrayList<>(); } I will have 5 Foos each having 10 Bars. for (int i = 0; i < 5; i++) { Foo foo = Foo.getInstance("Foo-" + i); for (int j = 0; j < 10; j++) { foo.addBar(Bar.getInstance("Bar-" + i + "-" + j)); } entityManager.persist(foo); } Single Entity When we try to access the Bar collection from Foo what ...

Ruling the code with Rules Design Pattern

Greetings! As a big fan of Chain Of Responsibility design pattern I was thinking to move out the if condition into a separate method so that I can loop through chain externally. It turned out that I end up implementing Rules Design Pattern. I first encountered the pattern few years ago in a C# article ( https://www.michael-whelan.net/rules-design-pattern/ ). You can find the pattern in this course patterns-library . Rules Design Pattern In this pattern, we decouple the business rules from their processing by encapsulating them in to separate classes. We can add/remove rules without impacting the existing logic. This looks much like the Chain of Responsibility except processing rule is separated. One glitch I have faced is we are unable share one rules data with another as the rule and processing are decoupled. Anyway, it is worth to try. I'm not going re-invent an example. Instead i'll use the same example as in C# article but with minimum logic. public class Customer { pr...

Let's Restore Agile

Greetings! I have been working in Agile environments for different companies for many years. We had/have a Scrum Master and Daily Standup, and that is it. That is Agile. This is what made me think "daily meetings are for bad managers". I started reading, practicing more on Agile and found this ( how-keep-your-team-from-getting-agile-wrong-hasitha-liyanage ) interesting article where I got more courage to apply Agile principles, values into my team. Where to Start? The first thing anyone should do is to read agile manifesto. It quite amusing that even certifed scrum masters/managers have no proper understanding on the manifesto. It Is Not About Processes The very first princile in Agile is "team over process". What does mean is, processes have value but the team has more value. For an example we don't need Daily meetings if the team decided so. It is the team decide how to work. Supoort and Trust Have you ever felt Trusted and Supported? If the answer is NO, you ...