notes
  • computer-networking
    • extend-wifi-with-router
    • how-the-internet-works
    • idk
    • networking-devices
    • osi-model
    • tcp-ip
    • Types of VPN
  • databases
    • Foreign Keys
    • Redis
    • simple-queries
  • devops
    • ansible
    • Manual deployment
    • docker
    • Workflow file
    • nginx
    • promethues-grafana
    • terraform
  • hardware
    • Power
  • home-server
    • proxmox-basics
    • proxmox-setup
    • storage
  • languages-frameworks
    • programming-paradigms
    • programming-languages
      • Regex Notes
      • c
        • basics
        • pointers-memory
      • cpp
        • basics
        • running-cpp
      • php
        • basics
        • choizez
        • frameworks
          • laravel
      • python
        • venv
        • concepts
          • Using lambda
        • frameworks
          • django
            • django
            • start
      • java
        • advanced
          • functional-programming
          • reactive-programming
        • concepts
          • how-java-works
          • serialization
          • sockets
          • threads
        • extra
          • collection-framework
          • generics-and-wildcards
          • Regular Expressions (Regex)
          • streams
        • frameworks
          • orm
        • fundamentals
          • OOP
          • conditionals
          • data-structures
          • data-types
          • exceptions
          • files
          • Functions (aka method)
          • Loops
          • packages
          • type-casting
      • javascript
        • frameworks
          • morgan
          • Using Sequelize with PostgreSQL in JavaScript
  • operating-system
    • basics
    • linux-directories
    • Basic Unix Terminal Commands
  • others
    • dark-web
    • piracy
  • system-design
    • system-design
  • web-dev
    • full-stack
  • books
    • sicp
      • Recursion thought process
      • 1
        • 1.1
        • 1.2
        • 1.3
      • 2
        • 2.1
  • certifications
    • aws-certified-cloud-practitioner
      • core-services
      • other-services
    • comptia-a+
      • Cloud
      • hardware
      • Important terms
      • Important terms
      • Troubleshooting
  • cloud
    • aws
      • aws-cli
      • aws-ec2-deployment
  • dsa
    • algorithms
      • back-tracking
      • bfs
      • Binary Search
      • bit-manipulation
      • Bubble sort
      • bucket-sort
      • counting-sort
      • dfs
      • Divide & Conquer
      • djikstras-algorithm
      • dynamic-programming
      • external-sorting
      • greedy-algorithm
      • Heap sort
      • Insertion sort
      • kadanes-algorithm
      • Merge sort
      • Permutation
      • quick-sort
      • Radix Sort
      • recurrence-relation
      • recursion
      • Selection sort
      • sliding-window
      • subset
      • time-space-complexity
      • topological-sort
      • tree-traversals
      • Two Pointers Technique
    • data-structures
      • data-structures
  • security
    • authentication
      • What is JWT (JSON Web Token)?
    • software-architecture-design
      • design-patterns
Powered by GitBook
On this page
  • What is Object-Relational Mapping (ORM)?
  • What is Java Persistence API (JPA)?
  • What is Hibernate?
  • What is Spring Data JPA?
  • Using Spring Data JPA in Spring Boot
  1. languages-frameworks
  2. programming-languages
  3. java
  4. frameworks

orm

PreviousframeworksNextfundamentals

Last updated 1 month ago

What is Object-Relational Mapping (ORM)?

Basically...

  • Save to database : Object -> ORM -> Database

  • Extract from database : Database -> ORM -> Object

What is Java Persistence API (JPA)?

JPA is a Java specification for managing relational data in an object-oriented way, enabling the mapping of Java objects to database tables and simplifying database interactions.

It is not an implementation, but a specification that various ORM frameworks, like Hibernate, EclipseLink, and Apache OpenJPA, implement.

What is Hibernate?

Hibernate is an ORM framework that facilitates the mapping of Java objects to database tables and simplifies database interactions in Java applications.

It is an implementation framework that follows the JPA specification ( one of the JPA providers )

What is Spring Data JPA?

Spring Data JPA is a framework that builds on top of JPA (including Hibernate as a potential JPA provider) to make it even easier to work with databases in Spring applications (extra layer of abstraction)

It is not an implementation or JPA provider, it's just an abstraction used to significantly reduce the amount of boilerplate code required to implement data access layers for various persistence stores.

Using Spring Data JPA in Spring Boot

File Structure for Spring Boot application:

  • StudentController: Concrete class that manages endpoints for frontend / REST API calls

    @RestController
    @RequestMapping("/students")
    public class StudentController {
        @Autowired
        private StudentService studentService;
    
        @GetMapping
        public List<Student> findAllStudent() {
            return studentService.findAllStudent();
        }
    
        // etc...
        // Ommited other methods for brevity
    
    }
  • StudentRepository: Fulfils the role of repository (other classes can use it to manipulate database)

      // DAO (Data access object) layer
    
      // @Repository tells Spring that this class is a repository
      // JpaRepository contains API for basic CRUD operations and more..
      @Repository
      public interface StudentRepository extends JpaRepository<Student,Long> {
    
      }
    
  • Student: POJO Student entity that is used to map to database

    // @Entity annotation defines that a class can be mapped to a table
    @Entity
    public class Student {
    
        public Student(String firstName, String lastName, String email) {
            this.firstName = firstName;
            this.lastName = lastName;
            this.email = email;
        }
    
        // @ID annotation specifies the primary key of the entity.
        // @GeneratedValue annotation specifies the primary key generation strategy to use
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private int id;
    
        @Column(name = "first_name") // @Column annotation specifies which column in database
        private String firstName;
    
        @Column(name = "last_name")
        private String lastName;
    
        private String email;
    
        // Getters and setters ommited for brevity
    }
  • StudentService: Interface for StudentServiceImpl

  • StudentServiceImpl: Service class defines methods that interact with the repository and are invoked by the controller.

    @Service
    public class StudentServiceImpl implements StudentService{
    
        @Autowired
        StudentRepository studentRepository;
        @Override
        public List<Student> findAllStudent() {
            return studentRepository.findAll();
        }
    
        // etc...
        // Ommited other methods for brevity
    
    }

As you can see we can easily use Spring Data JPA in Spring / Spring Boot applications by just :

  • creating a entity POJO class

  • a repository interface class which extends the JpaRepository interface which enables the repository to use its database CRUD methods.

Alt text
Alt text
Alt text
Alt text