0

I am saving a Document in the Couchbase using Spring. For some fields extra information is added.

POJO:

 @Document public class PlayerTxn implements Serializable {

    private static final long serialVersionUID = -2569497126561L;

    @Id     private String id;

    @Field  private Date txnDate;

    @Field  private BigDecimal wagerAmount;

    @Field  private BigDecimal pointsAwarded;

    @Field  private String segment;

RequiredResult:

{   "txnDate": 234234234324,   "wagerAmount": 234.33,   "pointsAwarded":
 23.2,   "segment": "xxx" }

End result:

{   "_class": "com.app.model.PlayerTxn",   "segment":
 "xxx",   "wagerAmount": {
     "intCompact": 24312,
     "scale": 2,
     "precision": 5,
     "stringCache": "243.12"   },   "pointsAwarded": {
     "intCompact": -9223372036854776000,
     "scale": 38,
     "precision": 0,
     "intVal": {
       "signum": 1,
       "bitCount": 0,
       "mag": [
         3800,
         -457875904,
         -1778440383,
         -1805069212,
         295579091
       ],
       "lowestSetBit": 0,
       "firstNonzeroIntNum": 0,
       "bitLength": 0
     }   },   "txnDate": 1466417747057 }

Had to write a customConverter for BigDecimal.

But still "_class" is being added in the Document. Any idea how can I remove it?

mickygo
  • 107
  • 1
  • 10

1 Answers1

2

Looks like you need to add custom converters for BigDecimal values, something like:

// extend or add a customized couchbase config like this
public class CustomCouchbaseConfig extends AbstractCouchbaseConfiguration {

    @Override
    public CustomConversions customConversions() {
        return new CustomConversions(Arrays.asList(
            BigDecimalToString.INSTANCE,     
            StringToBigDecimalConverter.INSTANCE));
    }

    @WritingConverter
    public static enum BigDecimalToString implements Converter<BigDecimal, String> {
        INSTANCE;

        @Override
        public String convert(BigDecimal source) {
            // or a more appropriate implementation
            return source.toString() ;
        }

    }


    @ReadingConverter
    public static enum StringToBigDecimalConverter implements Converter<String, BigDecimal> {
        INSTANCE;

        @Override
        public BigDecimal convert(String source) {
            return new BigDecimal(source);
        }

    }

}

Be sure, to make those converter methods null-safe!

See the Spring Data Couchbase Reference for more information: http://docs.spring.io/spring-data/couchbase/docs/2.1.2.RELEASE/reference/html/#datatypes

Edit: Regarding the _class attribute, see this SO question and the answer by Oliver Giercke for the reasoning behind _class. The question is aimed at MongoDB but can easily be translated for Couchbase as well.

Community
  • 1
  • 1
  • Hi Christian, Thanks for your reply. I was able to solve the problem(admin deleted my comment, i did not know that i was supposed to edit the original post) by wirting a custom converter like you have mentioned but I still get _class in the Document. – mickygo Jun 22 '16 at 14:13