20

我无法让 Hibernate 使用 PostgreSQL 的 java.util.UUID。

这是使用 javax.persistence.* 注释的映射:

private UUID itemUuid;

@Column(name="item_uuid",columnDefinition="uuid NOT NULL")
public UUID getItemUuid() {
    return itemUuid;
}

public void setItemUuid(UUID itemUuid) {
    this.itemUuid = itemUuid;
}

持久化瞬态对象时,我得到一个 SQLGrammarException:

column "item_uuid" is of type uuid but expression is of type bytea at character 149

PostgreSQL 版本是 8.4.4
JDBC 驱动 - 8.4.4-702(也试过 9.0 - 同样的事情)
Hibernate 版本是 3.6,主要配置属性:

<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://192.168.1.1/db_test</property>
4

6 回答 6

40

这可以通过向 UUID 添加以下注释来解决:

import org.hibernate.annotations.Type;
...
@Type(type="pg-uuid")
private java.util.UUID itemUuid;

至于为什么 Hibernate 不只是将其设为默认设置,我无法告诉你......

更新:使用 createNativeQuery 方法打开具有 UUID 字段的对象似乎仍然存在问题。幸运的是,到目前为止,createQuery 方法对我来说效果很好。

于 2011-09-20T01:04:20.577 回答
7

现在您还可以使用 java.util.UUID 提供的 UUID 类,该类通过 Hibernate 映射到 Postgres 的 uuid 数据类型,而无需在从数据库读取/写入时进行任何转换。

  @Id
  @GeneratedValue
  private UUID id;

默认情况下,生成的值是自动的,这让您的 JVM 可以定义 UUID。这也允许 hibernate 使用批量插入优化。

您可以配置数据库以设置 UUID 值。更多信息可以在这里找到https://vladmihalcea.com/uuid-identifier-jpa-hibernate/

于 2019-11-18T11:12:45.577 回答
6

您尝试保留 UUID 类型的对象,它不是休眠注释实体。所以hibernate想把它序列化为字节数组(blob类型)。这就是为什么您会收到此消息“bytea 类型的表达式”。

您可以将 UUID 作为 blob 存储在数据库中(不优雅),或者提供自定义序列化程序(大量工作)或手动转换该对象。UUID 类有 fromString 和 toString 方法,所以我会将它存储为 String。

于 2010-12-21T13:23:05.853 回答
3

正如其他人提到的,此问题的解决方案是添加@Type(type = "pg-uuid")注释。但是,这种类型与其他供应商的 UUID 类型不兼容,因此这会将您的 Hibernate 类与 Postgres 联系起来。要解决此问题,可以在运行时插入此注释。以下适用于 Hibernate 4.3.7。

首先,您需要插入自定义元数据提供程序来插入注释。创建类的实例后,第一步是这样做Configuration

// Perform some test to verify that the current database is Postgres.
if (connectionString.startsWith("jdbc:postgresql:")) {
    // Replace the metadata provider with our custom metadata provider.
    MetadataProviderInjector reflectionManager = MetadataProviderInjector)cfg.getReflectionManager();
    reflectionManager.setMetadataProvider(new UUIDTypeInsertingMetadataProvider(reflectionManager.getMetadataProvider()));
}

此自定义元数据提供程序查找类型为 的字段和方法UUID。如果找到,它会插入一个org.hibernate.annotations.Type注释实例,说明类型应该是"pg-uuid"

package nl.gmt.data;

import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.common.reflection.AnnotationReader;
import org.hibernate.annotations.common.reflection.MetadataProvider;

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

class UUIDTypeInsertingMetadataProvider implements MetadataProvider {
    private final Map<AnnotatedElement, AnnotationReader> cache = new HashMap<>();
    private final MetadataProvider delegate;

    public UUIDTypeInsertingMetadataProvider(MetadataProvider delegate) {
        this.delegate = delegate;
    }

    @Override
    public Map<Object, Object> getDefaults() {
        return delegate.getDefaults();
    }

    @Override
    public AnnotationReader getAnnotationReader(AnnotatedElement annotatedElement) {
        // This method is called a lot of times on the same element, so annotation
        // readers are cached. We only cache our readers because the provider
        // we delegate to also caches them.

        AnnotationReader reader = cache.get(annotatedElement);
        if (reader != null) {
            return reader;
        }

        reader = delegate.getAnnotationReader(annotatedElement);

        // If this element is a method that returns a UUID, or a field of type UUID,
        // wrap the returned reader in a new reader that inserts the "pg-uuid" Type
        // annotation.

        boolean isUuid = false;
        if (annotatedElement instanceof Method) {
            isUuid = ((Method)annotatedElement).getReturnType() == UUID.class;
        } else if (annotatedElement instanceof Field) {
            isUuid = ((Field)annotatedElement).getType() == UUID.class;
        }

        if (isUuid) {
            reader = new UUIDTypeInserter(reader);
            cache.put(annotatedElement, reader);
        }

        return reader;
    }

    private static class UUIDTypeInserter implements AnnotationReader {
        private static final Type INSTANCE = new Type() {
            @Override
            public Class<? extends Annotation> annotationType() {
                return Type.class;
            }

            @Override
            public String type() {
                return "pg-uuid";
            }

            @Override
            public Parameter[] parameters() {
                return new Parameter[0];
            }
        };

        private final AnnotationReader delegate;

        public UUIDTypeInserter(AnnotationReader delegate) {
            this.delegate = delegate;
        }

        @Override
        @SuppressWarnings("unchecked")
        public <T extends Annotation> T getAnnotation(Class<T> annotationType) {
            if (annotationType == Type.class) {
                return (T)INSTANCE;
            }

            return delegate.getAnnotation(annotationType);
        }

        @Override
        public <T extends Annotation> boolean isAnnotationPresent(Class<T> annotationType) {
            return annotationType == Type.class || delegate.isAnnotationPresent(annotationType);
        }

        @Override
        public Annotation[] getAnnotations() {
            Annotation[] annotations = delegate.getAnnotations();
            Annotation[] result = Arrays.copyOf(annotations, annotations.length + 1);
            result[result.length - 1] = INSTANCE;
            return result;
        }
    }
}
于 2015-06-25T09:35:52.587 回答
0

不使用 JPA 的人的解决方案。

前:

<property name="testId" >
        <column name="test_id"  sql-type="uuid"  not-null="true"/>
</property>

后:

<property name="testId" column="test_id" type="org.hibernate.type.PostgresUUIDType">
</property>
于 2014-10-09T08:09:18.490 回答
0

在 postgres 中使用 Sprint Data 和 jsonb 时,我遇到了一个类似的问题。谢谢斯里解决方案!

在模型中,替换

@Type(type="pg-uuid")

@Type(type="org.hibernate.type.PostgresUUIDType")

解决了使用 @SpringBootTest 运行 JUnit 测试的问题。

实体(Kotlin)中的示例:

@Type(type="org.hibernate.type.PostgresUUIDType")
@Column(
    nullable = false,
    unique = true,
    updatable = false,
    columnDefinition = "CHAR(36)"
)
var uuid: UUID = UUID.randomUUID()
于 2021-09-10T11:15:45.343 回答