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.
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 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.
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;
}
}
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
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.
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