To represent money value I use BigDecimal because of the accuracy (double type causes errors). So, how can I store BigDecimal value in the ObjectBox, what type of a field or converter should I use?
Asked
Active
Viewed 344 times
1 Answers
6
There is no default support for BigDecimal so you have to create Converter. Here is an example:
public class BigDecimalConverter implements PropertyConverter<BigDecimal, String> {
@Override
public BigDecimal convertToEntityProperty(String databaseValue) {
return new BigDecimal(databaseValue);
}
@Override
public String convertToDatabaseValue(BigDecimal entityProperty) {
return entityProperty.toString();
}
}
@Entity
public class BigDecimalEntity {
@Convert(dbType = String.class, converter = BigDecimalConverter.class)
private BigDecimal decimal;
public BigDecimal getDecimal() {
return decimal;
}
public void setDecimal(BigDecimal decimal) {
this.decimal = decimal;
}
}

Aleksander Mielczarek
- 2,787
- 1
- 24
- 43
-
So, now such complex types can be converted to a string value to be stored. That's clear. Thank you! – Roman Sep 18 '17 at 11:30
-
1Converting to strings seems like a terrible idea because you can't query them with > or <. Better to convert to double float, or a long. – xpusostomos Dec 06 '20 at 04:25