<?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: Kaweesha Marasinghe</title>
    <description>The latest articles on DEV Community by Kaweesha Marasinghe (@kaweeshamr).</description>
    <link>https://dev.to/kaweeshamr</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%2F1380769%2F4733d2a7-1a80-448e-a042-46466cffc49e.png</url>
      <title>DEV Community: Kaweesha Marasinghe</title>
      <link>https://dev.to/kaweeshamr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kaweeshamr"/>
    <language>en</language>
    <item>
      <title>Turbocharging TypeScript: What Happens When You Rebuild It in Go</title>
      <dc:creator>Kaweesha Marasinghe</dc:creator>
      <pubDate>Sun, 03 Aug 2025 05:52:40 +0000</pubDate>
      <link>https://dev.to/kaweeshamr/turbocharging-typescript-what-happens-when-you-rebuild-it-in-go-3ih1</link>
      <guid>https://dev.to/kaweeshamr/turbocharging-typescript-what-happens-when-you-rebuild-it-in-go-3ih1</guid>
      <description>&lt;h1&gt;
  
  
  Why Microsoft Is Rewriting the TypeScript Compiler in Go
&lt;/h1&gt;

&lt;p&gt;A few months ago, Microsoft announced they’re rewriting the TypeScript compiler — not the language — from JavaScript (running on Node.js) to &lt;strong&gt;Go&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The goal?&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Faster compiler performance&lt;/strong&gt;, not faster TypeScript apps.&lt;/p&gt;

&lt;h3&gt;
  
  
  Think of It Like This:
&lt;/h3&gt;

&lt;p&gt;If you're building cars and upgrade your car manufacturing machines, the cars themselves don’t suddenly drive faster. But now, you can build those cars much faster.&lt;/p&gt;

&lt;p&gt;Same thing here:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;TypeScript compiler&lt;/strong&gt; is the manufacturing machine.
&lt;/li&gt;
&lt;li&gt;Microsoft is rebuilding that machine in &lt;strong&gt;Go&lt;/strong&gt; to make it faster and more efficient.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;The language (TypeScript) itself stays the same.&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
What’s changing is the engine behind it.&lt;/p&gt;




&lt;h2&gt;
  
  
  Node.js Architecture Breakdown
&lt;/h2&gt;

&lt;p&gt;For years, the TypeScript compiler has run on &lt;strong&gt;Node.js&lt;/strong&gt;, which uses Google’s &lt;strong&gt;V8 engine&lt;/strong&gt; (same as Chrome).&lt;/p&gt;

&lt;p&gt;Here’s what powers Node.js:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;V8&lt;/strong&gt;: Written in C/C++, compiles JavaScript to machine code using &lt;strong&gt;JIT (Just-In-Time)&lt;/strong&gt; compilation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;libuv&lt;/strong&gt;: C library that handles async I/O like file access and network calls.&lt;/li&gt;
&lt;li&gt;Many of Node’s performance advantages come from these native under-the-hood parts.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But there’s a catch:&lt;/p&gt;

&lt;h3&gt;
  
  
  Node.js is great for I/O and memory-heavy work — but &lt;strong&gt;not for CPU-heavy tasks&lt;/strong&gt;.
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Examples:
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;I/O-bound&lt;/strong&gt; (Node is good at):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Database queries
&lt;/li&gt;
&lt;li&gt;Network/API calls
&lt;/li&gt;
&lt;li&gt;File system operations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Memory-bound&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Parsing JSON
&lt;/li&gt;
&lt;li&gt;Data transformations
&lt;/li&gt;
&lt;li&gt;Routing HTTP requests&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But when you throw &lt;strong&gt;CPU-intensive&lt;/strong&gt; jobs at it (like compiling a codebase), Node slows down.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Node.js Struggles with Compilers
&lt;/h2&gt;

&lt;p&gt;Node.js is &lt;strong&gt;single-threaded by default&lt;/strong&gt;.&lt;br&gt;&lt;br&gt;
If one heavy task (like compiling) runs, it &lt;strong&gt;blocks the event loop&lt;/strong&gt;, so:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No new requests get handled
&lt;/li&gt;
&lt;li&gt;Everything waits until that one job finishes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That’s a big problem for something like a compiler.&lt;/p&gt;

&lt;h2&gt;
  
  
  So... Is a Compiler CPU-Bound?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Yes — compilers are classic CPU-bound programs.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;They don’t just read files or wait on databases. They do serious processing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Type checking
&lt;/li&gt;
&lt;li&gt;Code analysis
&lt;/li&gt;
&lt;li&gt;Transformations
&lt;/li&gt;
&lt;li&gt;Output generation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These tasks hit the &lt;strong&gt;CPU&lt;/strong&gt; hard — not I/O, not memory.&lt;/p&gt;

&lt;p&gt;As TypeScript has added more features (generics, decorators, etc.), the compiler has gotten &lt;strong&gt;more complex&lt;/strong&gt; and CPU-hungry.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Go Is a Better Fit
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Go’s Superpower: Goroutines
&lt;/h3&gt;

&lt;p&gt;Go has a built-in concurrency model — &lt;strong&gt;goroutines&lt;/strong&gt; — which are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lightweight threads
&lt;/li&gt;
&lt;li&gt;Can run thousands at once
&lt;/li&gt;
&lt;li&gt;Managed by Go’s &lt;strong&gt;own scheduler&lt;/strong&gt;, not the OS&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That means Go can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Type-check multiple files in parallel
&lt;/li&gt;
&lt;li&gt;Transform code without freezing
&lt;/li&gt;
&lt;li&gt;Generate output concurrently
&lt;/li&gt;
&lt;li&gt;All &lt;strong&gt;without blocking anything else&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Compare that to Node.js, which relies on workarounds (like worker threads) to get any kind of parallelism.&lt;/p&gt;

&lt;p&gt;Another advantage: &lt;strong&gt;Go produces native executables&lt;/strong&gt; — &lt;code&gt;.exe&lt;/code&gt; for Windows, ELF for Linux, etc.&lt;/p&gt;

&lt;p&gt;No need for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Node.js runtime
&lt;/li&gt;
&lt;li&gt;JS engine like V8
&lt;/li&gt;
&lt;li&gt;External dependencies&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That makes it perfect for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;CLI tools&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Build systems&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Fast startup and distribution&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Compare this to Node.js:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Needs V8 at runtime
&lt;/li&gt;
&lt;li&gt;Slower cold starts
&lt;/li&gt;
&lt;li&gt;More dependencies to carry&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Microsoft’s Smart Move
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Anders Hejlsberg said they evaluated multiple languages, and Go was the easiest to port the code base into. The TypeScript team has apparently created a tool that generates Go code, resulting in a port that's nearly line-for-line equivalent in many places. That might lead you to think the code is "doing the same thing," but that's a misconception.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This isn’t just a performance tweak — it’s a &lt;strong&gt;strategic shift&lt;/strong&gt; in how dev tools are built.&lt;/p&gt;

&lt;h2&gt;
  
  
  What About Browser Support?
&lt;/h2&gt;

&lt;p&gt;The current TypeScript compiler runs in browser tools like the TS Playground. But Go doesn’t naturally run in browsers.&lt;/p&gt;

&lt;p&gt;So how could this work?&lt;/p&gt;

