繁体   English   中英

如何添加自己的EnumValueMapperSupport

[英]How can I add my own EnumValueMapperSupport

我试图在Hibernate中保留一些enum ,并且看起来我的两个内置支持选项是使用enum的名称,我宁愿不这样做,因为它是基于字符串的,而不是基于int的或枚举,我不愿意这样做,因为如果稍后在类的顶部添加枚举值之一,则会将所有内容分解。

相反,我有一个名为Identifiable的接口,该接口的合同中包含public int getId() 这样,我要保留的枚举可以实现Identifable并且我知道它们将定义自己的ID。

但是,当我尝试扩展EnumValueMapperSupport以便可以利用此功能时,由于EnumValueMapper接口和EnumValueMapperSupport类不是静态的,因此希望将其锁定在给定的EnumType对象中,因此会遇到来自编译器的错误。

我如何在Hibernate中扩展此功能,而无需重写一堆Hibernate代码并提交补丁。 如果我做不到,是否有另一种方式可以基于序数或名称之外的其他方式而不是根据您自己的代码存储枚举?

在一个相关的思想中,是否有人亲自决定“让我们看看名称映射有多糟糕”,并选择名称映射,因为它没有那么糟糕的性能? 就像,我可能在这里过早优化吗?

我正在使用Hibernate 5.0.2-final版本。

至少对于Hibernate 4.3.5, EnumValueMapper 静态的-尽管是私有的。

但是您可以在EnumType的扩展中扩展EnumValueMapperSupport

public class ExampleEnumType extends EnumType {

    public class ExampleMapper extends EnumValueMapperSupport {
      ...
    }

}

要创建此映射器的实例,您EnumType的实例:

ExampleEnumType type = new ExampleEnumType();
ExampleMapper mapper = type.new ExampleMapper();

或者在您的类型内创建它:

public class ExampleEnumType extends EnumType {

    public class ExampleMapper extends EnumValueMapperSupport {
        ...
    }

    public ExampleMapper createMapper() {
        return new ExampleMapper();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM