Search code examples
springjpatestingentityh2

Spring JPA entity and non-supported hash symbol in column name under h2


Let me show you the problem on following scenarios:

I have entity with column name VERSION# with hash like suffix and I would like to make some testing with this entity under h2 database. The column is from official Oracle dictionary table USER_TYPE_VERSIONS so that this cannot be modified.

However the h2 doesn't support such column names and hence it fails during test running. Is it possible to manage it somehow? Such as replacing column name under h2 profile to VERSION or any other workaround?


Solution

  • The solution is that the column VERSION# needs to be escaped with quotes in model class something like that:

    @Entity
    @Table("USER_TYPE_VERSIONS")
    public class UserTypeVersion {
    
       @Column("\"VERSION#\"")
       private String version;
       ...
    }
    

    Finally the hibernate is now able to load such entity under h2 database. It's recognizable by itself. So any special characters should be replaced in this way.

    Note: Don't forget to mention such change also in data.sql if you have some test data.

    insert into USER_TYPE_VERSIONS("VERSION#") values ("IT WORKS NOW!");