Skip to content

Hibernet6 Enum type DDL issue #652

Open

Description

Env:

Spring Boot - 3.2.1
Hibernet  - 6.4.1
Mysql - 5.7
Liquibase : 4.25.1
Liquibase Hibernate6: 4.25.1

Gradle:

liquibase {
    activities {
        main {
            defaultsFile "liquibase.properties"
            changelogFile "base.mysql.sql"
            referenceUrl "hibernate:spring:com.xxx.model?dialect=org.hibernate.dialect.MySQLDialect"
        }
    }
}


dependencies {
    liquibaseRuntime 'com.mysql:mysql-connector-j'
    liquibaseRuntime 'org.liquibase:liquibase-core:4.25.1'
    liquibaseRuntime 'info.picocli:picocli:4.7.5'
    liquibaseRuntime 'org.yaml:snakeyaml:2.0'
    liquibaseRuntime 'org.liquibase.ext:liquibase-hibernate6:4.25.1'
    liquibaseRuntime 'org.springframework.boot:spring-boot-starter'
    liquibaseRuntime 'org.springframework.boot:spring-boot-starter-data-jpa'
    liquibaseRuntime sourceSets.main.output
}

Entity class:


public enum Status {
    ACTIVE,
    INACTIVE
}

@Entity
@Getter
@Table(name = "user")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private Long id;

    @Enumerated(EnumType.STRING)
    @Column(nullable = false)
    private Status status;
}

Try to use: gradle diffChangelog to generate sql file, got:

CREATE TABLE user (id BIGINT AUTO_INCREMENT NOT NULL,  status ENUM NOT NULL, CONSTRAINT userPK PRIMARY KEY (id));

which result in wrong SQL.

expected:

CREATE TABLE user (id BIGINT AUTO_INCREMENT NOT NULL,  status ENUM('ACTIVE', 'INACTIVE') NOT NULL, CONSTRAINT userPK PRIMARY KEY (id));

Root cause:
According https://docs.jboss.org/hibernate/orm/6.2/migration-guide/migration-guide.html#ddl-implicit-datatype-enum, Hibernate 6 uses enum types on MySQL by default, but Liquibase hibernate6 class ColumnSnapshotGenerator#toDataType cannot process hibernateType with ENUM('ACTIVE', 'INACTIVE'), this method just keep ENUM as sql type.

Workaround:
Right now, we are add:

   @JdbcTypeCode(SqlTypes.VARCHAR)
   private Status status;

to make sure still using old stype VARCHAR for ENUM.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions