将AttributeConverter返回的UUID保存为pg-uuid

时间:2017-09-18 20:05:03

标签: java postgresql hibernate jpa uuid

我有一些 CustomUuid 类,它以自定义格式存储 java.util.UUID 。但我需要将其保存为pg-uuid,并在DAO层之外的任何地方都有自定义格式。我尝试使用javax.persistence.AttributeConverter,但我得到了

Caused by: org.postgresql.util.PSQLException: ERROR: column "custom_uuid" is of type uuid but expression is of type bytea
  Hint: You will need to rewrite or cast the expression.
  Position: 223

据我所知,UUID不会自动转换为pg-uuid(或其他嵌入式),为此,我需要添加注释@Type,但这个注释不适用于@Converter。附上一些代码。

我将非常感谢任何帮助!

@Convert(converter = CustomUuidConverter.class)
@Column(nullable = false)
private CustomUuid customUuid;

CustomUuidConverter.java

@Converter(autoApply = true)
public class CustomUuidConverter implements AttributeConverter<CustomUuid, UUID> {

    @Override
    public UUID convertToDatabaseColumn(CustomUuid uuid) {
        return uuid == null ? null : uuid.toJavaUuid();
    }

    @Override
    public CustomUuid convertToEntityAttribute(UUID uuid) {
        return uuid == null ? null : new CustomUuid(uuid);
    }
}

作为DB,我使用postgresql(和liquibase来创建表)

0 个答案:

没有答案