title: Modeling UUID in Hibernate entity against MySQL
body: <p>I have a <em>very</em> similar question to <a href="https://stackoverflow.com/q/6522420/4009451">this one</a>, however my question is even more basic than that one and so I don't <em>feel</em> like its a dupe...(we'll see what SO thinks). <strong>And</strong> if it is a dupe of another question, please point out to me (perhaps in a comment) how the accepted answer <em>completely</em> answers/addresses my issue (I don't think it does!).</p>
  
  <hr>
  
  <p>I have a Java/JPA/Hibernate <code>@Entity</code> class that needs to have a <code>UUID</code> field:</p>
  
  <pre><code>@Canonical
  @MappedSuperclass
  public abstract class BaseEntity {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      private UUID refId;
  
      // Getters/setters/ctors/etc.
  }
  
  @Entity(name = "accounts")
  @AttributeOverrides({
      @AttributeOverride(name = "id", column=@Column(name="account_id")),
      @AttributeOverride(name = "refId", column=@Column(name="account_ref_id"))
  })
  public class Account extends BaseEntity {
      // blah whatever
  }
  </code></pre>
  
  <p>That is, I need to be working with UUIDs at the app layer. However the database here is MySQL, and the overwhelming recommendations for representing UUIDs in MySQL seems to be to represent them as a <code>VARCHAR(36)</code>, so that's what I have.</p>
  
  <p>At runtime I'm getting the following exception:</p>
  
  <pre><code>Caused by: org.hibernate.HibernateException: Wrong column type in my_db.accounts for column account_ref_id. Found: varchar, expected: binary(255)
      at org.hibernate.mapping.Table.validateColumns(Table.java:373)
      at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1338)
    &lt;rest of stack trace omitted for brevity&gt;
  </code></pre>
  
  <p>So <em>clearly</em>, the database is presenting a <code>VARCHAR(36)</code>, but Hibernate seems to be defaulting to expect a <code>binary(255)</code>.</p>
  
  <p>I honestly don't care what the UUID gets stored as in the DB. It could be <code>VARCHAR</code>, it could be <code>TEXT</code>, it could be <code>BLOB</code>...it could be a toaster oven for all I care! But the essential requirement here is that the data model (the entity class) use <code>UUID</code>s.</p>
  
  <p><strong>What's the fix here?</strong> If I have to change the column type in the DB, what do I change the type to? If I have to change/modify the <code>UUID</code> field in the entity, what else do I need to annotate it with?</p>