2

How can i register a global available DynamicParameterizedType in hibernate?

I wrote the following type:

public class QuantityType extends AbstractSingleColumnStandardBasicType<Quantity<?>> implements DynamicParameterizedType {

    public static final QuantityType INSTANCE = new QuantityType();

    public QuantityType() {
        super(DoubleTypeDescriptor.INSTANCE, new QuantityJavaDescriptor(AbstractUnit.ONE));
    }

    @Override
    public String getName() {
        return QuantityType.class.getSimpleName();
    }

    @Override
    public void setParameterValues(Properties parameters) {
        ParameterType reader = (ParameterType) parameters.get(PARAMETER_TYPE);
        if (reader == null) throw new RuntimeException("Not Implemented");
        Unit<?> resolvedUnit = resolveUnit(reader);
        setJavaTypeDescriptor(new QuantityJavaDescriptor(resolvedUnit));
    }

    private Unit<?> resolveUnit(ParameterType reader) {...}
}

and registered it with a service registration in hibernate:

public class QuantityTypeRegistration implements TypeContributor {

    @Override
    public void contribute(TypeContributions typeContributions, ServiceRegistry serviceRegistry) {
        typeContributions.contributeType(QuantityType.INSTANCE);
    }
}

If i use the type in an entity, the wrap/unwrap method of the JavaTypeDescriptor gets called, but instead of the parameterized JavaTypeDescriptor, the default JavaTypeDescriptor gets called. For some reason the setParameterValues method was not called.

Code: https://github.com/raynigon/unit-api/tree/master/jpa-starter/src/main/java/com/raynigon/unit_api/jpa

Raynigon
  • 660
  • 1
  • 9
  • 21
  • Maybe [this](https://stackoverflow.com/questions/58681902/generic-enum-jpa-attributeconverter-implementation/58782773#58782773) answer will be useful. I guess you should not use registration via `TypeContributor` at all. – SternK Aug 24 '20 at 16:12
  • This would require a @Type Annotation for every usage, i would like to have the type as a out of the box feature. Best case would be to include the code as dependency, use the type and it works. – Raynigon Aug 24 '20 at 16:14
  • 1
    Seems like this doesnt work: https://hibernate.atlassian.net/browse/HHH-11110 – Raynigon Aug 25 '20 at 10:49

0 Answers0