문제 상황
@Convert(converter = Convert::class)
@Column(columnDefinition = "jsonb", name = "REQUEST_DATA")
var a: Any = a
protected set
entity에 any로 받고 dto등을 변환해서 json문자열로 받는 필드가 있었다.
@Convert
class Convert(
private val objectMapper: ObjectMapper
): AttributeConverter<Any, String> {
override fun convertToDatabaseColumn(attribute: Any?): String {
return attribute?.let { objectMapper.writeValueAsString(it) } ?: ""
}
데이터가 들어오면 convertToDatabaseColumn 함수를 오버라이딩해서 json문자열로 변환한다.
그러나 실제로 이 entity로 postgreSql에 저장을 하려고 하면 에러가 발생했다.
column a is of type json but expression is of type character varying"
Hint: You will need to rewrite or cast the expression.
칼럼의 타입은 json인데, String이 들어왔다는 것이다.
처음에는 이해가 되지 않았다. converting할때 objectMapper.writeValueAsString으로 json문자열로 바꾸는게 아닌가?
그러나 코틀린에서는 json으로 변환해도 명시해주지 않으면 인식을 하지 못한다고 한다.
해결책은
@JdbcTypeCode(SqlTypes.JSON)
을 해당 entity필드 위에 붙여서 json타입임을 명시해주는 것이었다.
@JdbcTypeCode(SqlTypes.JSON)
hibernate 6.0버전 이상부터 지원해주는 어노테이션
왜 @Type은 동작하지 않았을까?
사용자 정의 데이터 유형이라서?
https://velog.io/@chas369/TypeDef-Type
https://bin-repository.tistory.com/166
How to use Postgres JSONB datatype with JPA?
Im not finding a way to map the JSON and JSONB datatypes from PostgreSQL using JPA (EclipseLink). Is some one using this datatypes with JPA and can give me some working examples?
stackoverflow.com
'D' 카테고리의 다른 글
프론트엔드 콜백 지옥과 promise (1) | 2025.01.27 |
---|---|
라이브러리 의존 (0) | 2025.01.26 |
@JsonProperty 깊게 이해하기 (1) | 2025.01.18 |
제로 쓰러스트 추가하기 + 인터셉터에서 RequestBody 조회 (0) | 2025.01.01 |
yml과 환경에 따라 yml파일 분기 되는 이유 (0) | 2024.12.03 |