Hibernate JPA, MySQL and TinyInt(1) for Boolean instead of bit or char

后端 未结 6 2126
感情败类
感情败类 2020-12-12 23:38

Here is my JPA2 / Hibernate definition:

Code:
@Column(nullable = false)
private boolean enabled;

In MySql this column is resolved to a bit(

6条回答
  •  庸人自扰
    2020-12-13 00:28

    Try the NumericBooleanType. For some reason this doesn't have a declared short type name so you'd have to use:

    @Column(nullable = false)
    @Type(type = "org.hibernate.type.NumericBooleanType")
    private boolean enabled;
    

    This does map to an INTEGER type but it will probably work fine with a TINYINT.

    UPDATE: org.hibernate.type.NumericBooleanType Does not work with TINYINT in some RDBMS. Switch the database column type to INTEGER. Or use a different Java @Type value, or columnDefinition, as appropriate.

    In this example, Dude's answer of @Column(nullable = false, columnDefinition = "TINYINT(1)") would work without any database changes.

提交回复
热议问题