I need to retrieve data from legacy Couchbase bucket without a specific schema. It can be mapped as com.couchbase.client.java.document.StringDocument
from the Couchbase Java client. I can do this directly using Java client:
bucket.get(key, StringDocument.class)
But how can I map this StringDocument
using org.springframework.data.repository.CrudRepository
?
I can't create such interface interface UserRepository extends CrudRepository<StringDocument, String>
because Spring Data Couchbase requires @Document and @Id annotations.
All entities should be annotated with the @Document annotation. Also, every field in the entity should be annotated with the @Field annotation. While this is - strictly speaking - optional, it helps to reduce edge cases and clearly shows the intent and design of the entity.
There is also a special @Id annotation which needs to be always in place. Best practice is to also name the property id.
Should I directly use Bucket, create my own similar entity, or there is another solution?
I use following version of spring-boot-starter-data-couchbase: 1.5.9.RELEASE
.
If its just a StringDocument
(and it wasn't even saved by spring-data-couchbase
to begin with.. which injects the _class
attribute for deserialization purposes) then I'd just use the bucket to retrieve it.
Keep in mind if your repository (I'm talking about any other repository you have defined that is mapped to the same bucket the StringDocument is in) is defined as CouchbaseRepository
you will be able to access the bucket itself from the repository methods like so repository.getCouchbaseOperations().getCouchbaseBucket()
. Or maybe you can try the following repository.getCouchbaseOperations().findById("id", StringDocument.class)
.