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.

Two nodes with multiple relationship types (Also hello!)

Recently starting with neo4j and converting a program into using OGM. Have been able to get everything saved into the database just fine, however when retrieving, OGM seems to get confused on node types and relationships in this instance:

(LOCATION)<---[VISITED]---(PERSON)---[CORRESPONDED]--->(LOCATION)

When I do my get().load(Person.class, id, 1), I get the Person and the CLOTHING nodes just fine, but only the VISITED is attached to the returned Person Node and my CORRESPONDED set is null.

In the database, based on viewing in the browser, both LOCATION nodes are made and have the proper relationships on them

@NodeEntity
public class Person extends Entity {

    @Relationship(type = RelationshipTypes.CLOTHINGREL)
    private Set<Clothing> clothing;

    @Id
    private String id;

    @Relationship(type = RelationshipTypes.VisitedLocationREL)
    private Set<LocationVisited> LocationVisiteds;

    @Relationship(type = RelationshipTypes.CorrespondedLocationREL)
    private Set<LocationCorresponded> LocationCorrespondeds;

    private String name;
    private String gender;

    public Person() {
    }

    public Person(@JsonProperty(value = "id") String id,                 
                  @JsonProperty(value = "name") String name,
                  @JsonProperty(value = "gender") List<String> gender,
    ) {
        this.id = id != null ? id : "";
        this.name = name != null ? name : "";      
        this.gender = gender != null ? gender.get(0) : "";
		
        this.clothing = new HashSet<>();
        this.LocationVisiteds = new HashSet<>();
        this.LocationCorrespondeds = new HashSet<>();       
    }


    public void addClothing(Clothing cloth) {
        this.clothing.add(cloth);
    }

    public void addLocationCorresponded(LocationCorresponded dc) {
        this.LocationCorrespondeds.add(dc);
    }

    public void addLocationVisited(LocationVisited dd) {
        this.LocationVisiteds.add(dd);
    }
}

@NodeEntity
public abstract class Location extends Entity {

    @Id
    private String Location;

    Location(@JsonProperty(value = "Location") String Location) {
        this.Location = Location != null ? Location : "";
    }

    Location() {
    }

    public void addPerson(Person person) {
    }
}

@NodeEntity(label = "Location")
public class LocationVisited extends Location {
    @Relationship(type = RelationshipTypes.VisitedLocationREL, direction = Relationship.INCOMING)
    Set<Person> persons;

    LocationVisited(@JsonProperty(value = "Location") String Location) {
        super(Location);
        this.persons = new HashSet<>();
    }

    LocationVisited() {
    }

    public void addPerson(Person person) {
        this.persons.add(person);
    }
}

@NodeEntity(label = "Location")
public class LocationCorresponded extends Location {
    @Relationship(type = RelationshipTypes.CorrespondedLocationREL, direction = Relationship.INCOMING)
    Set<Person> persons;

    LocationCorresponded(@JsonProperty(value = "Location") String Location) {
        super(Location);
        this.persons = new HashSet<>();
    }

    LocationCorresponded() {
    }

    public void addPerson(Person person) {
        this.persons.add(person);
    }
}

2 REPLIES 2

If you are using Spring Data I would try something like:

Person p = personRepository.findById(id).orElseThrow(() -> new ItemNotFoundException("Count not find Person with id: " + id))

In my experience, that will populate all of your relationships in your Person Entity

Also, you've got some unusual naming conventions on your relationships. Make sure you haven't misinterpreted your own guidelines, in your strings, and respect directionality. I also hate to see things like (a)--(b) – always specify direction, when updating or querying. Direction is there for a reason, build it into your designs.