14

the problem is as follows: We're using hibernate with annotations as O/R Mapper.

Some @Column annotations look like:

@Column(columnDefinition = "longblob", name = "binaryData", nullable = true)

or

@Column(columnDefinition = "mediumtext", name = "remark", nullable = true)

with the columnDefinition attributes being mysql specific

on postgres for example, the columnDefinition values should be "bytea" and "varchar(999999)"

and on oracle probably something else.

Problems arise currently at the time of Schema Export, e.g. when creating the DDL statements.

Possible workarounds that I can think of are - Hack some JDBC driver that does a text replace (e.g. longblob->bytea) for the DDL statements. That's ugly but will work somehow - Use hibernate xml configuration instead of annotations. That will probably work but I prefer annotations

Does anybody know any alternatives? Hibernate specific workarounds are ok, e.g. if the columnDefinition attribute can contain dialect specific values like

@Column(columnDefinition = "mysql->mediumtext, postgres->varchar(999999)", name = "remark", nullable = true)

Thanks Holger

user1946784
  • 1,397
  • 3
  • 14
  • 20
  • 1
    Do you know that, if you find an answer useful, you can vote for it. Click on the UP arrow on the left of the answer. :-) – KLE Dec 24 '09 at 14:07
  • This provides a solution specific for UUIDs, it's not pretty: https://stackoverflow.com/a/48918942/2102158 – Jon N Feb 22 '18 at 03:01

2 Answers2

12

Why don't you use the database-agnostic annotations like:

  • @Lob (on a byte[] or a String property)
  • @Column(length=90000) (on a String property)

and see what columns will be generated in the database. They will most likely be of the types you want them to be.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • Hello, thanks I wasn't aware of that, that seems to work. Best regards Holger – user1946784 Dec 24 '09 at 13:18
  • 3
    And how to fix this for `columnDefinition="json"` where I can't decide if it's json based on the type. Postgres understands the json fine, but in H2 the column creation will just fail. I want hibernate to map the column name to string in that case. – Tarion Nov 13 '15 at 13:56
  • did you ever solve this @Tarion? – Rhubarb Feb 25 '22 at 10:02
  • I don't think so. It's long time ago. – Tarion Mar 01 '22 at 14:49
4

Some ideas:

  • Use annotation in general, but overload them from Xml in the case where they are specific to your database. Then you can have one configuration file specific to your database.
  • Use java Constants in your annotations (they have to be compile-time constants, so you are limited). You can have several sets of Java constants, and point toward the one you want to export to. (Beware, when you point toward another constant, you have to recompile everything.)
  • I have also used the dialect to switch some code in my Configuration class. The configuration class receives all data (from annotations or xml), and can then postprocess it.

    For example, I have changed the concatenation symbol from '||' on Oracle to '+' on SqlServer.
    This is conveniently done at runtime :-)

KLE
  • 23,689
  • 4
  • 56
  • 62
  • Thanks - I already have the columnDefinition properties as constants for easier searching. Didn't know that annotations can be overloaded though - thanks! – user1946784 Dec 22 '09 at 07:29
  • Thanks, I'm currently finishing the migration to @Lob / @Column(length=90000) annotations which I prefer over modifying the hibernate Dialect. However, that would probably be my second option if the first one failed. – user1946784 Dec 24 '09 at 13:19