postgresql Hibernate 6.2:Java枚举和Postgres枚举之间的Map

wmvff8tz  于 2023-06-29  发布在  PostgreSQL
关注(0)|答案(1)|浏览(159)

我有一个带有Spring Data(Hibernate 6.2)和Postgres 14.4.0Spring Boot 3.1.0项目。我也用io.hypersistence:hypersistence-utils-hibernate-62:3.5.1。我想在Postgres枚举和Java枚举之间Map。
Postgres枚举:

event_type    |
--------------+
usercreated   |
userupdated   |
userdeleted   |

Java枚举:

public enum EventType {
  usercreated,
  userupdated,
  userdeleted
}

JavaMap:

@Enumerated(EnumType.STRING)
@Column(name = "event_type", nullable = false)
@Type(PostgreSQLEnumType.class)
private EventType eventType;

当我运行这个应用程序时,我得到了这个错误:

Caused by: org.hibernate.MappingException: Unable to determine SQL type name for column 'event_type' of table 'user_updates'
    at org.hibernate.mapping.Column.getSqlTypeName(Column.java:281)
    at org.hibernate.mapping.Column.getSqlType(Column.java:339)
    at org.hibernate.tool.schema.internal.ColumnDefinitions.hasMatchingType(ColumnDefinitions.java:28)
    at org.hibernate.tool.schema.internal.AbstractSchemaValidator.validateColumnType(AbstractSchemaValidator.java:163)
    at org.hibernate.tool.schema.internal.AbstractSchemaValidator.validateTable(AbstractSchemaValidator.java:152)
    at org.hibernate.tool.schema.internal.GroupedSchemaValidatorImpl.validateTables(GroupedSchemaValidatorImpl.java:46)
    at org.hibernate.tool.schema.internal.AbstractSchemaValidator.performValidation(AbstractSchemaValidator.java:97)
    at org.hibernate.tool.schema.internal.AbstractSchemaValidator.doValidation(AbstractSchemaValidator.java:75)
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:293)
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.lambda$process$5(SchemaManagementToolCoordinator.java:143)
    at java.base/java.util.HashMap.forEach(HashMap.java:1421)
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:140)
    at org.hibernate.boot.internal.SessionFactoryObserverForSchemaExport.sessionFactoryCreated(SessionFactoryObserverForSchemaExport.java:37)
    at org.hibernate.internal.SessionFactoryObserverChain.sessionFactoryCreated(SessionFactoryObserverChain.java:35)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:291)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:431)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1455)
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:66)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:376)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409)
    ... 25 common frames omitted
Caused by: org.hibernate.HibernateException: No type mapping for org.hibernate.type.SqlTypes code: 1111 (OTHER)
    at org.hibernate.type.descriptor.sql.spi.DdlTypeRegistry.getTypeName(DdlTypeRegistry.java:205)
    at org.hibernate.type.descriptor.sql.spi.DdlTypeRegistry.getTypeName(DdlTypeRegistry.java:184)
    at org.hibernate.mapping.Column.getSqlTypeName(Column.java:277)
    ... 44 common frames omitted
xwbd5t1u

xwbd5t1u1#

基于Vlad Mihalcea答案here,我只需要将columnDefinition = "POSTGRES_ENUM_TYPE"添加到@column定义中,如下所示:

@Enumerated(EnumType.STRING)
@Column(name = "event_type", nullable = false, columnDefinition = "event_type")
@Type(PostgreSQLEnumType.class)
private EventType eventType;

相关问题