티스토리 뷰

코프링으로 개발하면서 h2가 처음 create table을 할 때 BaseEntity에 있는 필드들은 인식하지 못하는 에러를 마주한 내용을 포스팅해봤습니다.

https://github.com/laboratory-kkoon9/kotlin-spring/tree/develop

배경

h2에서 초기 데이터를 밀어넣어주고 통합 테스트를 하려고 시도했습니다.

이 때 baseEntity와 이를 상속한 cafe entity 코드입니다.

package com.laboratorykkoon9.kotlinspring.common

import jakarta.persistence.Column
import org.springframework.data.annotation.CreatedDate
import org.springframework.data.annotation.LastModifiedDate
import java.time.LocalDateTime

open class BaseEntity {
    @Column(name = "created_at")
    @CreatedDate
    val createdAt: LocalDateTime? = LocalDateTime.now()

    @Column(name = "updated_at")
    @LastModifiedDate
    var updatedAt: LocalDateTime? = null
}
package com.laboratorykkoon9.kotlinspring.cafe.domain

import com.laboratorykkoon9.kotlinspring.common.BaseEntity
import jakarta.persistence.*
import java.time.LocalTime

@Entity
@Table(name = "cafe")
class Cafe(
    name: String,
    latitude: String? = null,
    longitude: String? = null,
    mainAddress: String? = null,
    detailAddress: String? = null,
    openedAt: LocalTime? = null,
    closedAt: LocalTime? = null,
    activate: Boolean = true
) : BaseEntity() {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    val id: Long? = null

    @Column(name = "name")
    var name: String = name
        private set

    @Column(name = "latitude")
    var latitude: String? = latitude
        private set

    @Column(name = "longitude")
    var longitude: String? = longitude
        private set

    @Column(name = "main_address")
    var mainAddress: String? = mainAddress
        private set

    @Column(name = "detail_address")
    var detailAddress: String? = detailAddress
        private set

    @Column(name = "opened_at")
    var openedAt: LocalTime? = openedAt
        private set

    @Column(name = "closed_at")
    var closedAt: LocalTime? = closedAt
        private set

    @Column(name = "activate")
    var activate: Boolean = activate
        private set

    companion object {
        val CAFE_OPEN_TIME: LocalTime = LocalTime.of(9, 0)
        val CAFE_CLOSE_TIME: LocalTime = LocalTime.of(18, 0)
    }

    fun update(
        name: String? = null,
        latitude: String? = null,
        longitude: String? = null,
        mainAddress: String? = null,
        detailAddress: String? = null,
        openedAt: LocalTime? = null,
        closedAt: LocalTime? = null,
    ) {
        this.name = name ?: this.name
        this.latitude = latitude ?: this.latitude
        this.longitude = longitude ?: this.longitude
        this.mainAddress = mainAddress ?: this.mainAddress
        this.detailAddress = detailAddress ?: this.detailAddress
        this.openedAt = openedAt ?: this.openedAt
        this.closedAt = closedAt ?: this.closedAt
    }

    fun updateStatus() {
        this.activate = !this.activate
    }
}

 

다음은 sql insert script입니다.

INSERT INTO cafe(id, name, latitude, longitude, main_address, detail_address, opened_at, closed_at, activate, created_at, updated_at) VALUES (1,'둥겸의 커피집', '31', '32', '삼전역', '1층', '10:00', '19:00', true, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);

테스트 코드가 실행되기 전에 cafe table이 생성되고 sql script가 실행되는데, 이 때 다음과 같은 에러가 발생했습니다.

Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Column "CREATED_AT" not found; SQL statement:

cafe entity가 생성될 때 baseEntity에 있는 created_at와 updated_at이 누락되어 발생한 에러였습니다.

chatGPT에 물어보니, 다음과 같은 답변을 주었습니다.

실제로, baseEntity에 @MappedSuperclass를 추가해주니, 정상적으로 동작하였습니다~

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
글 보관함