0

I'm trying to persist some enums in Hibernate and it looks like my two options for built in support are to use the name of the enum, which I would rather not do because it's string based instead of int based, or the ordinal of the enum, which I would rather not do because if I add one of the enum values at the top of the class later on, I break everything down the line.

Instead, I have an interface called Identifiable that has public int getId() as part of its contract. This way, the enums I want to persist can implement Identifable and I can know that they'll define their own id.

But when I try to extend EnumValueMapperSupport so I can utilize this functionality, I'm greeted with errors from the compiler because the EnumValueMapper interface and the EnumValueMapperSupport class are not static, and thus are expected to be locked into a given EnumType object.

How can I extend this functionality in Hibernate, short of rewriting a bunch of Hibernate code and submitting a patch. If I can't, is there another way to somehow store an enum based on something other than the ordinal or name, but instead on your own code?

In a related thought, has anyone personally been down this road and decided "let's see how bad the name mapping is" and just went with name mapping because it wasn't that much worse performance? Like, is it possible I'm prematurely optimizing here?

I'm working against Hibernate version 5.0.2-final.

corsiKa
  • 81,495
  • 25
  • 153
  • 204
  • There are may questions about this topic. A recently one is [String Enum on Hibernate](http://stackoverflow.com/questions/33895184/string-enum-on-hibernate), a high voted one is: [Is it possible to write a generic enum converter for JPA](http://stackoverflow.com/questions/23564506) – Tobias Liefke Nov 28 '15 at 18:45
  • There are many questions about this topic. I've read many of them in the past week, in fact. I have used them in my current codebase to try to get close to what I want. What I don't see is any that allow me to define my own EnumValueMapper so it is painfully obvious to future developers that this is an enum instead of just any old custom type. Where right now I can (and do) use `Enumerated(EnumType.STRING)` – corsiKa Nov 28 '15 at 18:55
  • Sorry, but I focused on your second part (_is there another way to somehow store an enum_), because for me that seems to be perfectly answered by the linked questions. If you have an `Identifiable` interface, then write an `IdentifiableAttributeConverter`. If I still don't get the point: Could you add some example for your Identifiable? Especially for its usage, as you can't declare `Enum` and `Identifiable` for one property at the same time. Now to the first part: `EnumValueMapper` _is_ static (but private) and you can always extend `EnumValueMapperSupport` in an extension of `EnumType`. – Tobias Liefke Nov 28 '15 at 19:10
  • How can I extend EnumValueMapperSupport if it isn't static? And EnumValueMapper is not static, I'm looking at the hibernate source right now. – corsiKa Nov 28 '15 at 19:20
  • And I would prefer not to write an Identifiable attribute converter because there are many things that are Identifiable that are not enums, and I would not want to leave the impression that this only converts SOME enums. – corsiKa Nov 28 '15 at 19:20
  • I had to add a part of my response as answer below, to show you what I mean. Now I would appreciate if you provide an example, that I can see what you mean. – Tobias Liefke Nov 28 '15 at 19:41

1 Answers1

0

At least for Hibernate 4.3.5 the EnumValueMapper is static - although private.

But you can extend EnumValueMapperSupport in an extension of EnumType:

public class ExampleEnumType extends EnumType {

    public class ExampleMapper extends EnumValueMapperSupport {
      ...
    }

}

To create an instance of this mapper you need an instance of your EnumType:

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

Or you create it inside your type:

public class ExampleEnumType extends EnumType {

    public class ExampleMapper extends EnumValueMapperSupport {
        ...
    }

    public ExampleMapper createMapper() {
        return new ExampleMapper();
    }
}
Tobias Liefke
  • 8,637
  • 2
  • 41
  • 58
  • I'm currently working against 5.0.2-final. I wouldn't have expected this kinda of shift between versions. – corsiKa Nov 28 '15 at 22:03
  • But the extension of `EnumValueMapperSupport` is possible for Hibernate 5 as well. – Tobias Liefke Nov 28 '15 at 23:42
  • @corsiKa Did it help? I've got the feeling that my current answer is only half of what you need. And I would really like to complete it, if you give me some hints... – Tobias Liefke Nov 30 '15 at 20:27