본문 바로가기
개발 (ENG)

Spring Boot(Kotlin) — ep.11 Structuring JPA in a Spring Boot Multi-Module Architecture

by 새싹 아빠 2025. 11. 27.

In this article, we will organize how JPA should be placed within a Spring Boot multi-module project
and summarize the key concepts you need to understand during the initial setup.
Since the goal of this series is to build a “basic server setup template,”
we will NOT dive deeply into JPA itself or include domain code inside the shared template project.

 

Instead, we will explain why JPA is widely used in real-world development, how Entity / Repository / Service / Controller should be organized in a multi-module architecture,
and present only the minimal structure required for initial setup.

📚 Spring Boot (Kotlin) Basic Setup — Full Series

  1. Why multi-module structure? (Architecture philosophy & overall design)
  2. API Response format design
  3. Global Exception Handling (GlobalExceptionHandler)
  4. Swagger (OpenAPI) configuration
  5. Security (JWT) basic skeleton
  6. JWT TokenProvider
  7. Redis configuration
  8. Validation configuration
  9. Logging + MDC (traceId) setup
  10. application.yml profile separation (local/dev/prod)
  11. Multi-module + JPA basic structure ← this article
  12. Complete project template git shared

✔ Why is JPA widely used?

JPA is one of the most commonly used ORM technologies in the Spring ecosystem.
Here are the main reasons:

  • CRUD is handled automatically without writing SQL — huge productivity boost
  • Domain-driven development becomes natural through entity design
  • Repository pattern cleanly separates DB access logic
  • Supports multiple databases easily (MySQL, PostgreSQL, etc.)
  • Deep integration with Spring (transactions, lazy loading, auditing, etc.)

 

✔ Where should JPA live in a multi-module architecture?

Below is the layer structure used in this project:

api           → Controller & Request/Response DTO
application   → Service (business logic)
domain        → Entity & Repository (JPA)

📌 Placement of JPA components

  • Entity → domain module
  • Repository (JpaRepository) → domain module
  • Service → application module (combines entities & business rules)
  • Controller → api module (handles request/response)

With this separation, each layer has a clear responsibility,
and the request flow api → application → domain becomes clean and intuitive.

 

✔ Minimal JPA example for initial setup

The following example is NOT included inside the shared template zip file.
It is only used to help explain the multi-module + JPA structure in this article.
Real domain implementations depend on each project’s business requirements.

📌 Entity (domain module)

@Entity
@Table(name = "user")
class User(
    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    val id: Long = 0,
    val name: String,
    val email: String
)

📌 Repository (domain module)

interface UserRepository : JpaRepository<User, Long>

📌 Service (application module)

@Service
class UserService(
    private val userRepository: UserRepository
) {
    fun findUser(id: Long): User =
        userRepository.findById(id).orElseThrow()
}

📌 Controller (api module)

@RestController
@RequestMapping("/users")
class UserController(
    private val userService: UserService
) {
    @GetMapping("/{id}")
    fun getUser(@PathVariable id: Long): ApiResponse<UserResponse> {
        val user = userService.findUser(id)
        return ApiResponse.ok(UserResponse.from(user))
    }
}

 

✔ Auto Configuration works out of the box

On Spring Boot 3.x, no additional JPA scanning configuration is required.

  • @EnableJpaRepositories → not needed
  • @EntityScan → not needed
  • As long as the domain module is included as a dependency, Spring detects everything automatically

In other words, for this template project, datasource + jpa settings in application.yml are sufficient to make JPA work properly.

 

✔ Conclusion

This article focused not on JPA itself, but on how JPA should be organized and placed within a multi-module project.
Following this structure greatly improves maintainability and scalability as the project grows.

In the next article, we will share the complete server template as a downloadable git.

 

https://jaemoi8.tistory.com/51

 

Spring Boot(Kotlin) — ep.12 Sharing the Complete Project

📚 Spring Boot (Kotlin) Initial Setup — Complete SeriesWhy Multi-Module Architecture? (Architecture Philosophy & Overall Structure)API Response Format DesignGlobal Exception Handling (GlobalExceptionHandler)Swagger (OpenAPI) ConfigurationSecurity (JWT)

jaemoi8.tistory.com