EnumType with hbm mapping in Hibernate 6

Hello,

I’m trying to prepare the migration of an application written with Hibernate 3 (currently using Hibernate 5.5) that has a lot of .hbm.xml mappings.

In order to map enums by name (instead of the default by ordinal) it is configured this way:

Example hbm.xml file:

<hibernate-mapping package="x.y.z">
  <typedef name="exampleEnumType" class="x.y.z.EnumUserType">
  	<param name="enumClass">x.y.z.ExampleEnum</param>
  </typedef>
...
  <property name="..." type="exampleEnumType"/>

And our custom EnumUserType overrides the parameters to force the use of NamedEnumValueConverter:

import java.util.Properties;
import org.hibernate.type.EnumType;

public class EnumUserType<T extends Enum<T>> extends EnumType<T> {

	@Override
	public void setParameterValues(Properties parameters) {
		parameters.setProperty(EnumType.NAMED, "true");

		super.setParameterValues(parameters);
	}
}

This works fine with Hibernate 5 but when moving to 6.0.0 Alpha 9 only one instance of that EnumUserType ever seems to be created by Hibernate, even though there are multiple typedef in multiple hbm files. That instance is configured multiple times and in the end keeps the last enumClass. Then when the type is used in a query the wrong enum is used and the data fails to load.
In debug I was able to confirm that the EnumUserType.setParameterValues() method is always called on the same instance.

Could you please advise if that’s an incorrect/unsupported way to map enums by name (using hbm) or if that’s a bug in the new type system?

This is probably a bug and we would be very happy if you could create a JIRA issue with a reproducer. Anyway, I would recommend you switch to an AttributeConverter if possible.

Thank you for the quick reply, I’ll try to make a minimal reproducer
I did not know about AttributeConverter and I’ll be looking into that too

Hello,

Jira issue and Pull request for reproducer created:
https://hibernate.atlassian.net/browse/HHH-14820

Thank you very much!

1 Like