Hibernate支持的PostgreSQL UUID?
-
12-10-2019 - |
题
我无法与java.util.uuid合作进行postgresql。
这是使用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>
解决方案
可以通过将以下注释添加到UUID来解决:
import org.hibernate.annotations.Type;
...
@Type(type="pg-uuid")
private java.util.UUID itemUuid;
关于为什么Hibernate不仅将其作为默认设置,我无法告诉您...
更新:使用createNativeQuery方法打开具有UUID字段的对象似乎仍然存在问题。幸运的是,到目前为止的创建方法对我来说很好。
其他提示
您尝试持续存在UUID类型的对象,该对象不是冬眠通知的实体。因此,冬眠希望将其序列化为字节数组(BLOB类型)。这就是为什么您会收到此消息“ type bytea的表达”的原因。
您可以将UUID作为斑点存储在数据库中(不是优雅),也可以提供自定义序列化器(大量工作)或手动转换该对象。 UUID类具有来自String和ToString的方法,因此我将其存储为字符串。
正如其他人提到的那样,解决这个问题的方法是添加 @Type(type = "pg-uuid")
注解。但是,这种类型与其他供应商的UUID类型不兼容,因此这将您的Hibernate类与Postgres联系起来。为了解决此问题,可以在运行时插入此注释。以下适用于休眠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;
}
}
}
对于不使用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>