1

Using JPA/Hibernate, defining an entity with the following field:

@Id
@Column(length = 36)
private String id = UUID.randomUUID().toString();

Using Liquibase, knowing that this is a UUID, having the following in the change log:

<column name="id" type="uuid">
...

We like to use Hibernate's hbm2ddl.auto=validate/ddl-auto=validate on startup. Now, for MySQL, Hibernate is going to think this should be VARCHAR, while we know the length is fixed, CHAR, which Liquibase can derive from type="uuid" (in fact, Liquibase defines this in its UUIDType). So the Hibernate validation on startup is going to balk about the type difference between VARCHAR and CHAR.

How to fix this problem in a canonical way? Canonical means for me:

  • Don't compromise on type="uuid" in the Liquibase change log
  • Don't use @Column(..., columnDefinition="CHAR") since that's not strictly portable

(I'm currently working around this by having my own override of Liquibase's UUIDType and make the column a VARCHAR, but CHAR seems better, strictly speaking, anyway. And this seems like an overkill solution.)

Clearly, this is not an immediate problem (as I have the workaround), but I'd like to learn the right way to do this.

Sander Verhagen
  • 8,540
  • 4
  • 41
  • 63
  • *that's not strictly portable*: how is it not? Are there databases that don't have a char type? Do you really think you'll ever use another database? And if you do, do you really think that will be your main concern? – JB Nizet Jul 15 '17 at 06:40
  • We're dealing with on-premises customers, that can chose between MySQL and SQL Server. Once we have this working with MySQL, SQL Server _is_ up next. I believe SQL Server calls its `CHAR`/`VARCHAR` types differently, tho, me no expert. – Sander Verhagen Jul 15 '17 at 06:51
  • Also, like I already suggested in my question, the "problem" can be worked around. But sometimes it's also nice to figure something out to its root, and do things "correctly". Thanks for looking into this. – Sander Verhagen Jul 15 '17 at 06:59
  • 1
    https://learn.microsoft.com/en-us/sql/t-sql/data-types/char-and-varchar-transact-sql seems to indicate that char is a valid sql server type. – JB Nizet Jul 15 '17 at 07:12

0 Answers0