Spring Boot启动报错:无法加载pgsql_enum类,枚举字段配置求助
问题描述
在Spring Boot项目中,实体类使用枚举类型映射PostgreSQL的枚举字段时,启动出现错误:
Failed to initialize JPA EntityManagerFactory: Unable to load class [pgsql_enum]
相关实体类代码:
@Getter @Setter @ToString //@TypeDef(name = "Level", typeClass = ru.somepackege.Level.class) //@TypeDef(name = "Work", typeClass = ru.somepackege.Work.class) @Entity @Table(name = "employee") public class Employee { @Id @SequenceGenerator( name = "employee_id_seq", sequenceName = "employee_id_seq", allocationSize = 1) @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "employee_id_seq") private Integer id; @Column(name = "work_experience") @Enumerated(EnumType.STRING) @Type(type = "pgsql_enum") private Work workExperience; @Column(name = "level_competencies") @Enumerated(EnumType.STRING) @Type(type = "pgsql_enum") private Level levelCompetencies; @Column(name = "is_active") private boolean isActive; @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false; KpiEmployeeProfile that = (KpiEmployeeProfile) o; return id != null && Objects.equals(id, that.id); } @Override public int hashCode() { return getClass().hashCode(); } }
启动错误日志:
main] [ERROR] j.LocalContainerEntityManagerFactoryBean: [] Failed to initialize JPA EntityManagerFactory: Unable to load class [pgsql_enum] main] [WARN ] ConfigServletWebServerApplicationContext: [] Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [pgsql_enum] org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [pgsql_enum]
尝试过在实体类添加@TypeDef注解但无效,求正确配置方式。
解决方案
错误根源是Hibernate无法找到pgsql_enum对应的类型处理器,按以下步骤配置即可解决:
1. 引入依赖
添加Hibernate对PostgreSQL枚举的支持依赖,根据Hibernate版本选择对应包:
<!-- Maven 依赖(Hibernate 5.x 用这个) --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-types-52</artifactId> <version>2.19.0</version> </dependency> <!-- 若用Hibernate 6.x,替换为 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-types-60</artifactId> <version>2.19.0</version> </dependency>
2. 配置类型映射
方式一:全局注册(推荐)
在application.properties中添加全局配置,让Hibernate自动识别PostgreSQL枚举:
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect spring.jpa.properties.hibernate.type_contributors=com.vladmihalcea.hibernate.type.PostgresEnumTypeContributor
方式二:实体类局部配置
如果不需要全局配置,在实体类上正确注册类型映射,注意@TypeDef要指定完整的类型处理器类名:
@Getter @Setter @ToString @TypeDefs({ @TypeDef(name = "pgsql_enum", typeClass = PostgresEnumType.class) }) @Entity @Table(name = "employee") public class Employee { @Id @SequenceGenerator( name = "employee_id_seq", sequenceName = "employee_id_seq", allocationSize = 1) @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "employee_id_seq") private Integer id; // columnDefinition填写数据库中实际的枚举类型名 @Column(name = "work_experience", columnDefinition = "work_enum") @Type(type = "pgsql_enum") private Work workExperience; @Column(name = "level_competencies", columnDefinition = "level_enum") @Type(type = "pgsql_enum") private Level levelCompetencies; // 其他字段和方法... }
3. 清理冲突注解
无需同时使用@Enumerated(EnumType.STRING)和@Type(type = "pgsql_enum"),保留@Type注解即可,PostgresEnumType会自动处理枚举与PostgreSQL枚举类型的映射。
4. 确认数据库枚举已创建
确保PostgreSQL中已创建对应枚举类型,比如:
CREATE TYPE work_enum AS ENUM ('JUNIOR', 'MIDDLE', 'SENIOR'); CREATE TYPE level_enum AS ENUM ('LOW', 'MEDIUM', 'HIGH');
内容的提问来源于stack exchange,提问作者aleksandr1994
相关产品推荐
相关产品推荐

