简体   繁体   English

Hibernate 无法将 UUID 插入 VARCHAR 列

[英]Hibernate can't insert UUID to VARCHAR column

I am working on setting up a MySQL database and using Hibernate and Play Framework for the backend.我正在建立一个 MySQL 数据库,并为后端使用 Hibernate 和 Play Framework。 I am having problems with the IDs of entries.我遇到了条目 ID 的问题。 I defined my id column as VARCHAR(36) :我将我的 id 列定义为VARCHAR(36)

CREATE TABLE `logaritmical`.`users` (
   `id` VARCHAR(36) NOT NULL,
   `username` TEXT NOT NULL,
    `email` TEXT NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE INDEX `id_UNIQUE` (`id` ASC)
);

Now, the @Entity class is like this:现在,@Entity @Entity是这样的:

@Entity
@Table(name = "users")
public class UserDO {

   @Id
   @GeneratedValue(generator = "uuid2")
   @GenericGenerator(name = "uuid2", strategy = "uuid2")
   @Column(name = "id", updatable = false, nullable = false, columnDefinition = "VARCHAR(36)")
   private UUID id;
    
   @Column
   private String username;
    
   @Column
   private String email;
}

When doing the insert, I get the following error: Incorrect string value: '\xEF\xA5.\x89\xF3K...' for column 'id' at row 1执行插入时,出现以下错误: Incorrect string value: '\xEF\xA5.\x89\xF3K...' for column 'id' at row 1

If I change the column type and columnDefinition to BINARY(16) the insert works, but it has the disadvantage that the ID is not human-readable when doing selects.如果我将列类型和 columnDefinition 更改为BINARY(16)插入工作,但它的缺点是 ID 在执行选择时不可读。

Additional info: persistence.xml looks like this:附加信息: persistence.xml看起来像这样:

<persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">
   <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
   <non-jta-data-source>DefaultDS</non-jta-data-source>
   <properties>
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL8Dialect"/>
   </properties>
</persistence-unit>

DB Configuration and versions of libraries looks like this: jpa.default=defaultPersistenceUnit "org.hibernate" % "hibernate-entitymanager" % "5.4.24.Final", "mysql" % "mysql-connector-java" % "8.0.22",数据库配置和库版本如下所示: jpa.default=defaultPersistenceUnit "org.hibernate" % "hibernate-entitymanager" % "5.4.24.Final", "mysql" % "mysql-connector-java" % "8.0.22",

What can be done to have the UUID working with VARCHAR ?可以做些什么来让UUIDVARCHAR一起工作? Is there something I am missing?有什么我想念的吗?

Try to use the following definition:尝试使用以下定义:

import org.hibernate.annotations.Type;

@Entity
@Table(name = "users")
public class UserDO {
    @Id
    @GeneratedValue(generator = "uuid2")
    @GenericGenerator(name = "uuid2", strategy = "uuid2")
    @Column(name = "id", updatable = false, nullable = false, columnDefinition = "VARCHAR(36)")
    @Type(type = "uuid-char")
    private UUID id;

    // ...
}

It looks like that for your dialect UUID by default mapped to the uuid-binary basic type .默认情况下,您的方言UUID看起来像映射到uuid-binary 基本类型

PS Please note that saving UUID PK as a string can lead to the performance issue, as it explained in this article : PS 请注意,将UUID PK 保存为字符串可能会导致性能问题,如本文所述

Aside from the 9x cost in size (36 vs. 4 bytes for an int), strings don't sort as fast as numbers because they rely on collation rules.除了 9 倍的大小成本(36 对 int 的 4 个字节)之外,字符串的排序不如数字快,因为它们依赖于排序规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM