Skip to content

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! #3822

Open
@caspian-borison-by

Description

@caspian-borison-by

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?

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions