Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hibernet6 Enum type DDL issue #652

Open
frangere opened this issue Jan 22, 2024 · 2 comments
Open

Hibernet6 Enum type DDL issue #652

frangere opened this issue Jan 22, 2024 · 2 comments

Comments

@frangere
Copy link

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.

@jrpedrianes
Copy link

jrpedrianes commented Jun 5, 2024

In Postgres, there's a similar problem when we use an "Instant" column with Hibernate. Hibernate creates a type like timestamp(6) with time zone. But the toDataType function doesn't handle this properly and removes the time zone info, so it ends up as just timestamp(6).

@MikeFear
Copy link

Any updates on this?
This bug requires us to write all changelogs regarding enums manually.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants