gpt4 book ai didi

oracle - Hibernate自定义类型映射设置字符长度

转载 作者:行者123 更新时间:2023-12-04 17:14:02 32 4
gpt4 key购买 nike

我目前正在 hibernate 中实现自定义类型映射(将 UUID 映射到 char 以便在 oracle 中设置索引)。我实现了 hibernate 类型 UUIDTypeUUIDJavaDescriptor 并通过 @TypeDef 将它们添加到我们的 baseEntity (JPAEntity)。问题是列生成为 varchar(255) 而不是 varchar(36)。通过 @Column(length = 36,/* ... */) 注释 UUID 字段按预期工作,但我不想注释项目中的所有 UUID 字段。有谁知道全局调整所有 UUID 字段长度的方法吗?

public class UUIDType extends AbstractSingleColumnStandardBasicType<UUID> {
public static final UUIDType INSTANCE = new UUIDType();
public static final String NAME = "uuid";
public static final Size SIZE = new Size(0, 0, 36, Size.LobMultiplier.NONE);
// define the column size ^^^

public UUIDType() {
super(VarcharTypeDescriptor.INSTANCE, UUIDJavaDescriptor.INSTANCE);
}

@Override
public String getName() {
return NAME;
}

// Override size related methods
@Override
protected Size getDictatedSize() {
return SIZE;
}

@Override
public Size[] dictatedSizes(Mapping mapping) throws MappingException {
return new Size[] { SIZE };
}

@Override
public Size[] defaultSizes(Mapping mapping) throws MappingException {
return new Size[] { SIZE };
}
}
public class UUIDJavaDescriptor extends AbstractTypeDescriptor<UUID> {
public static final UUIDJavaDescriptor INSTANCE = new UUIDJavaDescriptor();

public UUIDJavaDescriptor() {
super(UUID.class, ImmutableMutabilityPlan.INSTANCE);
}

@Override
public UUID fromString(String string) {
if (StringUtils.isEmpty(string)) return null;
return UUID.fromString(string);
}

@Override
public <X> X unwrap(UUID value, Class<X> type, WrapperOptions options) {
if (value == null) {
return null;
}

if (String.class.isAssignableFrom(type)) {
return (X) toString(value);
}

throw unknownUnwrap(type);
}

@Override
public <X> UUID wrap(X value, WrapperOptions options) {
if (value == null) {
return null;
}

if (String.class.isInstance(value)) {
return UUID.fromString((String) value);
}

throw unknownWrap(value.getClass());
}
}
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
@Getter
@Setter
@ToString(callSuper = true)
@MappedSuperclass
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@TypeDefs({
@TypeDef(typeClass = UUIDType.class, defaultForType = UUID.class, name = UUIDType.NAME)
})
public abstract class JPAEntity {
// ...
}

技术细节:

  • Apache Maven 3.6.3
  • openjdk 11.0.2
  • Hibernate-Core 5.4.30.Final

最佳答案

您不能更改它,因为值 255 来自 javax.persistence.Column 注释。 Hibernate 6.0 将正确处理此问题,但目前,您只需在所有位置设置长度即可。

关于oracle - Hibernate自定义类型映射设置字符长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69011429/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com