3
votes

I get an exception while I am trying to get objects from CouchBase. I use Spring Data Repository to query data. I save data to CouchBase without any exceptions. I need to query data by dataTime which should be in range dateBefore and dateAfter. I have such Java Object:

@Document
public class DataObject{

    @Id
    @Field
    private String dataId;
    @Field
    private boolean isEnabled;
    @Field
    private BigDecimal price;
    @Field
    private LocalDateTime dataTime;
    @Field
    private Currency currency;

    public DataObject() {
    }

    public DataObject(String dataId, boolean isEnabled, BigDecimal price, Date dataTime, Currency currency) {
        this.dataId = dataId;
        this.dataId = dataId;
        this.price = price;
        this.dataTime = dataTime;
        this.currency = currency;
    }
}

I have such repository interface:

public interface DataObjectRepository extends CrudRepository<DataObject, String> {

    @Query("#{#n1ql.selectEntity} WHERE dataTime BETWEEN $1 AND $2 AND #{#n1ql.filter}")
    List<DataObject> getDataObjectsBetweenDates(LocalDateTime beforeTime, LocalDateTime afterTime);

}

When I run this query I got an exception:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: Unsupported type for JsonArray: class java.time.LocalDateTime

I tried to use Timestamp, old Date instead LocalDateTime, however I got the same exception but with different Class in the message.

I am new to CouchBase, so any help appreciated

1
Yes. My first thought was that Couch Does not support LocalDateTime querying, then I tried Timestamp and got: Unsupported type for JsonArray: class java.sql.Timestamp and also I tried Date and got Unsupported type for JsonArray: class java.util.Date - migAlex
I don't know why your error thinks you have a JsonArray. Where are you changing things? In the class or just on the N1ql method? - OneCricketeer
By the way I copied some query and then modified for my needs. It might be that error in the query - migAlex
I'm not entirely sure what AND #{#n1ql.filter} is supposed to do, but if you can save data using LocalDateTime, I think that class should be converted fine. - OneCricketeer
Is it possible that it can save it to DB(converts to JSON object), however it is not possible to query by Date range. Couch should know how to work with LocalDateTime like a Date - migAlex

1 Answers

0
votes

You got this exception, because couchbase java client tries to add your beforeTime to parameters array, but can't do this. LocalDateTime is not supported type and don't satisfy to this check inside client :

public static boolean checkType(Object item) {
    return item == null
        || item instanceof String
        || item instanceof Integer
        || item instanceof Long
        || item instanceof Double
        || item instanceof Boolean
        || item instanceof BigInteger
        || item instanceof BigDecimal
        || item instanceof JsonObject
        || item instanceof JsonArray;
}

When you save LocalDateTime to couchbase spring converts it to Long :

Date.from(source.atZone(systemDefault()).toInstant()).getTime();

So, you can also convert your dates with this approach and send Long parameters instead of LocalDateTime