0

I have some entities where some fields are represented as enums, there is an error when starting spring boot. below is the code, tell me what to use?

    @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]

annotations over entity classes don't help @TypeDef

  • An enum in the database or an enum in java? If it is the latter you don't need the type, if it is you need a custom type which, as the error shows, needs to point to a class which converts between the java type and the db type. – M. Deinum Nov 14 '22 at 07:36
  • @M. Deinum enums in java. classes Work and Level . Do you mean to do something different instead of @Enumerated(EnumType.STRING)? – aleksandr1994 Nov 14 '22 at 07:46
  • Then why use `@Type` that isn't needed. Postgres also has the notion of enumerations if you don't use that remove the `@Type`. – M. Deinum Nov 14 '22 at 07:53
  • By the way, the @TypeDef(name = "pgsql_enum", typeClass = PostgreSQLEnumType.class) annotation helped. project was started – aleksandr1994 Nov 14 '22 at 07:56
  • 1
    What I meant was do you use enumerations in your PostgreSQL database, if not you don't need the `@Type`. So if you want to store in a VARCHAR column you don't need the postgres enum as that would only make things more complex. So basically when something is an enum in java doesn't mean it has to be an enum in the database. – M. Deinum Nov 14 '22 at 08:01
  • @M. Deinum, No, I don't store these enums in PostgreSQL database. I understand correctly that it is better for me to remove my annotations Type under fields and TypeDef uder class? – aleksandr1994 Nov 14 '22 at 08:09
  • If you don't have enums in postgres remove them. – M. Deinum Nov 14 '22 at 08:42

0 Answers0