Hibernate 6+ 无法将 java 枚举映射到 postgres 枚举

pla*_*ket 6 postgresql hibernate

升级到 hibernate 6 后,我遇到了 postgres 保存枚举类型的问题。我无法再使用@TypeDef注释,因为它已被删除。

@Enumerated(value = EnumType.STRING)
@Type(MyPSQLType.class)
private MyType myType;
Run Code Online (Sandbox Code Playgroud)

postgres 类型定义为

public class MyPSQLType extends org.hibernate.type.EnumType<MyType> {
    @Override
    public void nullSafeSet(PreparedStatement st, MyType value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException {
        st.setObject(index, value != null ? value.name() : null, Types.OTHER);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我现在遇到的错误。既然 TypeDef 不再适用,我现在是否必须以不同的方式注册自定义类型?

Caused by: org.postgresql.util.PSQLException: ERROR: operator does not exist: my_type = character varying
  Hint: No operator matches the given name and argument types. You might need to add explicit type casts.
  Position: 187
Run Code Online (Sandbox Code Playgroud)

pla*_*ket 1

使用 hibernate 6 提供的类型解决了我的问题

@Type(PostgreSQLEnumType.class)
Run Code Online (Sandbox Code Playgroud)