且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Hibernate异常:枚举类的未知名称值

更新时间:2023-01-24 12:36:41

Hibernate没有知道并关心你的枚举中的id字段。所有它知道的是序数值(0和1)和名称(FISICA和JURIDICA)。如果要保留F和J,则必须将两个枚举常量重命名为F和J,并在实体中注释该字段:

Hibernate doesn't know and care about the id field inside your enum. All it knows about is the ordinal value (0 and 1) and the name (FISICA and JURIDICA). If you want to persist F and J, you'll have to rename your two enum constants to F and J, and annotate the field in the entity like this:

@Column(nullable=false, length=1)
@Enumerated(EnumType.STRING)
private TipoPessoa tipoPessoa;

或使用自定义用户类型将F转换为FISICA,反之亦然。

or use a custom user type to transform F to FISICA or vice-versa.