Varchar(1) column for java enum fails with ClassCastException

When upgrading from Hibernate 5 to Hibernate 6, the following column/java type causes failures:

public enum Mode { A, S, R };

@Enumerated(EnumType.STRING)
@Column(length = 1)
@JdbcType(VarcharJdbcType.class)
Mode mode;

The column type in the DB is a varchar(1). In Hibernate 5, it considered this column a String and successfully converts to a Mode enum without any intervention. In Hibernate 6, it considers the column a java.lang.Character and thus cannot convert it to a Mode. I’ve tried using an AttributeConverter<Mode, String> but it doesn’t seem to work in all cases. Especially when the column is used in a composite key.

The only fix I’ve found is to change the db column to a varchar(4) and the @Column to a length =4. It’s not ideal but it works.

I’m currently using 6.2.2.Final

This is very similar to what was discussed in this post. A JIRA issue was opened for this [HHH-16498] - Hibernate JIRA, you can follow that for any updates.

Would you mind creating a reproducer for this and create a new JIRA issue please? The issue you are describing seems to be a different one than HHH-16498.

I’ll try to when I get time.

@beikov i think the issue is definately different than the one in HHH-16498.
Since we hit the problem in one of our projects, I’ve created a reproducer on Github using Hibernate 6.2.7.Final: GitHub - Guschtel/enumconverter

What i found out so far is:

  • When setting the column length to 1 instead of using varchar/string, char/Character is used (that’s what i expect).
  • During the commit of a transaction with such an entity, an exception will occur instead of saving the entity
  • Even overriding the JdbcType / JavaType with a custom type that provides the correct wrapping / unwrapping with String will not solve the problem, since then in org.hibernate.type.descriptor.converter.internal.NamedEnumValueConverter#toDomainValue it will hit a classcast Exception because Character cannot be cast to String.
  • org.hibernate.type.descriptor.java.EnumJavaType#fromName also only accepts a String

Shall I open a bug report for this?

If you need anything else, feel free to ask, i’ll try to help if i can.

Created the issue [HHH-17106] - Hibernate JIRA

1 Like