Skip to content

JPA๋กœ PostgreSQL์˜ JSONB ์ปฌ๋Ÿผ์˜ JSON ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ณ ์ž ํ• ๋•Œ Hypersistance Utils๋ฅผ ์ ์šฉํ•˜์—ฌ ์‰ฝ๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

@Type(JsonType.class) โ€‹

๊ณต์‹ ๋ฌธ์„œ์— ์„ค๋ช…๋˜์–ด์žˆ๋Š” ๋Œ€๋กœ JsonType ๊ณผ JsonBinaryType ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

java
@Type(JsonBinaryType.class)
@Column(nullable = false, columnDefinition = "jsonb")
private Map<String, Object> metadata = new HashMap<>();

๋งŒ์•ฝ, ์ปฌ๋Ÿผ์— ๋Œ€ํ•œ columnDefinition๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๋งคํ•‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.

sh
Caused by: org.hibernate.MappingException: Unable to determine SQL type name for column 'metadata' of table 'users' because there is no type mapping for org.hibernate.type.SqlTypes code: 1111 (OTHER)

@JdbcTypeCode(SqlTypes.JSON) โ€‹

JsonType ๊ณผ JsonBinaryType ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค JdbcTypeCode์˜ ํƒ€์ž…์„ JSON ์œผ๋กœ ์ง€์ •ํ•˜๋Š”๊ฒŒ ๋” ๊ฐ„๋‹จํ•˜๋‹ค.

java
@JdbcTypeCode(SqlTypes.JSON)
private Metadata metadata;

@Data
public class Metadata {
    private boolean active;
}

์ž๋งคํ’ˆ์œผ๋กœ PostgreSQL ์—์„œ์˜ Enum ์ปฌ๋Ÿผ์— ๋Œ€ํ•œ @JdbcType(PostgreSQLEnumJdbcType::class)๋„ ํ™•์ธํ•ด๋ด์•ผ๊ฒ ๋„ค?

Released under the MIT License.