enumseclipselinkconvertersbigdecimalcolumn-types

EclipseLink converts Enum to BigDecimal


I try to convert an Enum into a BigDecimal using the Converter of EclipseLink. The conversion works, but the resulting database column has a type of String. Is it possible to set a parameter, that EclipseLink builds a decimal column type within the database?

I use a class, which implements org.eclipse.persistence.mappings.converters.Converter.

The application server logs

The default table generator could not locate or convert a java type (null) into a database type for database field (xyz). The generator uses java.lang.String as default java type for the field.

This message is generated for every field, which uses a converter. How can I define a specific database type for these fields?

public enum IndirectCosts {

EXTENDED {
    public BigDecimal getPercent() {
        return new BigDecimal("25.0");
    }
},
NORMAL {
    public BigDecimal getPercent() {
        return new BigDecimal("12.0");
    }
},
NONE {
    public BigDecimal getPercent() {
        return new BigDecimal("0.0");
    }
};

public abstract BigDecimal getPercent();

public static IndirectCosts getType(BigDecimal percent) {
    for (IndirectCosts v : IndirectCosts.values()) {
        if (v.getPercent().compareTo(percent) == 0) {
            return v;
        }
    }
    throw new IllegalArgumentException();
}
}

The database has to store the numeric values. I use such a converter:

public class IndirectCostsConverter implements Converter {

@Override
public Object convertObjectValueToDataValue(Object objectValue, Session session) {
    if (objectValue == null) {
        return objectValue;
    } else if (objectValue instanceof IndirectCosts) {
        return ((IndirectCosts) objectValue).getPercent();
    }
    throw new TypeMismatchException(objectValue, IndirectCosts.class);
}

@Override
public Object convertDataValueToObjectValue(Object dataValue, Session session) {
    if (dataValue == null) {
        return dataValue;
    } else if (dataValue instanceof String) {
        return IndirectCosts.getType(new BigDecimal((String) dataValue));
    }
    throw new TypeMismatchException(dataValue, BigDecimal.class);
}

@Override
public boolean isMutable() {
    return false;
}

@Override
public void initialize(DatabaseMapping databaseMapping, Session session) {
}
}

Within convertDataValueToObjectValue(Object I have to use String because the SQL generator defines the database column as varchar(255). I would like to have decimal(15,2) or something.

Thanks a lot Andre


Solution

  • The EclipseLink Converter interface defines a initialize(DatabaseMapping mapping, Session session); method that you can use to set the type to use for the field. Someone else posted an example showing how to get the field from the mapping here: Using UUID with EclipseLink and PostgreSQL

    The DatabaseField's columnDefinition, if set, will be the only thing used to define the type for DDL generation, so set it carefully. The other settings (not null, nullable etc) will only be used if the columnDefinition is left unset.