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.