Search code examples
springspring-boothibernatekotlin

How to define column type in Spring/Hibernate when PSQL column type is character(2)


I have this Postgres CREATE TABLE statement

CREATE TABLE public.foo (
                                  id bigint NOT NULL,
                                  language character(2) NOT NULL,
                                  updated timestamp with time zone NOT NULL
);

Now, this is how I try to define the column language in my @Entity foo in Kotlin

...
@Enumerated(EnumType.STRING)
@Column val language: AvailableLanguages,
...

AvailableLanguages is an enum

enum class AvailableLanguages {
    fr, en
}

However, this gives me the error

... org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: wrong column type encountered in column [language] in table [foo]; found [bpchar (Types#CHAR)], but expecting [varchar(255) (Types#VARCHAR)]

I tried

@Column(length = 2)

which does not change anything.


Solution

  • As I see you have some miss configuration.

    To fix it you have to rewrite a bit your sql and that is all.

    CREATE TABLE public.foo (
                                      id bigint NOT NULL,
                                      language varchar(2) NOT NULL,
                                      updated timestamp with time zone NOT NULL
    );
    

    Because hibernate set default definition of column String in varchar, but if you want to change it you have to set columnDefinition parameter in @Column annotation.