gpt4 book ai didi

postgresql - JPA 枚举(java.lang.ClassCastException : org. postgresql.util.PGobject)

转载 作者:行者123 更新时间:2023-11-29 12:47:29 25 4
gpt4 key购买 nike

我尝试使用@Enumerated 注释来映射我的枚举类型,但出现以下错误:

Exception in thread "main" java.lang.ClassCastException: org.postgresql.util.PGobject cannot be cast to java.lang.String

我做什么:在 postgres 中

create type "test_type" as enum ('test_1', 'test_2);

在Java中

public enum TestType{ test_1, test_2 }

@Entity @Table(name="test_table") public class TestTable { ...
@Enumerated(EnumType.STRING) @Column(name="col") private TestType col; ... }

最佳答案

在 JPA 中,枚举可以作为文本(枚举的名称)或数值(枚举的序数)持久化。 @Enumerated(EnumType.STRING) 表示您更愿意保留名称。因此数据库类型应该是varchar。您的 JPA 提供程序不知道 PostgreSQL 枚举。

关于postgresql - JPA 枚举(java.lang.ClassCastException : org. postgresql.util.PGobject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12145403/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com