maverickcr
maverickcr

Reputation: 71

How to convert a BigInteger to Objectid with spring data

I working with java 8 and the following spring specification:

<spring.version>4.2.3.RELEASE</spring.version>
<spring.security.version>4.0.3.RELEASE</spring.security.version>
<spring.data.version>1.8.1.RELEASE</spring.data.version>

I have a document that obviously has an _id, which is working perfectly, but I have another field that stores a reference that is another _id, this is my expected JSON

{
    "_id" : ObjectId("56c8f330d4c65e543ceac8de"),
    "companyId" : ObjectId("56c8f330d4c65e543ce8c8de"),
    "name" : "COMPANY S.R.L",
    more fields......
}

I have created the following converter:

 @Component
   public class PurchaseWriterConverter implements Converter<Purchase, DBObject>
   {
       @Override public DBObject convert(Purchase source)
        {
        DBObject dbo = new BasicDBObject();
        dbo.put("_id", source.getId());
        **ObjectId objCompany = new ObjectId(source.getCompany().toString(16));
        dbo.put("company", objCompany);**
        dbo.put("supplier", source.getSupplier());
        dbo.put("status", source.getStatus().toString());
        dbo.put("attendant", source.getAttendant());
        dbo.put("cashier", source.getCashier());

        List<Object> orders = new BasicDBList();

        for (OrderWrapper order : source.getOrders())
        {
            DBObject orderDBObject = new BasicDBObject();
            orderDBObject.put("description", order.getDescription());
            orderDBObject.put("basePrice", order.getBasePrice());
            orderDBObject.put("grossWeight", order.getGrossWeight());
            orderDBObject.put("netWeight", order.getNetWeight());
            orderDBObject.put("subtotal", order.getSubtotal());
            orderDBObject.put("totalWeight", order.getTotalWeight());
            orders.add(orderDBObject);
        }
        dbo.put("orders", orders);
        dbo.put("createDate", source.getCreateDate());
        dbo.put("updateDate", source.getUpdateDate());
        dbo.put("approvedBy", source.getApprovedBy());
        dbo.put("createDate", source.getCreateDate());
        dbo.put("updateDate", source.getUpdateDate());
        dbo.removeField("_class");

        return dbo;
    }
}

The following lines are not working

dbo.put("_id", source.getId()); **ObjectId objCompany = new ObjectId(source.getCompany().toString(16));

So, I tried to create another converter in order to convert BigInteger to ObjectId, something like this

public class BigIntegerToObjectIdConverter implements Converter {

@Override
public ObjectId convert(BigInteger source) {
    return source == null ? null : new ObjectId(source.toString());
}

}

But I getting the same error:

org.springframework.core.convert.ConversionFailedException: Failed to convert from type com.xxxxxxxxxxx.entity.Purchase to type com.mongodb.DBObject for value 'Purchase{company=1, orders=[com.xxxxxxx.equilibrium.wrapper.OrderWrapper@9cd3f65], supplier=26857458392532697059830357595, approvedBy='', status=TO_BE_APPROVED, attendant='User Admin', cashier=''}'; nested exception is java.lang.IllegalArgumentException: invalid ObjectId [1]

the value that I sending as a biginteger is "1".

Can anybody gives me some directions about how to convert a BigInteger to ObjectId or suggest something different.

Upvotes: 0

Views: 1444

Answers (1)

GaspardP
GaspardP

Reputation: 4832

I just answered to a similar question here https://stackoverflow.com/a/46508892/4660481

A valid ObjectId can be constructed from exactly 24 hexadecimal characters. In this case, your .toString(16) will return "1" which does not have a length of 24. What you would want in this case is "000000000000000000000001". You can resolve this issue by padding with 0s:

String hexString24 = StringUtils.leftPad(source.getCompany().toString(16), 24, "0");
ObjectId objCompany = new ObjectId(hexString24);

Upvotes: 1

Related Questions