How to convert Hibernate String columns to Enum?

I currently have a simple string field in a persistent Hibernate class:

 private String font = "Times New Roman";

"Times New Roman" is the default, so many stored objects have this data. For several reasons, I would like to convert this to Enum:

 @Enumerated(EnumType.String)
 private FontEnum font = FontEnum.TIMES;

Hibernate uses Enum.valueOf (clazz, string) to convert column data that will not be executed in Times New Roman because of spaces.

Googling suggests overriding Enum.valueOf () is impossible; do others know differently? Can I trick Enum.valueOf () to convert "Times New Roman" to FontEnum.TIMES?

Or, can I create my own Hibernate converter, for example:

 public FontEnum legacyConverter(String old);

and add

 @Enumerated(EnumType.String)
 @Converter(Converters.legacyConverter()) // Does this exist?
 private FontEnum font = FontEnum.TIMES;

to my fields? I have not seen anything like it.

, , , , .

- StackOverflow!

+3
1
+2

All Articles