Search code examples
javaspringhibernatemigrationflyway

flyway migration on existing database results in "wrong column type encountered in column"


i have spring boot application that uses my database schema. Lets say i have non-empty schema with table APPLICATION_USERS defined as follows:

create table AREA
(
  name NVARCHAR2(36) not null,
  id   NUMBER not null
)

now i wanted to add FlyWay to my application. I have defined in my application properties flyway.baselineOnMigrate=true to start initial Flyway deployment and spring.jpa.hibernate.ddl-auto=validate to validate my Enities agains schema, however upon starting application i get following error:

 wrong column type encountered in column [name] in table [APPLICATION_USERS]; found [nvarchar2 (Types#OTHER)], but expecting [varchar2(255 char) (Types#VARCHAR)]

as far as i understand it, it complains about NVARCHAR2, as it expect varchar how can i force hibernate to accept nvarchar2 as varchar?

i know i can use columnDefinition on my entity attributes, however this is not my dream solution, is there any other way?


Solution

  • I assume that you are using Oracle database.

    You have to annotate your the attribute name in your Entity with Nationalized

    @Nationalized
    private String name;
    

    You also could register your own dialect:

    public class CustomOracleDialect extends Oracle10gDialect {
    
        public CustomOracleDialect() {
            super();
            registerColumnType(Types.NVARCHAR, "nvarchar2($l)");
            registerHibernateType(Types.NVARCHAR, StandardBasicTypes.STRING.getName());
        }
    }