Search code examples
javahibernatehibernate-mapping

Hibernate specify @Column annotation only once on basic type


I have a lot of Boolean fields in my class and I'm storing them in TINYINT(1)

It's not a problem putting the annotation once, but I have a lot of them and this code looks a bit messy:

@Column(nullable = false, columnDefinition = "TINYINT(1)")
private Boolean isRescheduled;
@Column(nullable = false, columnDefinition = "TINYINT(1)")
private Boolean isCancelled;
@Column(nullable = false, columnDefinition = "TINYINT(1)")
private Boolean isFullDay;

Is it possible to specify @Column for Boolean once?


Solution

  • You need your own Dialect.

    1. Extend a dialect for your database, for example MyDialect extends MySQL5Dialect.

    2. In the constructor of MyDialect override a type

      registerColumnType(Types.BOOLEAN, "tinyint(1)");

    3. Use MyDialect in the hibernate.cfg.xml or hibernate.properties.