仅与 JPA 进行 UUID 映射

问题描述 投票:0回答:1

我有一个像这样使用UUID的应用程序

@Entity
@Table(name = "query_indicator")
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public final class QueryIndicatorEntity {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  @NotNull
  @Type(type = "uuid-char")
  @Column(name = "uuid", length = 36, nullable = false, unique = true)
  private UUID uuid;
....
}

有没有办法用完整的 JPA 替换 @Type hibernate 注释,以便完全独立于所选的 ORM?

java jpa
1个回答
0
投票

我找到了解决办法。我已在数据库中将我的 uuid char 转换为二进制 16。 所以我只有这个映射

@NotNull
@Column(name = "uuid", updatable = false, nullable = false)
private UUID uuid;

不再需要 hibernate (5) 注解

@Type(type = "uuid-char")

这是一个示例 MySql Server 脚本,用于将列从字符迁移到二进制

ALTER TABLE sample MODIFY COLUMN uuid VARBINARY(36) NOT NULL;
UPDATE sample set uuid=UUID_TO_BIN(UUID());
ALTER TABLE sample MODIFY COLUMN uuid BINARY(16) NOT NULL;
© www.soinside.com 2019 - 2024. All rights reserved.