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?
How can I use ObjectBox to store money values (BigDecimal)?
Thanks for coming up with this. I just created a feature request. –
Harsho
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;
}
}
So, now such complex types can be converted to a string value to be stored. That's clear. Thank you! –
Vet
Converting to strings seems like a terrible idea because you can't query them with > or <. Better to convert to double float, or a long. –
Mahout
© 2022 - 2024 — McMap. All rights reserved.