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

后端 未结 6 2132
感情败类
感情败类 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:32

    @Type annotation is an Hibernate annotation.

    In full JPA2 (with Hibernate 3.6+), the way to map a Boolean field to a TINYINT(1) SQL type instead of BIT(1), is to use the columnDefinition attribute.

    @Column(nullable = false, columnDefinition = "TINYINT(1)")
    private boolean enabled;
    

    nb: length attribute seems to have no effect in this case, then we use (1) syntax.


    With Hibernate 4.0+, this kind of syntax can cause an runtime error like this :

    Wrong column type Found: bit, expected: TINYINT(1)
    

    It seems that in this case, your only way is to use tinyInt1isBit=false in the MySQL datasource connection string like this :

    jdbc:mysql://server_host:3306/database?tinyInt1isBit=false
    

    By the way, you can now use the length attribute like this :

    @Column(nullable = false, columnDefinition = "TINYINT", length = 1)
    private boolean enabled;
    

提交回复
热议问题