2 분 소요


The core module is a module created for shared use by module-api and module-stream that we created.

The main reason is to share domain entities. If each server or application manages them separately, there’s the inconvenience of having to modify each server or application when entity changes occur.

So by creating entities in the core module and adding the core module as a dependency in each module, you can use the entities in the core module.

Writing the Core Module

  • We’ll use the module-core module that we wrote in the previous session.
  • In module-core’s build.gradle.kts, only the plugin, allOpen, and noArg sections are declared without any specific information.
      plugins{
    
      }
    
      allOpen {
          annotation("javax.persistence.Entity")
          annotation("javax.persistence.Embeddable")
          annotation("javax.persistence.MappedSuperclass")
      }
    
      noArg {
          annotation("javax.persistence.Entity") // Apply no arg plugin only to classes with @Entity
          annotation("javax.persistence.Embeddable")
          annotation("javax.persistence.MappedSuperclass")
      }
    
      dependencies{
    
      }
    
  • Since we already declared common settings in the Root folder of SpringBoot-Multimodules, dependencies can be used without additional configuration.

Writing Entities

Declaring the Package for Entities

  • Create a com.wool.entity package in the src/main/kotlin folder of module-core.
  • Remember that com.wool needs to be used not only in module-core but also in module-api and module-stream.
  • Let’s create Customer and Order entities

Creating Entity 1 - BaseEntity

  • We’re going to create a BaseEntity. Rather than serving another role, it will handle the created_at and updated_at DB columns.
  • If you just write it normally, you’d have to keep writing it commonly across all packages, but by creating a BaseEntity and inheriting from it, created_at and updated_at become available in all entities
  • Create a base package under the com.wool.entity package, and create a BaseEntity data class.
      package com.wool.entity.base
    
      import org.springframework.data.annotation.CreatedDate
      import org.springframework.data.annotation.LastModifiedDate
      import org.springframework.data.jpa.domain.support.AuditingEntityListener
      import java.time.LocalDateTime
      import javax.persistence.Column
      import javax.persistence.EntityListeners
      import javax.persistence.MappedSuperclass
    
      @MappedSuperclass
      @EntityListeners(value = [AuditingEntityListener::class])
      open class BaseEntity(
          @CreatedDate
          @Column(name = "created_at", nullable = false, updatable = false, columnDefinition = "DATE")
          var createdAt: LocalDateTime = LocalDateTime.now(),
    
          @LastModifiedDate
          @Column(name = "updated_at", nullable = false, columnDefinition = "DATE")
          val updatedAt: LocalDateTime = LocalDateTime.now(),
      )
    
  • Now let’s create Customer and Order entities using BaseEntity

Creating Entity 2 - Customer

  • Let’s create the Customer entity. Since this project is more about using multi-module well rather than how well you write entities, I’ll skip detailed explanations and settings for entities.
  • Create a simple Customer entity that can store customerNickname and Address as shown below.
  • Let’s inherit from BaseEntity()
      package com.wool.entity
    
      import com.wool.entity.base.BaseEntity
      import org.jetbrains.annotations.NotNull
      import javax.persistence.Column
      import javax.persistence.Entity
      import javax.persistence.GeneratedValue
      import javax.persistence.GenerationType
      import javax.persistence.Id
    
    
      @Entity
      data class Customer(
          @Id
          @GeneratedValue(strategy = GenerationType.IDENTITY)
          val customerId: Long = 0,
    
          @NotNull
          @Column
          val customerNickName: String,
    
          @NotNull
          @Column
          val customerAddress: String,
      ): BaseEntity()
    

Creating Entity 3 - Order

  • Let’s create the Order entity. Let’s make Order hold order information and Customer information
  • Similarly, let’s inherit from BaseEntity()
      package com.wool.entity
    
      import com.wool.entity.base.BaseEntity
      import org.jetbrains.annotations.NotNull
      import java.util.UUID
      import javax.persistence.*
    
      @Entity
      data class Order(
          @Id
          @GeneratedValue(strategy = GenerationType.IDENTITY)
          val orderId: Long = 0,
    
          @NotNull
          @Column
          val orderUUID: String = UUID.randomUUID().toString(),
    
          @NotNull
          @Column
          val orderStoreName: String,
    
          @NotNull
          @Column
          val orderStoreAddress: String,
    
          @NotNull
          @Column
          val orderItem: String,
    
          @NotNull
          @Column
          val orderPrice: Int,
    
          @ManyToOne(fetch = FetchType.EAGER)
          @JoinColumn(name = "customerId")
          val customer: Customer,
      ) : BaseEntity()
    

Wrapping Up module-core

  • Surprisingly, module-core only had entities configured, so there wasn’t much to touch.
  • We’ve now finished module-core. Let’s create module-api that will use module-core

댓글남기기