cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Custom query return multiple result instead of 1

Hi there. I'll try to explain my context.

I have a Pattern entity, a Creation entity. Creations concerns a Pattern. Creation can have some Images. A Pattern can be represented by an Image.

Structure is like :

Pattern {
  ...
  Image imgRepresent;
}

Creation {
  ...
  Pattern pattern;
  Collection<Image> images;
}

Image {
  Creation creation
}

I need to fetch an Image by its id, and also fetch the Creation related to it, the Pattern related to this Creation, and the imgRepresent (the Image that represent this Pattern).

My query :

MATCH (v:Image {id: {id}})-[b:BELONG_CREATION]-(p:Creation)-[c:CREA_CONCERNS_PATTERN]-(pa:Pattern) "
			+ "OPTIONAL MATCH (pa)-[e:IMAGE_REPRESENTS]-(i:Image) "
			+ "return v, b, p, c, pa, i"
Optional<Image> findCustById(String id);

Then on java side i'm supposed to be able to do :

Image img = repo.findCustById(123).get();
img .getCreation().getPattern().getImgRepresent()

Unfortunately repo.findCustById(123) throws the error " Incorrect result size: expected at most 1".
I've changed the return type to
List< Image > findCustById(String id);
to see what's returned, and indeed I have 2 Image objects, the one with the id 123, but also the one which is in Pattern.imgRepresent.

Is there a solution here to only get the Image corresponding to the filter id=123, and the nested Image only in Pattern, and not in resultset?

Thx

1 REPLY 1

Jiropole
Graph Voyager

I've encountered this myself – you want a single result of your return type, but you happen to have another object of that type in the path. The OGM is greedy about grabbing all the Images it can find, versus attaching the secondary image by mapping via the @RelationshipEntities in your model.

Several months ago, I believe @gerrit.meier (or maybe it was someone else) mentioned this was to be addressed in a future version of the OGM, so you might try updating your dependencies to latest.