Showing results for 
Search instead for 
Did you mean: 

Head's Up! Site migration is underway. Phase 2: migrate recent content

Projections for dynamic relationships

Hi all,

I am using Spring Data Neo4J 6.1.2 and would like to know if you can apply projections to dynamic relationships to retrieve only fields I am interested in.

Sample of node class:

public class Entity {

    private String id;

    private Map<String, List<RelatedEntity>> relatedEntities = new HashMap<>();

Sample of relationship class:

public class RelatedEntity {
    private Long id;

    private Entity relEntity;

Repository method: (So I can return different projections)

<T> Optional<T> findById(String id, Class<T> type);

Projection interface

public interface RelatedEntitiesProjection {

    Map<String, List<RelatedEntityProjection>> getRelatedEntities();

The related entity projection has a few fields I wish to return.

When I run this, all the entity fields are returned. Also the sub relatedEntities are recursively retrieved even though the relatedEntities field is not in the RelatedEntity projection.

    "relatedEntities": {
        "SUPPLIES": [
                "id": 2,
                "relEntity": {
                    "id": "5ccd6adc-b0ad-4249-93b5-9bc608dbbb0b",
                    "key": null,
                    "name": "product1",
                    "description": null,
                    "relatedEntities": {} // these are populated when there are related entities
                "createdBy": null,
                "createdDate": null,
                "lastModifiedDate": null,

Is there anyway to only return the projection fields for the relationship entities?



The projection of the dynamic relationship map with a collection of projections will not work correctly.
This is limited by the generics information we can get out of the (projection) model in this case.
It will always default (with the current SDN version 6.1.5) to the entity.
The good news is, that it won't be empty anymore because we introduced with the latest version the support for same type (also via multiple hops) references in projections.

OK thanks Gerrit. I've currently worked round it by mapping the fields I need through a response DTO.

I think the dynamic relationship feature is a very powerful addition to Spring Neo4J. It will hopefully allow us to build customisable knowledge graphs. Love to discuss the use case some time.