2

I am using jpa-buddy to generate liquibase changelog (sql format) from jpa (hibernate) entities. I am using hibernate-types-55 to map java Enum to posgres Enum. I do this as follow:

@Entity
@TypeDef(
        name = "pgsql_enum",
        typeClass = PostgreSQLEnumType.class
)
public class MyEntity {

  @Enumerated(EnumType.STRING)
  @Type(type = "pgsql_enum")
  private MyEnumType myEnum;
}

The generated DDL with jpa-buddy is:

CREATE TABLE my_entity
(
    my_enum  VARCHAR(255),
);

when remove

  @Enumerated(EnumType.STRING)

I get

CREATE TABLE my_entity
(
    my_enum  UNKNOWN__COM.VLADMIHALCEA.HIBERNATE.TYPE.BASIC.POSTGRESQLENUMTYPE,
)

The problem is I can't generate postgres enum type from entity.

what I am expecting is a generated DDL like:

create type my_enum_type as enum ('ENUM1', 'ENUM2', 'ENUM3');

CREATE TABLE my_entity ( 
    my_enum  my_enum_type, 
);

Has anyone managed to do this in the past ?

Thank you

Bakr
  • 51
  • 2

1 Answers1

2

I found the issue same with your issue:

Java Enums, JPA and Postgres enums - How do I make them work together?

You must create MyEnumConverter.class after import Hibernate-core.jar.

In your case, try:

@TypeDef(name="myEnumConverter", typeClass=MyEnumConverter.class)
public @Entity class MyEntity {
    
    public static enum Mood {ENUM1, ENUM2, ENUM3}
    
    @Type(type="myEnumConverter") MyEnumType myEnum;
}

But I think, using JPA entity isn't good for create table.
You should create table, view, etc,... by SQL native after that you create table if run success SQL statement.

Hungnn
  • 68
  • 1
  • 2
  • 19