在不同情况下使用不同的 Hibernate 用户类型 [英] Using Different Hibernate User Types in Different Situations

查看:26
本文介绍了在不同情况下使用不同的 Hibernate 用户类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Hibernate + JPA 作为我的 ORM 解决方案.

I am using Hibernate + JPA as my ORM solution.

我使用 HSQL 进行单元测试,使用 PostgreSQL 作为真正的数据库.

I am using HSQL for unit testing and PostgreSQL as the real database.

我希望能够使用 Postgres 的原生 UUID使用 Hibernate 键入,并在 HSQL 的字符串表示中使用 UUID 进行单元测试(因为 HSQL 没有 UUID 类型).

I want to be able to use Postgres's native UUID type with Hibernate, and use the UUID in its String representation with HSQL for unit testing (since HSQL does not have a UUID type).

我正在使用具有不同配置的持久性 XML 进行 Postgres 和 HSQL 单元测试.

I am using a persistence XML with different configurations for Postgres and HSQL Unit Testing.

这是我如何让 Hibernate查看"我的自定义 UserType:

Here is how I have Hibernate "see" my custom UserType:

@Id
@Column(name="UUID", length=36)
@org.hibernate.annotations.Type(type="com.xxx.UUIDStringType")
public UUID getUUID() {
    return uuid;
}


public void setUUID(UUID uuid) {
    this.uuid = uuid;
}

而且效果很好.但我需要的是能够在 XML 中或从无需重新编译即可更改的属性文件中换出注释的com.xxx.UUIDStringType"部分.

and that works great. But what I need is the ability to swap out the "com.xxx.UUIDStringType" part of the annotation in XML or from a properties file that can be changed without re-compiling.

有什么想法吗?

推荐答案

这个问题真的很老了,已经回答了很久了,但最近发现自己也遇到了同样的情况,找到了一个很好的解决方案.首先,我发现 Hibernate 具有三种不同的内置 UUID 类型实现:

This question is really old and has been answered for a long time, but I recently found myself in this same situation and found a good solution. For starters, I discovered that Hibernate has three different built-in UUID type implementations:

  1. binary-uuid : 将 UUID 存储为二进制
  2. uuid-char :将 UUID 存储为字符序列
  3. pg-uuid :使用原生 Postgres UUID 类型
  1. binary-uuid : stores the UUID as binary
  2. uuid-char : stores the UUID as a character sequence
  3. pg-uuid : uses the native Postgres UUID type

这些类型是默认注册的,可以使用 @Type 注释为给定字段指定,例如

These types are registered by default and can be specified for a given field with a @Type annotation, e.g.

@Column
@Type(type = "pg-uuid")
private UUID myUuidField;

还有一种覆盖方言中默认类型的机制.因此,如果最终部署是与 Postgres 数据库对话,但单元测试使用 HSQL,您可以重写 pg-uuid 类型以通过编写自定义方言来读取/写入字符数据,如下所示:

There's also a mechanism for overriding default types in the Dialect. So if the final deployment is to talk to a Postgres database, but the unit tests use HSQL, you can override the pg-uuid type to read/write character data by writing a custom dialect like so:

public class CustomHSQLDialect extends HSQLDialect {

    public CustomHSQLDialect() {
        super();

        // overrides the default implementation of "pg-uuid" to replace it
        // with varchar-based storage.
        addTypeOverride(new UUIDCharType() {
            @Override
            public String getName() {
                return "pg-uuid";
            }
        });
    }
}

现在只需插入自定义方言,pg-uuid 类型在两种环境中都可用.

Now just plug in the custom dialect, and the the pg-uuid type is available in both environments.

这篇关于在不同情况下使用不同的 Hibernate 用户类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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