&lt;h3&gt;
  
  
  Possible Approaches:
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1. WebAssembly (WASM)
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Go can be compiled to WASM
&lt;/li&gt;
&lt;li&gt;Works in browsers
&lt;/li&gt;
&lt;li&gt;Has &lt;strong&gt;limitations&lt;/strong&gt; (e.g., no full multithreading)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Might be used for basic tasks (like playgrounds or small apps).&lt;/p&gt;

&lt;h4&gt;
  
  
  2. Cloud Compilation
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Browser sends code to a remote server running the Go compiler
&lt;/li&gt;
&lt;li&gt;Server returns compiled output/errors
&lt;/li&gt;
&lt;li&gt;Very scalable, browser stays lightweight&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  3. Dual Compiler Strategy
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Keep the JS-based compiler for browsers
&lt;/li&gt;
&lt;li&gt;Use the Go compiler for CLI/build tools/IDEs
&lt;/li&gt;
&lt;li&gt;Harder to maintain both, but possible&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  4. Lite Browser Version
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Stripped-down version for type-checking/syntax validation
&lt;/li&gt;
&lt;li&gt;Could use WASM or JS under the hood
&lt;/li&gt;
&lt;li&gt;Good enough for teaching and small projects&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Microsoft hasn’t officially confirmed their plan yet.&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
These are educated guesses based on blog posts and engineering trends.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Final Thoughts&lt;br&gt;
Microsoft’s decision to rewrite the TypeScript compiler in Go isn’t just about raw speed — it’s about building a modern, scalable toolchain that matches the complexity of today’s codebases. Go’s native concurrency, efficient memory usage, and ability to produce fast binaries make it a perfect fit for powering large-scale developer tools.&lt;/p&gt;

&lt;p&gt;While this move introduces questions around browser support, the likely hybrid model (Go for CLI, JS/WASM for browser) shows that Microsoft is thinking long-term.&lt;/p&gt;

&lt;p&gt;In the end, the language we write (TypeScript) stays the same — but the tools we use to build and scale with it are evolving. And that evolution, powered by Go, could shape the future of how fast, reliable, and portable our dev workflows become.&lt;/p&gt;

</description>
      <category>go</category>
      <category>devops</category>
      <category>cloudnative</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Object-Oriented Concepts: The Foundation of Modern Programming - Part 01</title>
      <dc:creator>Kaweesha Marasinghe</dc:creator>
      <pubDate>Sat, 18 Jan 2025 08:10:11 +0000</pubDate>
      <link>https://dev.to/kaweeshamr/object-oriented-concepts-the-foundation-of-modern-programming-part-01-6ip</link>
      <guid>https://dev.to/kaweeshamr/object-oriented-concepts-the-foundation-of-modern-programming-part-01-6ip</guid>
      <description>&lt;h2&gt;
  
  
  What Came Before OOP
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Procedural Programming (1950s-70s)
&lt;/h3&gt;

&lt;p&gt;Procedural programming was a paradigm that followed a step-by-step approach to solving problems by breaking them into smaller tasks or procedures. &lt;/p&gt;

&lt;p&gt;In its early stages, &lt;strong&gt;C&lt;/strong&gt; was a widely used procedural programming language. Here’s an example of a simple procedural program in C:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;calculateTotal&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;price1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;price2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;price1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;price2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Total: %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;calculateTotal&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While procedural programming worked well for smaller, straightforward programs, it started to face challenges as software became more complex. This complexity brought several issues:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Code Duplication&lt;/strong&gt;: The same logic was often repeated in different parts of the program.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Maintenance Problems&lt;/strong&gt;: A single change required modifications in multiple places, increasing the risk of errors.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scalability Issues&lt;/strong&gt;: Procedural programming struggled to adapt to the growing demands of larger, more complex software.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Poor Representation of Real-World Scenarios&lt;/strong&gt;: Data and operations were kept separate, making it difficult to model real-world entities like a "Bank Account" or "Car."&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  The Shift to Object-Oriented Programming (OOP)
&lt;/h2&gt;

&lt;p&gt;As software complexity grew, procedural programming began to show its limitations. This led to the evolution of a new paradigm—&lt;strong&gt;Object-Oriented Programming (OOP)&lt;/strong&gt;. OOP revolutionized the way we design and build software by modeling real-world entities and combining data and behavior into a single unit called an &lt;strong&gt;object&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key Concepts of OOP
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1. &lt;strong&gt;Encapsulation&lt;/strong&gt; 🛡
&lt;/h4&gt;

&lt;p&gt;Encapsulation binds data (attributes) and behaviors (functions) that operate on the data into a single entity, typically a &lt;strong&gt;class&lt;/strong&gt;. This means we can group related data and methods together into one unit and control how the data is accessed and modified. This promotes &lt;strong&gt;data security&lt;/strong&gt; and &lt;strong&gt;modularity&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq2cj2mc9w69vd2jneih0.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq2cj2mc9w69vd2jneih0.jpg" alt="Encapsulation" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Encapsulation is implemented with the support of access specifiers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Private variables&lt;/strong&gt;: These protect sensitive data by restricting outside access to variables.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Public methods&lt;/strong&gt;: These provide functionality to modify or access the private data from the outside.&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Why Encapsulation is Important:
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Data Protection&lt;/strong&gt;: By using private access specifiers, some attributes (e.g., &lt;code&gt;accBalance&lt;/code&gt;) are restricted from direct modification.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Modularity&lt;/strong&gt;: Changes made internally won't affect external code that interacts with the object.&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Example:
&lt;/h5&gt;

&lt;p&gt;A great example of encapsulation is a &lt;strong&gt;TV remote&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The remote has several buttons that perform actions. Each button is coded to execute a related function when pressed, but you cannot change the underlying code that defines what each button does. The remote hides the implementation details, providing you with public methods (the buttons) while keeping the internal functions (private attributes) hidden.&lt;/p&gt;




&lt;h4&gt;
  
  
  2. &lt;strong&gt;Abstraction&lt;/strong&gt; 🔍
&lt;/h4&gt;

&lt;p&gt;As the complexity of a system grows, &lt;strong&gt;abstraction&lt;/strong&gt; plays a vital role in managing that complexity. Abstraction is the concept that hides the complex details and only exposes the essential features.&lt;/p&gt;

&lt;p&gt;The goal of abstraction is to simplify the interface and reduce the complexity for the user or objects interacting with it.&lt;/p&gt;

&lt;h5&gt;
  
  
  Example:
&lt;/h5&gt;

&lt;p&gt;When you drive a &lt;strong&gt;car&lt;/strong&gt;, you use the &lt;strong&gt;steering wheel&lt;/strong&gt;, &lt;strong&gt;accelerator&lt;/strong&gt;, &lt;strong&gt;brakes&lt;/strong&gt;, and &lt;strong&gt;gear shifts&lt;/strong&gt;, which are essential for driving.&lt;/p&gt;

&lt;p&gt;But to drive, you don’t need to know how the gears are shifted, how the brake system works, or how the wheels rotate when you turn the steering wheel. These details are &lt;strong&gt;hidden&lt;/strong&gt; (abstracted) from you.&lt;/p&gt;

&lt;p&gt;You only interact with the simplified interface: the &lt;strong&gt;brake&lt;/strong&gt;, &lt;strong&gt;accelerator&lt;/strong&gt;, and &lt;strong&gt;steering wheel&lt;/strong&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  How to Use Abstraction in OOP
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Abstract Classes:
&lt;/h4&gt;

