smeeb
smeeb

Reputation: 29577

Modeling UUID in Hibernate entity against MySQL

I have a very similar question to this one, however my question is even more basic than that one and so I don't feel like its a dupe...(we'll see what SO thinks). And if it is a dupe of another question, please point out to me (perhaps in a comment) how the accepted answer completely answers/addresses my issue (I don't think it does!).


I have a Java/JPA/Hibernate @Entity class that needs to have a UUID field:

@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
}

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 VARCHAR(36), so that's what I have.

At runtime I'm getting the following exception:

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)
  <rest of stack trace omitted for brevity>

So clearly, the database is presenting a VARCHAR(36), but Hibernate seems to be defaulting to expect a binary(255).

I honestly don't care what the UUID gets stored as in the DB. It could be VARCHAR, it could be TEXT, it could be BLOB...it could be a toaster oven for all I care! But the essential requirement here is that the data model (the entity class) use UUIDs.

What's the fix here? 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 UUID field in the entity, what else do I need to annotate it with?

Upvotes: 2

Views: 5829

Answers (2)

smeeb
smeeb

Reputation: 29577

For me the fix required changes to both DB schema as well as the entity (Java) code.

First I changed my CREATE TABLE statement to use BINARY(255) instead of VARCHAR(36):

CREATE TABLE IF NOT EXISTS accounts (
    # lots of fields...

    account_ref_id BINARY(255) NOT NULL,

    # ...lots of other fields
);

Next I added @Type(type="uuid-binary") to my field declaration in the entity:

@Canonical
@MappedSuperclass
public abstract class BaseEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Type(type="uuid-binary")
    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
}

This works beautifully for me.

Upvotes: 2

dsharew
dsharew

Reputation: 10675

Try it like this:

    public class BaseEntity{

    @Column(nullable = false)
    private String uuid;

    public BaseEntity(){
         setUuid(UUID.randomUUID().toString());
    }   

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

  @PrePersist
    public void prePersist(){

        if(null == getUuid())
             setUuid(UUID.randomUUID().toString());
 }

    ....

Column definition:

uuid varchar(255) DEFAULT NULL

When generating UUID use toString method:

entity.setUuid(UUID.randomUUID().toString())

Upvotes: 0

Related Questions