How to migrate the CompositeUserType with parameters to Hibernate 6

484 Views Asked by At

I am migrating a custom type that implements CompositeUserType and ParameterizedType interfaces. This custom type serves for mapping, among other things, two arrays of numbers: hardLevels and softLevels. The arrays lengths are passed as parameters via the @TydeDef annotation.

However, the @TypeDef was abandoned in Hibernate 6 and replaced by @CompositeType, which does not accept any parameters.

The mapping looks as follows in Hibernate 5:

    @Entity
    @TypeDef(defaultForType = BendableScore.class, typeClass = BendableScoreHibernateType.class, parameters = {
            @Parameter(name = "hardLevelsSize", value = "3"), @Parameter(name = "softLevelsSize", value = "2") })
    class BendableScoreHibernateTypeTestJpaEntity {

        @Columns(columns = { @Column(name = "initScore"),
                @Column(name = "hard0Score"), @Column(name = "hard1Score"), @Column(name = "hard2Score"),
                @Column(name = "soft0Score"), @Column(name = "soft1Score") })
        protected BendableScore score;

    ....

    }

I tried implementing the new CompositeUserType, which works fine for a different class without the aforementioned arrays, but I fail to see how to migrate this type with the array sizes. I haven't found any example nor test showing similarly complex CompositeUserType.

Another concern is backward compatibility: the data persisted using this custom type with Hibernate 5 need to be correctly restored with Hibernate 6.

1

There are 1 best solutions below

3
Radovan Synek On

One option to overcome this change the Hibernate team recommended to me is by using inheritance: implement the CompositeUserType as an abstract class and pass the parameters from its child classes via a constructor. Of course, it does not keep full backward compatibility as the annotation-based approach for setting parameters is no longer supported. On the other hand, it offers the same level of flexibility.