Open ALGA0887 opened 7 months ago
Thank you for reaching out!
@DocumentReference
is no drop in replacement for @DBRef
, but an alternative approach of storing references between documents. I'm not aware of any section in the reference documentation that would promote the given scenario to work out of the box. If so, please let us know so we can update that part to be more clear.
Hello @christophstrobl!
Thank you for your answer!
Yes you are right: there is nothing directly about such replacement in the documentation.
Nevertheless it is not expected behavior that processing of such combination of annotations @DocumentReference
and @ReadOnlyProperty
on some field leads to loading of any data (in our case it is DBRef
but in general it could be anything) from MongoDB during evaluation of Spel expression. With spring-data our schema is specified in java code and java code should have higher priority than data in MongoDB. In our code this field is not DBRef
and should be ignored by default.
https://docs.spring.io/spring-data/mongodb/docs/current-SNAPSHOT/reference/html/#mapping-usage.document-references
It is also possible to model relational style One-To-Many references using a combination of @ReadonlyProperty and @DocumentReference. This approach allows link types without storing the linking values within the owning document but rather on the referencing document as shown in the example below.
So there is expectation after reading of documentation above that combination of @ReadonlyProperty
and @DocumentReference
tells Mapping Framework just ignore data in this field in MongoDB. If we don't store anything in the field, why it is necessary to read anything from it? How we can ignore existing data in MongoDB then?
Hello @christophstrobl!
Could you please share plans regarding this issue taking into account my last comment?
Hello!
I would like to describe a problem with replacement of annotation
@DBRef
to annotation@DocumentReference
in case old data in MongoDB exist. For this purpose I have created a project with reproducing of this problem: https://github.com/ALGA0887/reference.git.So lets imagine the following class in previous releases of some application:
Here there is a
@DBRef
to the list ofOrderItem
:OrderItem
itself also contains information aboutguestIds
. So in previous releasesguests
were saved in MongoDB withDBRef
toorderItems
.In current release it was decided not to have a field
orderItems
inguests
collection since it is overhead. And it was planned to change the annotation@DBRef
to annotation@DocumentReference
in the following way:For new records in
guests
such changes work as expected (testcom.alga.reference.ReferenceApplicationTests#testLoadGuestWithoutDbRef
). But old records can't be read with the following exception:So my question is why
DBRef
is taken into account in lookup expression? Is it bug in reference resolving?In my project I have tried the following workaround and it works:
But if we imagine that migration of data is impossible (I mean remove
DBRef
for old records inguests
) then how we can apply such changes without any workaround at all?To reproduce a problem just comment
@ActiveProfiles(value = "workaround")
inReferenceApplicationTests
and run testtestLoadGuestWithDbRef
.