Reputation: 352
@Document(collection = "loanDetails")
class LoanDetails {
@Transient
public static final String COLLECTION_NAME = "loanDetails
@Id
String id
String loanId
String loanUUID
String loanStatus
Date loanDateCreated
Date loanLastUpdated
BigDecimal loanAmount //Stores in String
}
I declared a class LoanDetails and a field with BigDecimal type loanAmount in it Whenever i saved some data in this collection
I tried to add annotation over loanAmount field
@Field(targetType = Decimal128) // This also throws error does not contain targetType attribute attached screenshot for the same
BigDecimal loanAmount
Upvotes: 3
Views: 4441
Reputation: 3483
You can register a custom converted in mongo config
@Configuration
public class MongoConfig
{
@Bean
public CustomConversions customConversions()
{
return new MongoCustomConversions(Arrays.asList(
new BigDecimalToDocumentWritingConverter()
));
}
}
And do the conversion there
public class BigDecimalToDocumentWritingConverter implements Converter<BigDecimal, String> {
@Override
public String convert(@Nullable BigDecimal amount) {
if (amount == null) return null;
return String.valueOf(amount.doubleValue());
}
}
Upvotes: -1
Reputation: 13103
Probably, you need to drop / migrate your already created loanDetails
, since the loanAmount
field is persisted as String
.
By adding @Field(targetType = FieldType.DECIMAL128)
to your loanAmount
field it should work.
import org.springframework.data.mongodb.core.mapping.FieldType;
...
@Field(targetType = FieldType.DECIMAL128)
BigDecimal loanAmount;
...
public LoanDetails() {
loanAmount = new BigDecimal(21.905334472656250);
}
...
template.save(loan);
template.find(new Query(), org.bson.Document.class, "loanDetails").forEach(l -> {
System.out.println(l.get("loanAmount").getClass().getName() + " " + l.get("loanAmount"));
});
//org.bson.types.Decimal128 21.90533447265625
template.findAll(LoanDetails.class).forEach(l -> {
System.out.println(l.loanAmount.getClass().getName() + " " + l.loanAmount);
});
//java.math.BigDecimal 21.90533447265625
If you get Conversion to Decimal128 would require inexact rounding of XXX
Note: The
Decimal128
type only supports up to 34 digits of precision. (Generally, "precision" = total # of digits, while "scale" = # of decimal digits). Whereas a Java BigDecimal can go higher. So, before saving to the DB, you need to round the BigDecimal value (e.g. useBigDecimal.setScale()
) method.
BigDecimalConverter issue | Decimal128
Upvotes: 1