&lt;p&gt;Use abstract classes when you want to share common behavior but allow subclasses to provide specific implementations.&lt;br&gt;&lt;br&gt;
Example: A &lt;code&gt;Shape&lt;/code&gt; class might have a method &lt;code&gt;calculateArea()&lt;/code&gt;. You can abstract the calculation in the base class and allow subclasses like &lt;code&gt;Circle&lt;/code&gt;, &lt;code&gt;Rectangle&lt;/code&gt;, etc., to implement their specific area calculations.&lt;/p&gt;

&lt;h4&gt;
  
  
  Interfaces:
&lt;/h4&gt;

&lt;p&gt;Use interfaces when you want to ensure a class implements certain behaviors but don’t care about the implementation details.&lt;br&gt;&lt;br&gt;
Example: An &lt;code&gt;IPlayable&lt;/code&gt; interface that requires &lt;code&gt;play()&lt;/code&gt; and &lt;code&gt;pause()&lt;/code&gt; methods. Any class that implements this interface, like &lt;code&gt;AudioPlayer&lt;/code&gt; or &lt;code&gt;VideoPlayer&lt;/code&gt;, must provide implementations for &lt;code&gt;play()&lt;/code&gt; and &lt;code&gt;pause()&lt;/code&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Code Example
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Abstract class - Shape (provides a blueprint for all shapes)&lt;/span&gt;
&lt;span class="kd"&gt;abstract&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Shape&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Abstract method (does not have implementation)&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;abstract&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;calculateArea&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; 

    &lt;span class="c1"&gt;// Concrete method (implemented in the abstract class)&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;displayShape&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"This is a shape."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Concrete class - Circle (specific implementation of calculateArea)&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Circle&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Shape&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;radius&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Circle&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;radius&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;radius&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;radius&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;calculateArea&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;PI&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;radius&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;radius&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// Area of circle&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Concrete class - Rectangle (specific implementation of calculateArea)&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Rectangle&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Shape&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Rectangle&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;width&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;calculateArea&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// Area of rectangle&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Create objects of Circle and Rectangle&lt;/span&gt;
        &lt;span class="nc"&gt;Shape&lt;/span&gt; &lt;span class="n"&gt;myCircle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Circle&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;5.0&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// radius = 5&lt;/span&gt;
        &lt;span class="nc"&gt;Shape&lt;/span&gt; &lt;span class="n"&gt;myRectangle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Rectangle&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;4.0&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;6.0&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// length = 4, width = 6&lt;/span&gt;

        &lt;span class="c1"&gt;// Display the shape and calculate areas&lt;/span&gt;
        &lt;span class="n"&gt;myCircle&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;displayShape&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Circle Area: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;myCircle&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;calculateArea&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;

        &lt;span class="n"&gt;myRectangle&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;displayShape&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Rectangle Area: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;myRectangle&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;calculateArea&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  🌟 &lt;strong&gt;In Conclusion&lt;/strong&gt; 🌟
&lt;/h3&gt;

&lt;p&gt;As we've explored, the shift from procedural programming to Object-Oriented Programming (OOP) introduced us to powerful concepts such as &lt;strong&gt;Encapsulation&lt;/strong&gt; and &lt;strong&gt;Abstraction&lt;/strong&gt; that make software more manageable, scalable, and modular. 🌍💻&lt;/p&gt;

&lt;p&gt;By focusing on &lt;strong&gt;objects&lt;/strong&gt; that represent real-world entities, OOP allows developers to write cleaner and more maintainable code. 🎯&lt;/p&gt;

&lt;p&gt;Embrace OOP, and start designing software that mirrors the complexity of the real world in an elegant and efficient manner! ✨&lt;/p&gt;




</description>
      <category>oop</category>
      <category>java</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Getting Started with Hibernate: An Introduction to ORM and JPA Annotations</title>
      <dc:creator>Kaweesha Marasinghe</dc:creator>
      <pubDate>Sun, 12 Jan 2025 03:49:07 +0000</pubDate>
      <link>https://dev.to/kaweeshamr/getting-started-with-hibernate-an-introduction-to-orm-and-jpa-annotations-50l0</link>
      <guid>https://dev.to/kaweeshamr/getting-started-with-hibernate-an-introduction-to-orm-and-jpa-annotations-50l0</guid>
      <description>&lt;p&gt;In my previous blog post, we explored how databases work and delved into the differences between relational, object-oriented, and object-relational databases. If you haven’t read it yet, feel free to &lt;a href="https://dev.to/kaweeshamr/from-sql-to-object-oriented-databases-navigating-the-evolution-of-database-models-m80"&gt;check it out here&lt;/a&gt;.  &lt;/p&gt;

&lt;p&gt;Building on that foundation, this blog will dive into &lt;strong&gt;Object-Relational Mapping (ORM)&lt;/strong&gt;—a concept that bridges the gap between object-oriented programming and relational databases. Specifically, we’ll focus on how ORM frameworks like Hibernate simplify the process of mapping Java objects to database tables. This not only makes database operations more intuitive but also helps developers create and manage database structures seamlessly from their Java code. 🚀  &lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;What is ORM?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Object-Relational Mapping (ORM)&lt;/strong&gt; is a technique used to map Java objects to database tables, making it easier to handle databases without writing raw SQL queries.  &lt;/p&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;

&lt;p&gt;Without ORM, performing CRUD (Create, Read, Update, Delete) operations requires writing raw SQL queries like &lt;code&gt;SELECT&lt;/code&gt;, &lt;code&gt;INSERT&lt;/code&gt;, &lt;code&gt;UPDATE&lt;/code&gt;, and &lt;code&gt;DELETE&lt;/code&gt; to interact with the database.  &lt;/p&gt;

&lt;p&gt;With ORM, you can retrieve data and map it to Java objects effortlessly, eliminating the need for extensive SQL code.  &lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Hibernate: The Go-To ORM Tool&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Hibernate&lt;/strong&gt; is an implementation of JPA (Java Persistence API). JPA is a standard specification for managing relational data, while Hibernate adheres to this specification and performs ORM operations.  &lt;/p&gt;

&lt;p&gt;Hibernate simplifies database interaction by allowing developers to write &lt;strong&gt;database-independent code&lt;/strong&gt;. This means you can switch your data source with minimal changes to your application code.  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnx4le4zooz9e4m63s7lj.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnx4le4zooz9e4m63s7lj.jpg" alt="Image description" width="800" height="289"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Why Use Hibernate?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;With Hibernate, developers can:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Avoid writing raw SQL queries.  &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Work with Java objects instead of relational data structures.  &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Easily switch between different databases with minimal code adjustments.  &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;JPA Annotations in Hibernate&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;To handle ORM operations, Hibernate relies on &lt;strong&gt;JPA annotations&lt;/strong&gt;. Below, we’ll cover 8 of the most commonly used JPA annotations that make database operations seamless.  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsyo452x0a88l6a8r0r2q.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsyo452x0a88l6a8r0r2q.jpg" alt="Image description" width="776" height="536"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;1. &lt;code&gt;@Entity&lt;/code&gt;&lt;/strong&gt; 🏛️
&lt;/h3&gt;

&lt;p&gt;This annotation marks a Java class as a JPA entity, which maps to a table in the database. For example, if a class called &lt;code&gt;User&lt;/code&gt; is annotated with &lt;strong&gt;@Entity&lt;/strong&gt;, Hibernate will treat it as a table in the database, with its attributes mapped to table columns.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Entity&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Id&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Long&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;User&lt;/code&gt; class will be mapped to a table named &lt;code&gt;User&lt;/code&gt; in the database.
&lt;/li&gt;
&lt;li&gt;The class attributes (&lt;code&gt;id&lt;/code&gt;, &lt;code&gt;name&lt;/code&gt;, &lt;code&gt;email&lt;/code&gt;) will become columns in the table.
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;2. &lt;code&gt;@Table&lt;/code&gt;&lt;/strong&gt; 🗄️
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;@Table&lt;/strong&gt; annotation allows customization of the table name and other table-specific attributes.  &lt;/p&gt;

&lt;p&gt;By default, when a class is annotated with &lt;strong&gt;@Entity&lt;/strong&gt;, the database table will have the same name as the entity class. However, if you want a different name, you can customize it using &lt;strong&gt;@Table&lt;/strong&gt;.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Entity&lt;/span&gt;
&lt;span class="nd"&gt;@Table&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"users"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;schema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"public"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Id&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Long&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;User&lt;/code&gt; class is mapped to a table named &lt;code&gt;users&lt;/code&gt; in the &lt;code&gt;public&lt;/code&gt; schema.
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;3. &lt;code&gt;@Id&lt;/code&gt;&lt;/strong&gt; 🆔
&lt;/h3&gt;

&lt;p&gt;Every entity class must have a primary key to uniquely identify records in the corresponding table. The &lt;strong&gt;&lt;a class="mentioned-user" href="https://dev.to/id"&gt;@id&lt;/a&gt;&lt;/strong&gt; annotation is used to specify which attribute serves as the primary key.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Entity&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Id&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Long&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;id&lt;/code&gt; field is now the primary key of the &lt;code&gt;User&lt;/code&gt; entity and corresponds to the primary key column in the database table.
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;4. &lt;code&gt;@GeneratedValue&lt;/code&gt;&lt;/strong&gt; 🔢
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;@GeneratedValue&lt;/strong&gt; annotation specifies how the primary key value is generated. Hibernate supports four key strategies:  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;IDENTITY&lt;/strong&gt;: The primary key is auto-incremented, similar to SQL database behavior.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SEQUENCE&lt;/strong&gt;: Uses a database sequence to generate unique values.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;TABLE&lt;/strong&gt;: Uses a separate database table to generate primary key values. This method is slower due to additional database reads.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;AUTO&lt;/strong&gt;: JPA automatically selects the generation strategy based on the database dialect.
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Id&lt;/span&gt;
&lt;span class="nd"&gt;@GeneratedValue&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strategy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;GenerationType&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;IDENTITY&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Long&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;In this example, the &lt;code&gt;id&lt;/code&gt; value will be auto-incremented by the database.
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;5.&lt;code&gt;@Column&lt;/code&gt;&lt;/strong&gt; 📋
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;&lt;a class="mentioned-user" href="https://dev.to/column"&gt;@column&lt;/a&gt;&lt;/strong&gt; annotation is used to define column-specific constraints and customize column names in the database.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Column&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"full_name"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nullable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;unique&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Maps the &lt;code&gt;name&lt;/code&gt; field to a column named &lt;code&gt;full_name&lt;/code&gt;.
&lt;/li&gt;
&lt;li&gt;The column cannot have &lt;code&gt;null&lt;/code&gt; values (&lt;code&gt;nullable = false&lt;/code&gt;) and must contain unique entries (&lt;code&gt;unique = true&lt;/code&gt;).
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;6. &lt;code&gt;@OneToMany and @ManyToOne&lt;/code&gt;&lt;/strong&gt; 🔗
&lt;/h3&gt;

&lt;p&gt;These annotations define relationships between entities:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;@OneToMany&lt;/strong&gt;: A parent entity is related to multiple child entities.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;@ManyToOne&lt;/strong&gt;: A child entity is related to a single parent entity.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example: Parent-Child Relationship&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Let’s say a &lt;code&gt;User&lt;/code&gt; can have multiple &lt;code&gt;Order&lt;/code&gt; records.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Entity&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Id&lt;/span&gt;
    &lt;span class="nd"&gt;@GeneratedValue&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strategy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;GenerationType&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;IDENTITY&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Long&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@OneToMany&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mappedBy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"user"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cascade&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;CascadeType&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ALL&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Entity&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Order&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Id&lt;/span&gt;
    &lt;span class="nd"&gt;@GeneratedValue&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strategy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;GenerationType&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;IDENTITY&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Long&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@ManyToOne&lt;/span&gt;
    &lt;span class="nd"&gt;@JoinColumn&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"user_id"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Foreign key in the "orders" table&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;@OneToMany&lt;/strong&gt; in &lt;code&gt;User&lt;/code&gt; indicates a list of &lt;code&gt;Order&lt;/code&gt; entities.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;@ManyToOne&lt;/strong&gt; in &lt;code&gt;Order&lt;/code&gt; links each order to a single user.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;@JoinColumn&lt;/strong&gt; defines the foreign key column (&lt;code&gt;user_id&lt;/code&gt;) in the &lt;code&gt;orders&lt;/code&gt; table.
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Wrapping It All Up 🎁
&lt;/h2&gt;

&lt;p&gt;Hibernate and JPA annotations make database interactions seamless, bridging the gap between Java objects and relational databases. They eliminate the need to write raw SQL queries and ensure your application remains database-independent.  &lt;/p&gt;

&lt;p&gt;In the next blog post, we’ll explore advanced Hibernate topics like caching, transactions, and best practices for optimizing performance.  &lt;/p&gt;

&lt;p&gt;Stay tuned, and happy coding! ✨&lt;/p&gt;

</description>
    </item>
    <item>
      <title>From SQL to Object-Oriented Databases: Navigating the Evolution of Database Models</title>
      <dc:creator>Kaweesha Marasinghe</dc:creator>
      <pubDate>Thu, 09 Jan 2025 12:42:22 +0000</pubDate>
      <link>https://dev.to/kaweeshamr/from-sql-to-object-oriented-databases-navigating-the-evolution-of-database-models-m80</link>
      <guid>https://dev.to/kaweeshamr/from-sql-to-object-oriented-databases-navigating-the-evolution-of-database-models-m80</guid>
      <description>&lt;p&gt;When we talk about databases, SQL often comes to mind. Most people are familiar with SQL and relational databases, which use tables to organize and store data. But have you ever heard of Object-Oriented Databases? 🤔&lt;/p&gt;

&lt;p&gt;At first glance, the term may seem unfamiliar. We all know about &lt;em&gt;Object-Oriented Programming (OOP)&lt;/em&gt;, but a database based on OOP concepts? Sounds new, right? 🤷‍♂️&lt;/p&gt;

&lt;p&gt;Well, you're partially right! Object-oriented databases do indeed borrow principles from OOP, enhancing the capabilities of traditional databases and significantly boosting their performance. But how? Let’s dive in! 🚀&lt;/p&gt;

&lt;h2&gt;
  
  
  What are Relational Databases? 📊
&lt;/h2&gt;

&lt;p&gt;Relational databases store data in tables, similar to how you would organize information in an Excel spreadsheet. Each column holds a specific type of data (like numbers, text, or dates), and each row represents a unique record.&lt;/p&gt;

&lt;h3&gt;
  
  
  Limitations of Relational Databases ⚠️
&lt;/h3&gt;

&lt;p&gt;However, as Benjamin says in &lt;em&gt;"Who Am I" (2016)&lt;/em&gt;: "No system is perfect." Relational databases have their own set of issues:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Struggle with Multimedia Data:&lt;/strong&gt;&lt;br&gt;
Relational databases find it difficult to handle large, unstructured data like images and videos, often storing them as files and referencing them with keys, which can slow down performance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Set-Value Attributes:&lt;/strong&gt;&lt;br&gt;
When you need to store multiple values in one field (e.g., a list of skills for an employee), relational databases struggle. This requires creating extra tables or complex structures to manage such data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Hierarchical Data:&lt;/strong&gt;&lt;br&gt;
Data with parent-child relationships (like organizational charts or family trees) doesn't fit neatly into tables, making it hard to represent complex structures efficiently.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To tackle these limitations, object-oriented databases emerged as a new breed. 🧬&lt;/p&gt;

&lt;h2&gt;
  
  
  What are Object-Oriented Databases? 🛠️
&lt;/h2&gt;

&lt;p&gt;An &lt;strong&gt;Object-Oriented Database (OODB)&lt;/strong&gt; is a database system that stores data in the form of objects, similar to how objects are handled in &lt;em&gt;Object-Oriented Programming (OOP)&lt;/em&gt; languages like Java or C++. This makes OODBs ideal for applications that work with complex data and relationships.&lt;/p&gt;

&lt;p&gt;A Relational Database (RDBMS) stores data in tables with rows and columns, and managing complex or hierarchical relationships often requires creating multiple tables, foreign keys, and applying normalization. This can make the system more complex, especially when dealing with data that doesn’t naturally fit into tables, such as nested objects or relationships like many-to-many.&lt;/p&gt;

&lt;p&gt;For example, imagine you have a Student table with attributes like name, age, multiple phone numbers, and a list of certificates the student is enrolled in. In a relational database, you would need to create multiple columns for each attribute (including handling lists like phone numbers and certificates by normalizing the data into separate tables). You would also need to set up complex relationships to link these tables.&lt;/p&gt;

&lt;p&gt;However, with an Object-Oriented Database, you can store the entire Student object — including its name, age, phone numbers, and certificates — as a single object within one column. This simplifies the process significantly, as you no longer need to manage multiple tables and relationships for the data. Instead, the complex data is encapsulated into a single object that can be stored and retrieved easily. 🧩&lt;/p&gt;

&lt;p&gt;However, object-oriented databases were also replaced by another breed:&lt;/p&gt;

&lt;h2&gt;
  
  
  Object-Relational Databases ⚡
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Why Object-Relational Databases Replaced Object-Oriented Databases (OODB)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Limited Adoption of OOP:&lt;/strong&gt;&lt;br&gt;
OODB worked well with object-oriented languages but didn’t work well with traditional procedural languages.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;SQL Dependency:&lt;/strong&gt;&lt;br&gt;
Many organizations still rely on SQL because moving from SQL to OODBs was challenging.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Performance Issues:&lt;/strong&gt;&lt;br&gt;
While OODBs handled complex objects well, they sometimes struggled with high-performance needs in applications requiring simple, structured data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Integration Challenges:&lt;/strong&gt;&lt;br&gt;
Many organizations already had relational databases in place. Completely replacing these systems with OODBs was impractical and expensive.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These issues led to the emergence of a new database technology: Object-Relational Databases. 🎯&lt;/p&gt;

&lt;h2&gt;
  
  
  What are Object-Relational Databases? 🌀
&lt;/h2&gt;

&lt;p&gt;Object-relational databases were developed as a mixture of relational and object-oriented databases. They combined the best features of relational databases and OODBs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Features of Object-Relational Databases
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Maintain the Standard Relational Model:&lt;/strong&gt;&lt;br&gt;
They allow the creation of custom data types, nested data, and user-defined functions while still being compatible with SQL.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;SQL Compatibility:&lt;/strong&gt;&lt;br&gt;
This extension of SQL provides the ability to query both complex data as well as traditional data with a well-established standard.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Gradual Adoption:&lt;/strong&gt;&lt;br&gt;
Organizations could keep their relational databases while gradually introducing object-oriented features.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Examples of Database Models  📚
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;1. Relational Model (RDBMS)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Scenario&lt;/strong&gt;: Storing information about a Student and their Courses.  &lt;/p&gt;

&lt;h3&gt;
  
  
  Example: Relational Model
&lt;/h3&gt;

&lt;p&gt;Storing information about Students and Courses.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;&lt;code&gt;Students&lt;/code&gt; Table&lt;/strong&gt;
&lt;/h4&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;strong&gt;StudentID&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Name&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Age&lt;/strong&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Alice&lt;/td&gt;
&lt;td&gt;21&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Bob&lt;/td&gt;
&lt;td&gt;22&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;&lt;code&gt;Courses&lt;/code&gt; Table&lt;/strong&gt;
&lt;/h4&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;strong&gt;CourseID&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;CourseName&lt;/strong&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;101&lt;/td&gt;
&lt;td&gt;Mathematics&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;102&lt;/td&gt;
&lt;td&gt;Computer Science&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;&lt;code&gt;StudentCourses&lt;/code&gt; (Join Table)&lt;/strong&gt;
&lt;/h4&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;strong&gt;StudentID&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;CourseID&lt;/strong&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;101&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;102&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;101&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h4&gt;
  
  
  Characteristics:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Data is stored in flat, structured tables.
&lt;/li&gt;
&lt;li&gt;Relationships are managed using &lt;strong&gt;foreign keys&lt;/strong&gt; and joins.
&lt;/li&gt;
&lt;li&gt;Querying is done using &lt;strong&gt;SQL&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;2. Object-Oriented Model (OODB)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Scenario&lt;/strong&gt;: Representing a Student as an object with nested attributes.  &lt;/p&gt;

&lt;h4&gt;
  
  
  Object Representation:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"StudentID"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"Name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Alice"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"Age"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"Courses"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"CourseID"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;101&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"CourseName"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Mathematics"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"CourseID"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;102&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"CourseName"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Computer Science"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Characteristics:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Data is stored as &lt;strong&gt;objects&lt;/strong&gt;, similar to programming languages like Java or Python.
&lt;/li&gt;
&lt;li&gt;No need for separate tables or joins.
&lt;/li&gt;
&lt;li&gt;Methods (e.g., &lt;code&gt;getCourseList()&lt;/code&gt; or &lt;code&gt;calculateAge()&lt;/code&gt;) can be attached to objects.
&lt;/li&gt;
&lt;li&gt;Querying is done using &lt;strong&gt;object-oriented APIs&lt;/strong&gt; instead of SQL.
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;3. Object-Relational Model (ORD)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Scenario&lt;/strong&gt;: Combining relational structure with object-oriented features.  &lt;/p&gt;

&lt;h4&gt;
  
  
  PostgreSQL Example:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Define a custom type for Courses&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TYPE&lt;/span&gt; &lt;span class="n"&gt;Course&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;CourseID&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;CourseName&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Create a &lt;code&gt;Students&lt;/code&gt; table with a column for nested courses&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;Students&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;StudentID&lt;/span&gt; &lt;span class="nb"&gt;SERIAL&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Age&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Courses&lt;/span&gt; &lt;span class="n"&gt;Course&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Insert data&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;Students&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Courses&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s1"&gt;'Alice'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;ARRAY&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;ROW&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;101&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Mathematics'&lt;/span&gt;&lt;span class="p"&gt;)::&lt;/span&gt;&lt;span class="n"&gt;Course&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;ROW&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;102&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Computer Science'&lt;/span&gt;&lt;span class="p"&gt;)::&lt;/span&gt;&lt;span class="n"&gt;Course&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Query nested data&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Courses&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Students&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Alice'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Characteristics:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Combines relational structure with object-oriented features like &lt;strong&gt;nested types&lt;/strong&gt; and &lt;strong&gt;arrays&lt;/strong&gt;.
&lt;/li&gt;
&lt;li&gt;Uses extended SQL to handle complex data types.
&lt;/li&gt;
&lt;li&gt;Provides a middle ground between &lt;strong&gt;RDBMS&lt;/strong&gt; and &lt;strong&gt;OODB&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Summary Table
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;strong&gt;Model&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Structure&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Example Data&lt;/strong&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Relational Model&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Flat tables with relationships&lt;/td&gt;
&lt;td&gt;Students, Courses, Join tables&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Object-Oriented&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Hierarchical, nested objects&lt;/td&gt;
&lt;td&gt;JSON-like objects or OOP classes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Object-Relational&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Tables with nested data and objects&lt;/td&gt;
&lt;td&gt;Arrays, custom types in tables&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;p&gt;🎉 Hope this helps clarify the different database models! Let me know if you have questions! 😊&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Understanding the Spring Framework: A Developer’s Journey to Clean Code 🚀</title>
      <dc:creator>Kaweesha Marasinghe</dc:creator>
      <pubDate>Mon, 06 Jan 2025 12:22:32 +0000</pubDate>
      <link>https://dev.to/kaweeshamr/understanding-the-spring-framework-a-developers-journey-to-clean-code-39de</link>
      <guid>https://dev.to/kaweeshamr/understanding-the-spring-framework-a-developers-journey-to-clean-code-39de</guid>
      <description>&lt;h2&gt;
  
  
  Introduction: The Early Java Era
&lt;/h2&gt;

&lt;p&gt;In the early era of programming, writing Java code was a bit frustrating. Developers had to set up long configurations, deal with verbose XML files, and manually manage dependencies that were rapidly updated. This era made it harder for developers to thrive and write efficient code.&lt;/p&gt;

&lt;p&gt;In 2003, &lt;strong&gt;Spring Framework&lt;/strong&gt; revolutionized the Java ecosystem by introducing &lt;strong&gt;Dependency Injection (DI)&lt;/strong&gt; and &lt;strong&gt;Inversion of Control (IoC)&lt;/strong&gt;. These features significantly reduced developers' headaches by eliminating tightly coupled code and making it easier to write clean, modular code. 🙌&lt;/p&gt;




&lt;h2&gt;
  
  
  What is &lt;strong&gt;Inversion of Control (IoC)&lt;/strong&gt;? 🤔
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fasx4qycqtps16zpgr242.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fasx4qycqtps16zpgr242.png" alt="IoC Diagram" width="800" height="586"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IoC&lt;/strong&gt; is a design principle where the control of object creation and dependency management is transferred to a container (like the Spring IoC container). Instead of objects managing their own dependencies, the container does this work for you! 🎉&lt;/p&gt;

&lt;h3&gt;
  
  
  Normal (Without IoC) Behavior
&lt;/h3&gt;

&lt;p&gt;Without IoC, developers are responsible for creating and managing their object dependencies. &lt;/p&gt;

&lt;p&gt;For example, let’s consider a &lt;code&gt;Car&lt;/code&gt; class that depends on an &lt;code&gt;Engine&lt;/code&gt; class. In this scenario, the &lt;code&gt;Car&lt;/code&gt; class manages the &lt;code&gt;Engine&lt;/code&gt; object by itself:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Engine&lt;/span&gt; &lt;span class="n"&gt;engine&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Engine&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// Car directly creates an Engine&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;In this case:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;Car&lt;/code&gt; class &lt;strong&gt;directly creates&lt;/strong&gt; the &lt;code&gt;Engine&lt;/code&gt; object.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;Car&lt;/code&gt; class is responsible for both creating and managing the &lt;code&gt;Engine&lt;/code&gt; object, which creates a &lt;strong&gt;tight coupling&lt;/strong&gt; between the two.&lt;/li&gt;
&lt;li&gt;If we want to change the &lt;code&gt;Engine&lt;/code&gt; class (for example, to use a &lt;code&gt;HybridEngine&lt;/code&gt;), we’d need to modify the &lt;code&gt;Car&lt;/code&gt; class. This violates the &lt;strong&gt;Open/Closed Principle&lt;/strong&gt;: classes should be &lt;strong&gt;open for extension&lt;/strong&gt; but &lt;strong&gt;closed for modification&lt;/strong&gt;. 🚧&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;With IoC in Spring (Dependency Injection)&lt;/strong&gt; 🛠️
&lt;/h3&gt;

&lt;p&gt;In &lt;strong&gt;IoC&lt;/strong&gt; and &lt;strong&gt;Dependency Injection (DI)&lt;/strong&gt;, Spring takes care of creating and injecting dependencies into your classes.&lt;/p&gt;

&lt;p&gt;Simply put, the Spring IoC container creates the &lt;code&gt;Engine&lt;/code&gt; object and injects it into the &lt;code&gt;Car&lt;/code&gt; object when required. This way, you don’t have to manually manage object creation or dependencies. Spring handles it for you. 🧑‍💻&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Engine&lt;/span&gt; &lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// Engine is injected by Spring, not created by Car&lt;/span&gt;

    &lt;span class="c1"&gt;// Constructor-based injection&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Car&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Engine&lt;/span&gt; &lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;engine&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;strong&gt;How Does Spring Know Which Objects to Inject?&lt;/strong&gt; 🧐
&lt;/h3&gt;

&lt;p&gt;Spring uses &lt;strong&gt;Spring Beans&lt;/strong&gt; to manage the lifecycle and dependencies of objects. In Spring, a &lt;strong&gt;bean&lt;/strong&gt; is an object that the Spring IoC container manages. The container knows which beans to create and inject based on your configuration. 🌱&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Spring Beans and Bean Lifecycle&lt;/strong&gt; 🔄
&lt;/h3&gt;

&lt;p&gt;In Spring, a &lt;strong&gt;bean&lt;/strong&gt; is any object that is managed by the Spring IoC container. The container is responsible for:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Instantiation&lt;/strong&gt;: Creating the bean.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dependency Injection&lt;/strong&gt;: Injecting the required dependencies.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Initialization&lt;/strong&gt;: Calling any initialization methods (e.g., &lt;code&gt;@PostConstruct&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Destruction&lt;/strong&gt;: Calling cleanup methods (e.g., &lt;code&gt;@PreDestroy&lt;/code&gt;).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  How to Define a Spring Bean? 🏷️
&lt;/h3&gt;

&lt;p&gt;You can define beans using annotations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;@Component&lt;/code&gt;&lt;/strong&gt;: Marks a class as a Spring-managed bean. Spring automatically detects and instantiates this class during component scanning.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;@Autowired&lt;/code&gt;&lt;/strong&gt;: Tells Spring to inject the required dependency (either via constructor, field, or setter).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;@Configuration&lt;/code&gt;&lt;/strong&gt;: Used to define bean definitions using Java configuration (using &lt;code&gt;@Bean&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example with annotations:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Component&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Engine&lt;/span&gt; &lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@Autowired&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Car&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Engine&lt;/span&gt; &lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;engine&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;@Component&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Engine&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Engine is running..."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;strong&gt;Advantages of IoC (and DI)&lt;/strong&gt; 🏆
&lt;/h3&gt;

&lt;p&gt;By using IoC and DI, Spring offers several advantages:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Loose Coupling&lt;/strong&gt;: The &lt;code&gt;Car&lt;/code&gt; class is no longer tightly coupled with the &lt;code&gt;Engine&lt;/code&gt;. It only depends on the interface or type, not the specific implementation. This makes it easy to swap the &lt;code&gt;Engine&lt;/code&gt; type, such as using an &lt;code&gt;ElectricEngine&lt;/code&gt; instead of a &lt;code&gt;GasolineEngine&lt;/code&gt;. 🔄&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Easier Testing&lt;/strong&gt;: Since dependencies are injected, you can easily substitute mock objects or different implementations when writing unit tests for the &lt;code&gt;Car&lt;/code&gt; class. 🧪&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Flexibility&lt;/strong&gt;: You can change the dependencies externally (e.g., through Spring configuration) without modifying the core business logic of the classes. 🔧&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  Spring Configuration Methods
&lt;/h3&gt;

&lt;p&gt;There are three primary ways to configure Spring beans and the IoC container:&lt;/p&gt;

&lt;h4&gt;
  
  
  1. &lt;strong&gt;XML Configuration (Traditional)&lt;/strong&gt; 📜
&lt;/h4&gt;

&lt;p&gt;In the traditional approach, beans are defined in an XML configuration file. This method is becoming less common but is still used in legacy systems.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;beans&lt;/span&gt; &lt;span class="na"&gt;xmlns=&lt;/span&gt;&lt;span class="s"&gt;"http://www.springframework.org/schema/beans"&lt;/span&gt;
       &lt;span class="na"&gt;xmlns:xsi=&lt;/span&gt;&lt;span class="s"&gt;"http://www.w3.org/2001/XMLSchema-instance"&lt;/span&gt;
       &lt;span class="na"&gt;xsi:schemaLocation=&lt;/span&gt;&lt;span class="s"&gt;"http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;bean&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"car"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"com.example.Car"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;constructor-arg&lt;/span&gt; &lt;span class="na"&gt;ref=&lt;/span&gt;&lt;span class="s"&gt;"engine"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/bean&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;bean&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"engine"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"com.example.Engine"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/beans&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  2. &lt;strong&gt;Annotation-based Configuration (Modern)&lt;/strong&gt; 🏷️
&lt;/h4&gt;

&lt;p&gt;The modern approach uses annotations like &lt;code&gt;@Component&lt;/code&gt;, &lt;code&gt;@Autowired&lt;/code&gt;, and &lt;code&gt;@Configuration&lt;/code&gt; to define and inject beans.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Component&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Engine&lt;/span&gt; &lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@Autowired&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Car&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Engine&lt;/span&gt; &lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;engine&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To enable annotation-based configuration, use &lt;code&gt;@ComponentScan&lt;/code&gt; in a configuration class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Configuration&lt;/span&gt;
&lt;span class="nd"&gt;@ComponentScan&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;basePackages&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"com.example"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AppConfig&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Spring scans the specified package for @Component annotated classes&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  3. &lt;strong&gt;Java-based Configuration (Best for Type Safety)&lt;/strong&gt; 🖥️
&lt;/h4&gt;

&lt;p&gt;Java-based configuration uses &lt;code&gt;@Configuration&lt;/code&gt; and &lt;code&gt;@Bean&lt;/code&gt; annotations, which is more type-safe and easier to maintain.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Configuration&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AppConfig&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@Bean&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt; &lt;span class="nf"&gt;car&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Car&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Bean&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Engine&lt;/span&gt; &lt;span class="nf"&gt;engine&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Engine&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt; 🏁
&lt;/h3&gt;

&lt;p&gt;In this blog, we’ve explored:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;IoC (Inversion of Control)&lt;/strong&gt;: The core design principle where Spring takes control of object creation and dependency management.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DI (Dependency Injection)&lt;/strong&gt;: A technique that helps Spring inject dependencies into your beans, allowing for clean and flexible code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Spring Beans&lt;/strong&gt;: Objects that are managed by Spring, and how their lifecycle is handled by the IoC container.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Spring Configuration&lt;/strong&gt;: Different ways to configure Spring beans, from XML to annotations to Java-based configurations.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Spring's powerful IoC and DI capabilities help you write cleaner, more modular, and easily testable code. 🌟&lt;/p&gt;




</description>
      <category>webdev</category>
      <category>java</category>
      <category>spring</category>
    </item>
    <item>
      <title>Building Secure Authentication Microservices with Spring Boot: Part 1 - Getting Started</title>
      <dc:creator>Kaweesha Marasinghe</dc:creator>
      <pubDate>Tue, 24 Dec 2024 03:38:53 +0000</pubDate>
      <link>https://dev.to/kaweeshamr/building-secure-authentication-microservices-with-spring-boot-part-1-getting-started-37n6</link>
      <guid>https://dev.to/kaweeshamr/building-secure-authentication-microservices-with-spring-boot-part-1-getting-started-37n6</guid>
      <description>&lt;h2&gt;
  
  
  🌟 &lt;strong&gt;In the World of Programming: Spring Boot &amp;amp; Authentication&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;When you hear the word &lt;strong&gt;&lt;em&gt;Spring Boot&lt;/em&gt;&lt;/strong&gt;, what comes to mind? 💭 Chances are, it’s &lt;em&gt;authentication&lt;/em&gt;. But why? 🤔 What makes Spring Boot so closely tied to authentication? Is there some secret connection? 🕵️‍♂️ Let’s uncover the truth!  &lt;/p&gt;




&lt;h2&gt;
  
  
  🌱 &lt;strong&gt;What is Spring Boot?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Spring Boot is a &lt;strong&gt;Java framework&lt;/strong&gt; built on top of the &lt;strong&gt;Spring Framework&lt;/strong&gt;, designed to:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Help developers quickly build &lt;strong&gt;production-ready&lt;/strong&gt; 🚀, standalone &lt;strong&gt;Java applications&lt;/strong&gt;.
&lt;/li&gt;
&lt;li&gt;It’s a go-to framework for &lt;strong&gt;enterprise-level applications&lt;/strong&gt; 🏢.
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  ✨ &lt;strong&gt;Why is Spring Boot So Popular?&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Quick Setup&lt;/em&gt;&lt;/strong&gt; ⚡  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Eliminates complex configurations by using &lt;strong&gt;built-in templates&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Opinionated Defaults&lt;/em&gt;&lt;/strong&gt; 🎯  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Provides pre-configured settings for common setups.
&lt;/li&gt;
&lt;li&gt;You can start quickly but still &lt;strong&gt;customize and enhance&lt;/strong&gt; as needed.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Embedded Servers&lt;/em&gt;&lt;/strong&gt; 🔧  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No need for external servers like Tomcat!
&lt;/li&gt;
&lt;li&gt;You can &lt;strong&gt;run your application directly&lt;/strong&gt; without extra setup.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Microservices Support&lt;/em&gt;&lt;/strong&gt; 🛠️  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Perfect for creating &lt;strong&gt;small, scalable, and independent services&lt;/strong&gt;.
&lt;/li&gt;
&lt;li&gt;Each microservice can be deployed and scaled separately.
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  🛡️ &lt;strong&gt;Spring Boot’s Authentication Powers&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;So, this is &lt;strong&gt;Spring Boot&lt;/strong&gt;. But where do all these &lt;strong&gt;authentication superpowers&lt;/strong&gt; come from? 🤔  &lt;/p&gt;

&lt;p&gt;That’s where &lt;strong&gt;&lt;em&gt;Spring Security&lt;/em&gt;&lt;/strong&gt; comes into play! 💥&lt;br&gt;&lt;br&gt;
With this &lt;strong&gt;Infinity Stone 💎&lt;/strong&gt;, even your simplest application gets the power to ensure that only authorized people make it through the door! 🚪👊  &lt;/p&gt;


&lt;h2&gt;
  
  
  &lt;strong&gt;Spring Security&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;(Or, may I ask, what does this Infinity Stone do?)  &lt;/p&gt;

&lt;p&gt;Think of &lt;strong&gt;Spring Security&lt;/strong&gt; as the ultimate &lt;strong&gt;sidekick&lt;/strong&gt; to your Spring Boot app. 🦸‍♂️  &lt;/p&gt;

&lt;p&gt;It gives your application the power to:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Protect&lt;/strong&gt; against unauthorized access. 🚫
&lt;/li&gt;
&lt;li&gt;Shield your app from &lt;strong&gt;malicious attacks&lt;/strong&gt; 🔒 like CSRF, XSS, etc.
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  🛡️ &lt;strong&gt;Features of Spring Security&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Authentication&lt;/em&gt;&lt;/strong&gt; 🔑  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Verifies the user’s identity.
&lt;/li&gt;
&lt;li&gt;Checks if the username/password or token (like JWT) is valid.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Authorization&lt;/em&gt;&lt;/strong&gt; 🛂  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Determines what actions or resources a user is allowed to access.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Protection Against Common Attacks&lt;/em&gt;&lt;/strong&gt; 🛡️  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Mitigates threats like &lt;strong&gt;Cross-Site Request Forgery (CSRF)&lt;/strong&gt; and &lt;strong&gt;Cross-Site Scripting (XSS)&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;


&lt;h2&gt;
  
  
  🌐 &lt;strong&gt;Why Spring Boot for Authentication?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Sure, you can use other languages like &lt;strong&gt;Node.js&lt;/strong&gt; or &lt;strong&gt;Go&lt;/strong&gt; for authentication. 🐹 🚀  &lt;/p&gt;

&lt;p&gt;But &lt;strong&gt;Spring Boot&lt;/strong&gt; stands out because:  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Integration with Spring Ecosystem&lt;/em&gt;&lt;/strong&gt;:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Out-of-the-box support for &lt;strong&gt;OAuth2&lt;/strong&gt;, &lt;strong&gt;JWT&lt;/strong&gt;, and other modern protocols.
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Enterprise-grade Security&lt;/em&gt;&lt;/strong&gt;:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ready-made integrations with &lt;strong&gt;LDAP&lt;/strong&gt;, &lt;strong&gt;SSO&lt;/strong&gt;, and &lt;strong&gt;Active Directory&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Rich Ecosystem&lt;/em&gt;&lt;/strong&gt;:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Vast documentation 📚 and an active community.
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Microservices-ready&lt;/em&gt;&lt;/strong&gt;:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ideal for secure, stateless &lt;strong&gt;microservices architectures&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;


&lt;h2&gt;
  
  
  &lt;strong&gt;Every Superhero Needs a Sidekick&lt;/strong&gt; 🦸‍♂️🛡️
&lt;/h2&gt;

&lt;p&gt;In the world of authentication, &lt;strong&gt;JWT (JSON Web Token)&lt;/strong&gt; is the &lt;strong&gt;sidekick&lt;/strong&gt; that never misses its mark. 🎯  &lt;/p&gt;


&lt;h2&gt;
  
  
  📝 &lt;strong&gt;What is JWT?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;JWT is a &lt;strong&gt;compact, URL-safe token&lt;/strong&gt; used to:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Authenticate&lt;/strong&gt; users. 🔑
&lt;/li&gt;
&lt;li&gt;Authorize their actions in web applications. 🌐
&lt;/li&gt;
&lt;/ul&gt;


&lt;h3&gt;
  
  
  🧩 &lt;strong&gt;Key Features of JWT&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Compact&lt;/em&gt;&lt;/strong&gt; 📦  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Small in size, making it efficient for web transmission.
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Self-Contained&lt;/em&gt;&lt;/strong&gt; 📜  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;All necessary user/session information is inside the token.
&lt;/li&gt;
&lt;li&gt;No need for server-side sessions!
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Secure&lt;/em&gt;&lt;/strong&gt; 🔒  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Digitally signed to ensure integrity and authenticity.
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;


&lt;h3&gt;
  
  
  ⚙️ &lt;strong&gt;Structure of a JWT&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;A JWT consists of three parts, separated by dots (&lt;code&gt;.&lt;/code&gt;):  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Header&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Metadata like token type and signing algorithm.
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
     &lt;/span&gt;&lt;span class="nl"&gt;"alg"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"HS256"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
     &lt;/span&gt;&lt;span class="nl"&gt;"typ"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"JWT"&lt;/span&gt;&lt;span class="w"&gt;
   &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Payload&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Contains user data or claims.
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
     &lt;/span&gt;&lt;span class="nl"&gt;"sub"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1234567890"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
     &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"John Doe"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
     &lt;/span&gt;&lt;span class="nl"&gt;"admin"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
   &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Signature&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Ensures the token hasn’t been tampered with.
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   HMACSHA256(
     base64UrlEncode(header) + "." + base64UrlEncode(payload), 
     secret
   )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  🛠️ &lt;strong&gt;How JWT Works&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;User Logs In&lt;/strong&gt; 🔐  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Provides credentials (e.g., username/password).
&lt;/li&gt;
&lt;li&gt;Server generates a JWT and sends it to the client.
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Client Stores JWT&lt;/strong&gt; 💾  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Stored in &lt;code&gt;localStorage&lt;/code&gt; or &lt;code&gt;cookies&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Client Sends JWT with Requests&lt;/strong&gt; 📩  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Token is sent in the &lt;code&gt;Authorization&lt;/code&gt; header:
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; Authorization: Bearer &amp;lt;JWT&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Server Verifies JWT&lt;/strong&gt; ✅  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Checks the token’s validity and processes the request.
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  🤔 &lt;strong&gt;Why Use JWT?&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Stateless&lt;/strong&gt;: No server-side sessions required.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scalable&lt;/strong&gt;: Perfect for distributed systems.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cross-Domain&lt;/strong&gt;: Great for APIs.
&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;What’s Next?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;So, this was the &lt;strong&gt;basic breakdown&lt;/strong&gt; of the key players in an authentication microservice:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Spring Boot&lt;/strong&gt; 🌱
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Spring Security&lt;/strong&gt; 🛡️
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;JWT&lt;/strong&gt; 🎯
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the next blog, we’ll &lt;strong&gt;start coding&lt;/strong&gt; from scratch to build a robust authentication microservice using these powerful tools. 🚀  &lt;/p&gt;

&lt;p&gt;Let’s get coding! 💻✨  &lt;/p&gt;




</description>
      <category>beginners</category>
      <category>tutorial</category>
      <category>api</category>
      <category>java</category>
    </item>
  </channel>
</rss>
