Assign a type to a dependency according to the type of the parent class

Hi,

I don’t know how to automatically assign a type to a dependency based on the type of the parent class?

Here ETypeEnvoiPostal inherits HermesNLSEnumType like many of our enumerated from our in-house framework.
But each time we have to add @Type (type = “HermesNLSEnumType”) on the variable to indicate the type.

Is there a more automatic way?

thanks a lot

@TypeDefs({
    @TypeDef(name = "HermesNLSEnumType", typeClass = HermesEnumTypeHibernate.class),
})
public class HermesEnumTypeHibernate extends AbstractSingleColumnStandardBasicType<HermesNLSEnumType> implements DynamicParameterizedType {

  public HermesEnumTypeHibernate() {
    super(IntegerTypeDescriptor.INSTANCE, new HermesEnumTypeJavaDescriptor(HermesNLSEnumType.class));
  }

  @Override
  public String getName() {
    return "HermesEnum";
  }

  @Override
  public void setParameterValues(Properties parameters) {
    final ParameterType reader = (ParameterType) parameters.get(PARAMETER_TYPE);
    if (reader != null) {
      setJavaTypeDescriptor(new HermesEnumTypeJavaDescriptor(reader.getReturnedClass()));
    }
  }
}
public class Document  {
  @Type(type = "HermesNLSEnumType")
  ETypeEnvoiPostal typeEnvoiPostal;
}
public class ETypeEnvoiPostal extends HermesNLSEnumType {
}

public class EType2 extends HermesNLSEnumType {
public class EType3 extends HermesNLSEnumType {
...
}

I would suggest you replace these enum types with JPA AttributeConverter implementations which also support auto-apply for the type and will reduce the necessary effort for upgrading to Hibernate 6.0, as that will change the UserType SPI.

I started with the AttributeConverter solution but in the same way, I couldn’t match the converter with the type of the parent class for autoapply.

And maybe, too, I couldn’t convey the girl class type to her. Here I am using DynamicParameterizedType to retrieve the type of the child class.

Possible in your opinion?

I would also prefer to use a Converter! ^^
I will try again.

I don’t know what you mean by “match the converter with the type of the parent class”. If you have further questions, please be a bit more specific and maybe show examples of what you want to achieve or what the problem is with the converter solution.

Sorry,

I have 2 problems:

1: That the autoApply works for all classes that inherit from HermesNLSEnumType (I’m only creating one converter, because I don’t want to create one converter per type : EType1Converteur, EType2Converter, etc.)

2: In the single converter I would like to recover the type of the class: EType1 / EType2 / EType3 where I put ???

public class EType1 extends HermesNLSEnumType {}
public class EType2 extends HermesNLSEnumType {}
public class EType3 extends HermesNLSEnumType {}
@Converter(autoApply = true)
public class HermesNLSEnumTypeConverter implements AttributeConverter<HermesNLSEnumType , Integer> {
  private static final ConvertEnum CONVERT_ENUM = new ConvertEnum();
  @Override
  public Integer convertToDatabaseColumn(HermesNLSEnumType  attribute) {
    return attribute != null ? attribute.getValue() : null;
  }
  @Override
  public T convertToEntityAttribute(Integer dbData) {
    Class typeClass = ???
    return dbData != null ? CONVERT_ENUM.getJavaValue(typeClass, dbData) : null;
  }
}

Well, that’s not possible. You will just have to create a converter per type.

That’s what it seemed to me. Thank you.

I allow myself to raise the problem because with hibernate 6.0 the converter is well applied if the type of the converted attribute corresponds to the parent class.

My problem is that there is no context attached to the converted attribute at all.

Because I would need to know the actual type to instantiate the right type.

So I was wondering if it would not be possible to create an instance of InstanceBasedConverterDescriptor instead of ClassBasedConverterDescriptor when creating the converterDescriptor in some cases like with DynamicParameterizedType interface

a bit like here :

At the same time I’m going to see if I can’t use a custom type rather than a converter to precisely exploit this dynamic side (with context)

Thank you.

here we can see that it retrieves the type descriptor via a type and not a parent class

and the creator only handles parameterized types or arrays

and the RegistryHelper ended up creating a SerializableJavaType

All that to say that I feel like assigning a Java type using its parent class doesn’t seem to work.

Only solution I see is to use the @Type annotation to apply the custom type to it

If you need to know the parameterized type, implementing UserType and DynamicParameterizedType is your only option.

And it will also automatically assign it to classes that inherit with type register?
Or will it be necessary to use the explicitCustomType with @Type?

You can either use @TypeRegistration for which you can specify the Java class under which the custom type should be registered, or you can use @Type on every persistent attribute.

Thank you for your response.

For the @TypeRegistration I only find for JavaType and not for UserType.

Also the problem with @JavaTypeRegistration is that it does not accept an instance but only a class and DynamicParameterizedType does not work a priori with javaType

image

For the @TypeRegistration I only find for JavaType and not for UserType.

You’ll have to update to 6.2.0.CR3

1 Like