Skip to content

Commit

Permalink
Merge pull request #6 from mash-up-kr/junhyoung/AddQuerydslSetting
Browse files Browse the repository at this point in the history
feat: Add QueryDSL configuration, split BaseEntity and BaseTimeEntity & enable JPA Auditing
  • Loading branch information
toychip authored Jun 16, 2024
2 parents 4bd4b76 + 9aec470 commit bedb233
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ControllerExceptionAdvice {
}

@ExceptionHandler(DojoException::class)
fun handleMoitException(ex: DojoException): ResponseEntity<DojoApiResponse<Any>> {
fun handleDojoException(ex: DojoException): ResponseEntity<DojoApiResponse<Any>> {
log.error { "Dojo Exception Handler $ex" }
return errorResponse(ex.httpStatusCode, ex.toApiErrorResponse())
}
Expand Down
24 changes: 18 additions & 6 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ plugins {
idea
}

val properties = project.properties

java {
sourceCompatibility = JavaVersion.VERSION_21
sourceCompatibility = JavaVersion.toVersion(properties["javaVersion"] as String)
}

allprojects {
Expand Down Expand Up @@ -45,13 +47,13 @@ subprojects {
testImplementation("org.springframework.boot:spring-boot-starter-test")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
// logging
implementation("io.github.oshai:kotlin-logging-jvm:5.1.0")
implementation("io.github.oshai:kotlin-logging-jvm:${properties["kotlinLoggingJvmVersion"]}")
}

tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs += "-Xjsr305=strict"
jvmTarget = "21"
jvmTarget = properties["javaVersion"] as String
}
}

Expand Down Expand Up @@ -87,11 +89,21 @@ project(":entity") {

dependencies {
api("org.springframework.boot:spring-boot-starter-data-jpa")
api("com.mysql:mysql-connector-j:8.4.0")
runtimeOnly("com.h2database:h2") // todo : fade out
api("com.mysql:mysql-connector-j:${properties["mysqlConnectorVersion"]}")
runtimeOnly("com.h2database:h2:${properties["h2DatabaseVersion"]}") // todo : fade out

// Jasypt
implementation("com.github.ulisesbocchio:jasypt-spring-boot-starter:3.0.5")
implementation("com.github.ulisesbocchio:jasypt-spring-boot-starter:${properties["jasyptSpringBootStarterVersion"]}")

// Querydsl
implementation("io.github.openfeign.querydsl:querydsl-core:${properties["queryDslVersion"]}")
implementation("io.github.openfeign.querydsl:querydsl-jpa:${properties["queryDslVersion"]}")
annotationProcessor("io.github.openfeign.querydsl:querydsl-apt:${properties["queryDslVersion"]}:jpa")
annotationProcessor("jakarta.annotation:jakarta.annotation-api")
annotationProcessor("jakarta.persistence:jakarta.persistence-api")

// query 값 정렬
implementation("com.github.gavlyukovskiy:p6spy-spring-boot-starter:${properties["p6spyVersion"]}")
}
}

Expand Down
1 change: 1 addition & 0 deletions entity/src/main/kotlin/com/mashup/dojo/SampleEntity.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mashup.dojo

import com.mashup.dojo.base.BaseEntity
import jakarta.persistence.Entity

@Entity
Expand Down
35 changes: 35 additions & 0 deletions entity/src/main/kotlin/com/mashup/dojo/base/BaseEntity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.mashup.dojo.base

import jakarta.persistence.Column
import jakarta.persistence.EntityListeners
import jakarta.persistence.MappedSuperclass
import org.springframework.data.annotation.CreatedBy
import org.springframework.data.annotation.LastModifiedBy
import org.springframework.data.jpa.domain.support.AuditingEntityListener

@MappedSuperclass
@EntityListeners(AuditingEntityListener::class)
abstract class BaseEntity : BaseTimeEntity() {
@CreatedBy
@Column(updatable = false)
var createdBy: String? = null
private set

@LastModifiedBy
var lastModifiedBy: String? = null
private set

@Column(nullable = false)
var isDeleted: Boolean = false
private set

// 재활성화 - soft delete
fun activate() {
this.isDeleted = false
}

// 비활성화 - soft delete
fun deactivate() {
this.isDeleted = true
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mashup.dojo
package com.mashup.dojo.base

import jakarta.persistence.Column
import jakarta.persistence.EntityListeners
Expand All @@ -13,20 +13,19 @@ import java.time.LocalDateTime

@MappedSuperclass
@EntityListeners(AuditingEntityListener::class)
abstract class BaseEntity {
abstract class BaseTimeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
val id: Long = 0L

@CreatedDate
@Column(name = "created_at", nullable = false)
@Column(name = "created_at", nullable = false, updatable = false)
lateinit var createdAt: LocalDateTime
private set

@LastModifiedDate
@Column(name = "updated_at", nullable = false)
lateinit var updatedAt: LocalDateTime

@Column(name = "is_deleted", nullable = false)
var isDeleted: Boolean = false
private set
}
35 changes: 35 additions & 0 deletions entity/src/main/kotlin/com/mashup/dojo/config/BaseEntityConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.mashup.dojo.config

import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.data.domain.AuditorAware
import org.springframework.data.jpa.repository.config.EnableJpaAuditing
import java.util.Optional
import java.util.UUID

@EnableJpaAuditing
@Configuration
class BaseEntityConfig {
@Bean
fun auditorProvider(): AuditorAware<String> {
return AuditorAware {
/*
val authentication: Authentication? = SecurityContextHolder.getContext().authentication
if (authentication == null) {
return@AuditorAware Optional.of("AnonymousNULL")
}
val principal: Any = authentication.principal
if (principal is Member) {
val email: String? = principal.email
return@AuditorAware Optional.ofNullable(email)
}
// principal이 Member 타입이 아닌 경우의 처리
Optional.of("AnonymousNOT_TYPE")
*/

// 임시로 UUID 생성하여 반환
Optional.of(UUID.randomUUID().toString())
}
}
}
18 changes: 18 additions & 0 deletions entity/src/main/kotlin/com/mashup/dojo/config/QuerydslConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.mashup.dojo.config

import com.querydsl.jpa.impl.JPAQueryFactory
import jakarta.persistence.EntityManager
import jakarta.persistence.PersistenceContext
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration
class QuerydslConfig {
@PersistenceContext
private lateinit var entityManager: EntityManager

@Bean
fun jpaQueryFactory(): JPAQueryFactory {
return JPAQueryFactory(entityManager)
}
}
7 changes: 7 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
javaVersion=21
kotlinLoggingJvmVersion=5.1.0
queryDslVersion=6.0
jasyptSpringBootStarterVersion=3.0.5
mysqlConnectorVersion=8.4.0
h2DatabaseVersion=2.1.210
p6spyVersion=1.9.0

0 comments on commit bedb233

Please sign in to comment.