VOOZH about

URL: https://www.geeksforgeeks.org/advance-java/rich-domain-model-with-hibernate/

⇱ Rich Domain Model with Hibernate - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Rich Domain Model with Hibernate

Last Updated : 23 Jul, 2025

Rich Domain Model in the context of software development and Hibernate is the model where the business logic is encapsulated directly within the domain objects, rather than being separated into the services or other layers. It can allow the domain model to enforce the business rules and ensure data consistency. This article will guide you through setting up the simple example project using Hibernate with Rich Domain Model.

In traditional applications, business logic can be spread across multiple layers, leading to a lack of cohesion and harder maintenance. The Rich Domain Model centralizes this logic within the domain entities. Making the code more intuitive and aligned with real-world business operations. This encapsulation aids in maintaining the business rule consistency and logic encapsulation.

For example, consider the simple banking application where the Account entities can handle operations like deposits and withdrawals internally rather than relying on external services to enforce the rules of the application.

Prerequisites:

  • Basic knowledge of the Java and Spring Boot.
  • Maven tool for dependency management.
  • The Integrated Development Environment like IntelliJ IDEA.

Implementation of Rich Domain Model with Hibernate

Step 1: Create Spring Boot Project

Create a new Spring project using Spring Initializr and add the below dependencies into the project.

Dependencies:

  • Spring Web
  • Spring Data JPA
  • MySQL Driver
  • Lombok
  • Spring DevTools

After creating the project, then the file structure will look like the below image.

👁 Project Folder Structure


Step 2: Application Properties

Open the application.properties file and add the configuration of the MySQL database and Hibernate.

spring.application.name=rich-domain-model
# MySQL Configuration
spring.datasource.url=jdbc:mysql://localhost:3306/example
spring.datasource.username=root
spring.datasource.password=

# JPA/Hibernate properties
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.show-sql=true


Step 3: Create the Account Entity

We will create the Account class with the methods to handle the deposits and withdrawals of the application.

Go to src > main > java > org.example.richdomainmodel > Account and put the below code.


Step 4: Create the AccountRepository

Go to src > main > java > org.example.richdomainmodel > AccountRepository and put the below code.


Step 5: Create AccountController Class

Now, we will create the Simple RESTful endpoints to handle the HTTP requests of the application.

Go to src > main > java > org.example.richdomainmodel > AccountController and put the below code.


Step 6: Main Class

No changes are required in the main class.


pom.xml:


Step 7: Run the application

Now, run the application and it will start at port 8080.

👁 Application Running


Step 8: Testing the Endpoints

1. Create Account Endpoint

POST http://localhost:8080/accounts

Output:

👁 Account Endpoint


2. Retrieve Account balance through Id

GET http://localhost:8080/accounts/102

Output:

👁 Retrieve Account balance through Id


4. Deposit Endpoint

POST http://localhost:8080/accounts/102/deposit?amount=100

Output:

👁 Deposit Endpoint


5. Withdraw Endpoint

POST http://localhost:8080/accounts/102/withdraw?amount=100

Output:

👁 Withdraw Endpoint

This example demonstrates the embedded critical business logic within the domain model itself and adhering to the principles of the Rich Domain Model with Hibernate in the Spring Boot application.

Comment

Explore