在Hibernate实体中针对MySQL建模UUID [英] Modeling UUID in Hibernate entity against MySQL

查看:92
本文介绍了在Hibernate实体中针对MySQL建模UUID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常这个问题类似的问题,但是我的问题甚至更多比那个基本的更重要,所以我不喜欢它的双重感觉...(我们会看到SO的想法). 并且如果是其他问题的虚假记载,请向我指出(也许在评论中)接受的答案如何完全回答/解决我的问题(我不会认为确实如此!).

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!).

我有一个Java/JPA/Hibernate @Entity类,需要一个UUID字段:

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
}

也就是说,我需要在应用程序层使用UUID.但是这里的数据库是MySQL,关于在MySQL中表示UUID的压倒性建议似乎是将它们表示为VARCHAR(36),这就是我所拥有的.

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>

所以显然,数据库显示的是VARCHAR(36),但是Hibernate似乎默认情况下期望的是binary(255).

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

老实说,我不在乎UUID在数据库中存储了什么.可能是VARCHAR,可能是TEXT,可能是BLOB ...就我所知,它可能是烤面包机!但是这里的基本要求是数据模型(实体类)使用UUID s.

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.

这里的解决方法是什么?如果我必须更改数据库中的列类型,该将类型更改为什么?如果必须更改/修改实体中的UUID字段,还需要用什么其他注释呢?

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?

推荐答案

对我而言,此修复程序需要更改数据库模式以及实体(Java)代码.

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

首先,我将CREATE TABLE语句更改为使用BINARY(255)而不是VARCHAR(36):

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
);

接下来,我在实体的字段声明中添加了@Type(type="uuid-binary"):

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
}

这对我来说效果很好.

这篇关于在Hibernate实体中针对MySQL建模UUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