Hibernate 5 > 6: DiscriminatorColumn of type Enum

33 Views Asked by At

In migration from hibernate 5.* to 6.* there seems to be an issue that I've not been able to navigate. In hibernate 5.* I have used enumerations for the discriminator-type. This worked very well until this migration effort.

The following scenario:

@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME,
    include = JsonTypeInfo.As.EXISTING_PROPERTY,
    property = "type"
)
@JsonSubTypes(
    JsonSubTypes.Type(value = GenericAction::class, name = "generic"),
    JsonSubTypes.Type(value = BreakAction::class, name = "break")
)
@Entity
@DiscriminatorColumn(name = "type")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
class Action (

    @Column(name = "type", updatable = false, insertable = false)
    @Enumerated(EnumType.STRING)
    var type: ActionType = ActionType.GENERIC, //GENERIC, BREAK
....
)
@Entity
@DiscriminatorValue("GENERIC")
class GenericAction (
... 
): Action(ActionType.GENERIC)
@Entity
@DiscriminatorValue("BREAK")
class BreakAction (
... 
): Action(ActionType.BREAK)

This worked. It would save either 'GENERIC' or 'BREAK' in the database and create the correct polymorphic sub-type when fetched.

In hibernate 6.* not anymore. The issue I have found is that it tries to compare a String with the ActionType (which obviously does not work).

Tried the following:

  1. @DiscriminatorFormula, this however fails whenever a .createNativeQuery is used.
  2. Not using an Enum, this however creates issues at our JSON serialiser/deserialiser level as those same @JsonProperty conversions cannot be done
  3. Tried @Converter on the type field
  4. Dug through the sourcecode to see if I could inject/override or manipulate that Enum/String comparison in any way.

When converting everything to strings, it works to store/fetch data, it however does not allow for the original json-conversion. No matter which way I look, the changes on already existing data would be massive.

0

There are 0 best solutions below