Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何添加自己的EnumValueMapperSupport_Java_Hibernate_Enums - Fatal编程技术网

Java 如何添加自己的EnumValueMapperSupport

Java 如何添加自己的EnumValueMapperSupport,java,hibernate,enums,Java,Hibernate,Enums,我试图在Hibernate中持久化一些enums,看起来我的两个内置支持选项是使用enum的名称,我不希望这样做,因为它是基于字符串的而不是基于int的,或者是枚举的序号,我宁愿不这样做,因为如果我以后在类的顶部添加一个枚举值,我会将所有内容分解成一行 相反,我有一个名为identification的接口,它将public int getId()作为其契约的一部分。这样,我想要持久化的枚举就可以实现Identifable,我知道它们将定义自己的id 但是,当我尝试扩展EnumValueMappe

我试图在Hibernate中持久化一些
enum
s,看起来我的两个内置支持选项是使用enum的名称,我不希望这样做,因为它是基于字符串的而不是基于int的,或者是枚举的序号,我宁愿不这样做,因为如果我以后在类的顶部添加一个枚举值,我会将所有内容分解成一行

相反,我有一个名为
identification
的接口,它将
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 {
      ...
    }

}
ExampleEnumType type = new ExampleEnumType();
ExampleMapper mapper = type.new ExampleMapper();
要创建此映射器的实例,您需要
枚举类型的实例

public class ExampleEnumType extends EnumType {

    public class ExampleMapper extends EnumValueMapperSupport {
      ...
    }

}
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();
    }
}

关于这个话题可能有一些问题。最近的一个是,一个高票的是:关于这个话题有很多问题。事实上,在过去的一周里,我读了很多。我在当前的代码库中使用了它们,试图接近我想要的东西。我没有看到任何允许我定义自己的EnumValueMapper的东西,因此对于未来的开发人员来说,这显然是一个enum,而不仅仅是任何旧的自定义类型。现在我可以(并且确实)使用
Enumerated(EnumType.STRING)
对不起,但是我把重点放在了你的第二部分(是否有另一种存储枚举的方法),因为对我来说,链接的问题似乎完美地回答了这一点。如果您有一个
可识别的
接口,那么编写一个
可识别的属性转换器
。如果我还是不明白的话:你能为你的观点添加一些例子吗?特别是对于它的使用,因为您不能同时为一个属性声明
Enum
identification
。现在来看第一部分:
EnumValueMapper
是静态的(但是私有的),您可以始终在
EnumType
的扩展中扩展
EnumValueMapperSupport
。如果EnumValueMapperSupport不是静态的,我如何扩展它?EnumValueMapper不是静态的,我现在正在查看hibernate源代码。我不希望编写可识别的属性转换器,因为有许多可识别的东西不是枚举,我不想留下这样的印象,即这只转换一些枚举。我目前正在使用5.0.2-final。我没有料到版本之间会有这种变化。但是,Hibernate 5也可以扩展
EnumValueMapperSupport
。@corsiKa有帮助吗?我觉得我现在的答案只是你需要的一半。我真的很想完成它,如果你给我一些提示。。。