ClassCastException if not using JSONDocument



i have found a problem with the new Couchbase Java SDK.
i want the returned Document as RawJSONDocument and i must give this in the ViewQuery and in the ViewRow.document() call. i think this is redundant and should changed to Generics or ViewQuery is saving the Class and only the ViewRow is Generic and document() returns the defined Class.



@timtasse where do you give it for the ViewQuery? Can you post an example?


ClassCastException comes if:
bucket is the connected Bucket instance

result = bucket.query(ViewQuery.from(design, view).includeDocs(true))

ClassCastException says JSONDocument is not cast to RawJSONDocument

works if:

result = bucket.query(ViewQuery.from(design, view).includeDocs(RawJSONDocument.class))


@timtasse right, I see what you are saying. Here is the trouble with it: the includeDocs fetches the documents in the background already, so when you call .document() on the API (which you can do btw even when not setting includeDocs(true)), it is already loaded. There is no way to “carry over” the generic type that is used at a later point, because then the document is already loaded.

Also the other way round you need it set, because even when set on includeDocs properly, you can’t carry over the generic type into the .document call. And while I think about it, it is even possible that different documents return different types. Keep in mind that the includeDocs was only added as an optimization for the blocking API, if you are running non-blocking there is nothing to gain from just calling .document() when you need it.


ok i have delete the includeDocs method and now it works again.
i have now migrated from client 1.4, there i used this method.
i use the blocking API because the data is used in EJBs,
so i must initialize all data before it goes to presentation layer.

but the speed is ok, it needs 1 to 2 ms for returning the document.

thanks for the explaining


@timtasse if you are on the blocking API, using the includeDocs() will be a good idea if you indeed need to load the full data. Why don’t you keep it and just specify the document type twice?


i have found a good place for me to give this twice.
i build a library around the couchbase-java-sdk and there i not want to specify this explicit, now i have found a good place and do it on the side where the lib is used.
you can keep the idea for using the type from the ViewQuery to the ViewRow in mind, for some API enhancement. i think that would more logically.
please document that it is necessary to give the type twice or the Exception is thrown in runtime.


Good idea, I filed a docs ticket to track it: