Skip to content

Enhance @DocumentReference to allow one-to-many style lookups #3798

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
caspian-borison-by opened this issue Sep 1, 2021 · 3 comments
Closed
Assignees
Labels
in: references DBRef and @DocumentReference type: enhancement A general enhancement

Comments

@caspian-borison-by
Copy link

caspian-borison-by commented Sep 1, 2021

This is an amazing new feature and I definitely am looking forward to using it when 2.6 is released.

However, I have been playing around with it in some sandbox projects and (perhaps there is something I am missing, the documentation doesn't seem to address it?) but there appears to be one specific functional gap in the usage of it.

Assume I have:

class Book {
    @Id   
    String id;
    
    private String publisherId;
}

class Publisher {
   @Id
   String id;
   
   @ReadOnlyProperty
   @DocumentReference(lookup="{'publisherId': <this.id> ???}")
   List<Book> books;
}

Is it at all possible for the list of books to do a db.books.find({'publisherId': Publisher::id}) without storing a List bookIds on the Publisher class? Perhaps I am missing something from the documentation and this is already supported?

If not, then (I realize we're starting to get into foreign key relationships with Mongo which isn't the greatest thing), it does seem like a use case that would be beneficial to support if possible?

Thanks!

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Sep 1, 2021
@caspian-borison-by caspian-borison-by changed the title Enhance @DocumentReference to allow ManyToOne style lookups Enhance @DocumentReference to allow OneToMany style lookups Sep 1, 2021
@christophstrobl christophstrobl self-assigned this Sep 6, 2021
@christophstrobl christophstrobl added status: pending-design-work Needs design work before any code can be developed and removed status: waiting-for-triage An issue we've not yet triaged labels Sep 6, 2021
@christophstrobl
Copy link
Member

@caspian-borison-by thanks for the suggestion! Turns out with a few bits changed this can be done (#3802).

@caspian-borison-by
Copy link
Author

caspian-borison-by commented Sep 7, 2021

Awesome! I can definitely see the self reference being useful.
Thank you!
Edit: Whoops, didn't mean to close the issue -- just write a reply. Sorry. :/

@mp911de mp911de closed this as completed in c8307d5 Sep 8, 2021
mp911de added a commit that referenced this issue Sep 8, 2021
Tweak reference documentation wording. Extract self/target source dereferencing into utility methods.

See: #3798
Original pull request: #3802.
@mp911de mp911de added this to the 3.3 M3 (2021.1.0) milestone Sep 8, 2021
@mp911de mp911de added type: enhancement A general enhancement and removed status: pending-design-work Needs design work before any code can be developed labels Sep 8, 2021
@mp911de mp911de changed the title Enhance @DocumentReference to allow OneToMany style lookups Enhance @DocumentReference to allow one-to-many style lookups Sep 8, 2021
@mp911de mp911de added the in: references DBRef and @DocumentReference label Sep 8, 2021
@alabotski
Copy link

Hi can you explain me

MongoTemplateDocumentReferenceTests

Test - allowsOneToMayStyleLookupsUsingSelfVariable works fine

But if I rewrite it. It doesn't pass... =(

	@Test // GH-3798
	void allowsOneToMayStyleLookupsUsingSelfVariable_2() {

		OneToManyStylePublisher publisher = new OneToManyStylePublisher();
		template.save(publisher);

		OneToManyStyleBook book1 = new OneToManyStyleBook();
		book1.id = "id-1";
		book1.publisherId = publisher.getId();

		OneToManyStyleBook book2 = new OneToManyStyleBook();
		book2.id = "id-2";
		book2.publisherId = "p-200";

		OneToManyStyleBook book3 = new OneToManyStyleBook();
		book3.id = "id-3";
		book3.publisherId = publisher.getId();

		template.save(book1);
		template.save(book2);
		template.save(book3);

		OneToManyStylePublisher target = template.findOne(query(where("id").is(publisher.id)), OneToManyStylePublisher.class);
		assertThat(target.books).containsExactlyInAnyOrder(book1, book3);
	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: references DBRef and @DocumentReference type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants