2

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?

Luke
  • 1,163
  • 2
  • 11
  • 19
  • 1
    Why do you want to include a hash in the column name? – Dehan Oct 17 '18 at 06:47
  • Because there is official Oracle table with such column name. Sorry, I will add it above for better understanding. – Luke Oct 17 '18 at 07:09
  • 1
    Have you tried escaping the `#`? – Sven Hakvoort Oct 17 '18 at 07:35
  • Unfortunately, it doesn't work. There is still the same error that the table is not found by h2 dialect. – Luke Oct 17 '18 at 07:42
  • Create a view and map your entity to that? – Alan Hay Oct 17 '18 at 07:49
  • I would prefer some other workaround without impact on Oracle database side. To keep the table as is. Maybe to create entity under h2 profile where the problematic column should be without hash symbol. Then h2 could load it correctly. Not sure. – Luke Oct 17 '18 at 07:56
  • 2
    You can override anything via an orm.xml file. You'd just need a mechanism to ensure it was only active for H2. https://docs.jboss.org/hibernate/stable/annotations/reference/en/html/xml-overriding.html. See also https://stackoverflow.com/q/8750777/1356423 – Alan Hay Oct 17 '18 at 07:59
  • That's interesting. I'll have a look at it and let you know next time. – Luke Oct 17 '18 at 08:17

1 Answers1

0

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!");
Luke
  • 1,163
  • 2
  • 11
  • 19