spring-projects / spring-projects/spring-data-mongodb

Query or sort by @Reference association causes Mapping Exception - Invalid path reference <path>! Associations can only be pointed to directly or via their id property!

Open
#3,822 2 comments 2 reactions 1 assignee View on GitHub

@christophstrobl is already working on this.

Since Sep 20, 2021.

type: task
Dominant language
Java
Stars
1.7k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

When re-using @Document entities as embedded objects under another document, it can be desirable to disable auto-index creation for the embedded objects. Searching the internet turns up this stackoverflow link which seems to be the only way to get this desired behavior (ignore any defined indexes on Child class when creating Parent collection).

e.g.

@Document
class Parent {
   @Id String id;   
   @Reference Child child;
   @Reference List<Child> children;
}

@Document
class Child {
   @Id String id;
   String name;
}

However, you try to then query or sort parents via either above child/children path, it leads to an exception:

   // Any of these leads to the exception:
   mongoTemplate.find(Query.query(Criteria.where("child.name").is("blah")), ParentDocument.class);
   mongoTemplate.find(new Query().with(Sort.by("child.name")), ParentDocument.class);
   mongoTemplate.find(Query.query(Criteria.where("children.name").is("blah")), ParentDocument.class);
   mongoTemplate.find(new Query().with(Sort.by("children.name")), ParentDocument.class);

org.springframework.data.mapping.MappingException: Invalid path reference child.name! Associations can only be pointed to directly or via their id property!

	at org.springframework.data.mongodb.core.convert.QueryMapper$MetadataBackedField.getPath(QueryMapper.java:1224)
	at org.springframework.data.mongodb.core.convert.QueryMapper$MetadataBackedField.<init>(QueryMapper.java:1060)
	at org.springframework.data.mongodb.core.convert.QueryMapper$MetadataBackedField.<init>(QueryMapper.java:1037)
	at org.springframework.data.mongodb.core.convert.QueryMapper.createPropertyField(QueryMapper.java:340)
	at org.springframework.data.mongodb.core.convert.QueryMapper.mapFieldsToPropertyNames(QueryMapper.java:230)
	at org.springframework.data.mongodb.core.convert.QueryMapper.getMappedSort(QueryMapper.java:198)
	at org.springframework.data.mongodb.core.MongoTemplate.getMappedSortObject(MongoTemplate.java:2886)
	at org.springframework.data.mongodb.core.MongoTemplate.access$300(MongoTemplate.java:162)
	at org.springframework.data.mongodb.core.MongoTemplate$QueryCursorPreparer.prepare(MongoTemplate.java:3280)
	at org.springframework.data.mongodb.core.CursorPreparer.initiateFind(CursorPreparer.java:71)
	at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:2806)
	at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:2543)
	at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:2525)
	at org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:847)
	at org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:833)

I am unsure if this is or is not a a bug -- but it seems weird. I can understand why @DBRef would throw the above error, but I am uncertain why @Reference does? Alternatively, is there a different way to disable embedded object index creation when using IndexOperations?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.