Search code examples
mysqlhibernateenumshbm2ddl

Tell Hibernate's hbm2ddl to add MySQL enum columns for @Enumerated annotated fields


I'm creating a DB table using hbm2ddl with Java code similar to the following:

@Entity
public  class Filter {
    public enum Type {
        TypeA, TypeB;
    }
    @Enumerated(EnumType.STRING)
    private Type type;
}

It works fine, but for "type" a VARCHAR column is created, i.e. the DDL code looks like this:

CREATE TABLE IF NOT EXISTS `filter` (`type` varchar(255) DEFAULT NULL)

But what I want to have is this:

CREATE TABLE IF NOT EXISTS `filter` (`type` enum('TypeA','TypeB') NOT NULL)

Is this possible to declare in Hibernate, preferred with annotations?

Or is there a way to extend SchemaUpdate and overwrite the method that renders the alter script part for enumerated field the way I like it?

Background: The same database is used in a PHP part of the project and I want to prevent that invalid values are inserted.


Solution

  • I believe that's going to be complicated, since the java.sql.Types, which define the sql types treated by java, does not have enum type (since it's not a standardized type according to SQL-92).

    If that was the case you could create a hibernate custom UserType extending the EnumType and setting the sqlType accordingly, but since java.sql.Types doesn't handle it I don't see how to use native sql enum.

    best regards!