I am trying to figure out whether, is there any annotation or a way to restrict the documents which are of specific class type when using findById method querying by passing Id of the document.
Situation: Assume, there are class A and class B which extends A. While saving into couch, we could be able to save as class B but when we are trying to get using findById in different context where class A is relevant, its fetching the document which is of type B and mapping it to class A by mutating and thereby causing data inconsistency.
Do we have any implicit way to restrict on class while querying with findById method? Any help/suggestions would be really appreciated. Thanks!!
Just to be clear,
findById() is from Spring Data, right?
Assuming yes, @davidkelly can probably help.
Yes, it is from Spring Data.
I believe one way that should work would be to have a repository for A, and a repository for B. Then, just use the appropriate repo. However, when you save with repo A, the _class will be written as the full class path to A, similarly for B. So if you want to save as B, then later update as A, this probably will not work.
I think maybe you could get your A as a projection, however. Checkout https://docs.spring.io/spring-data/couchbase/docs/current/reference/html/#couchbase.repository.n1ql - the section on DTO Projections. That will give you a way to return an A, at least. But perhaps you’d want to do something clever to accept an A in a method in your repo (along with an id, etc…) and update the B.