DoubleJdbcType#getDdlTypeCode -> Types.FLOAT?

Hi,

I have a problem with the double type conversion to DDL because the method Double JdbcType#getDdlTypeCode return Types.FLOAT.

It’s volontary? I think yes but i don’t undestrand.

Thank you.

image

My solution, override the DoubleJdbcType and DoubleJavaType :

public class EfluidDoubleJdbcType extends DoubleJdbcType {

  public static final EfluidDoubleJdbcType INSTANCE = new EfluidDoubleJdbcType();

  @Override
  public int getDdlTypeCode() {
    return Types.DOUBLE;
  }
}
public class EfluidDoubleJavaType extends DoubleJavaType {

  public static final EfluidDoubleJavaType INSTANCE = new EfluidDoubleJavaType();

  @Override
  public JdbcType getRecommendedJdbcType(JdbcTypeIndicators indicators) {
    return EfluidDoubleJdbcType.INSTANCE;
  }
}
public class EfluidTypeContributor implements TypeContributor {

  @Override
  public void contribute(TypeContributions typeContributions, ServiceRegistry serviceRegistry) {
    typeContributions.contributeJavaType(EfluidDoubleJavaType.INSTANCE);
    typeContributions.contributeJdbcType(EfluidDoubleJdbcType.INSTANCE);
  }
}

Since you don’t explain what the problem is that you have I can’t really help you. The way this is designed is on purpose.

Yes sorry.

My problem is i must get the format in database of a column for generate the DDL
And the java type double are number type and not float int the BDD.

It is a comparison that is expected :

public class MappingSQLType {

  public static String getSqlType(Column columnHibernate) {
    if (columnHibernate.getValue() instanceof SimpleValue value) {
      return columnHibernate.getSqlType(value.getMetadata());
    }
    return null;
  }
}

Well, if you want to map your Double fields in the database as number DDL type, then I would recommend you to do that on a per-field basis with @Column(columnDefinition). If you have many columns and want all of them to use the same mapping, you can register that in the DdlTypeRegisty, by calling addDescriptor( new DdlTypeImpl( SqlTypes.FLOAT, "number", dialect ) ) within e.g. a custom org.hibernate.boot.model.TypeContributor

1 Like

It’s a good idea, i will test that. Thank you !

It’s ok ! :smiley:
Thanks.

1 Like

The implementation:

public class EfluidTypeContributor implements TypeContributor {

  @Override
  public void contribute(TypeContributions typeContributions, ServiceRegistry serviceRegistry) {
    typeContributions.contributeJavaType(ListEnumTypeJava.INSTANCE);
    Dialect dialect = serviceRegistry.getService(JdbcEnvironment.class).getDialect();
    typeContributions.getTypeConfiguration().getDdlTypeRegistry().addDescriptor(new DdlTypeImpl(SqlTypes.FLOAT, "number", dialect ));
  }
}

Can you explain why you need a ListEnumTypeJava? Hibernate 6.2 properly supports stuff like @Basic List<MyEnum> already and maps that to e.g. tinyint array or smallint array on the DB if possible, and falls back to varbinary if the DB does not support arrays natively.

Sorry, it’s a enum custom from us framework, not the java enum.

public class ListEnum<T extends EnumerationType> implements Serializable, Cloneable, Iterable<T> {
1 Like