简体   繁体   中英

Neo4J OGM: relationships between nodes becomes nodes with edges to the entities (nodes)

I am using Spring Boot and OGM. I have a @Nodeentity (Person) and a @RelationshipEntity (Familyrelation) with a type. When I put together nodes and relationships (a simple family-relationship between two persons), I get four nodes and the property of the two "extra" nodes is the relationship-id. Here is the code (excerpt):

@Repository
public interface PersonRepository extends Neo4jRepository<Person, Long> {

}

@Data
@Slf4j
@NodeEntity("Person")
public class Person {

  @Id
  @GeneratedValue
  private Long id;
  private String name;
  private int age;

  @Relationship(type = "FAMILY")
  private Set<Familyrelation> relations = new HashSet<>();

  public Set<Familyrelation> getRelations() {
    return relations;
  }

  public void setRelasjon(Familyrelation relasjon) {
    this.relations.add(relasjon);
  }

  //Hashcode and equals omitted

@RelationshipEntity(type = "FAMILY")
public class Familyrelation {

  @Id
  @GeneratedValue
  private Long relationshipId;
  @StartNode
  private Person person;
  @EndNode
  private Person relatedPerson;


  public void setPerson(final Person person) {
    this.person = person;
  }

  public void setRelatedPerson(final Person relatedPerson) {
    this.relatedPerson = relatedPerson;
  }

//Omitted hashcode, equals and other getters/setters

Here is how I create the graph:


 final Person person1 = new Person();
 person1.setName("Bob");
 person1.setAge(40);

 final Person person2 = new Person();
 person2.setName("Alice");
 person2.setAge(30);

 final Familyrelation familierelasjon1 = new Familyrelation();
 familierelasjon1.setPerson(person1);
 familierelasjon1.setRelatedPerson(person2);
 final Familyrelation familierelasjon2 = new Familyrelation();
 familierelasjon2.setPerson(person2);
 familierelasjon2.setRelatedPerson(person1);

 person1.setRelasjon(familierelasjon1);
 person2.setRelasjon(familierelasjon2);

 personRepository.save(person1);
 personRepository.save(person2);

关系正在成为一个节点

What am I missing here?

EDIT: omitting persisting person2 will yield the same result.

It seems like I have choosen to add so-called "rich relationships" ( https://docs.spring.io/spring-data/neo4j/docs/4.2.0.RC1/reference/html/#__relationshipentity_rich_relationships ). Omitting this relationship class will yield only edges between nodes. So instead of having a set with relationships, modify the set to contain Person.

Change this


@Relationship(type = "FAMILY")
  private Set<Familyrelation> relations = new HashSet<>();

to

@Relationship(type = "FAMILY")
  private Set<Person> relations = new HashSet<>();
final Person2 person1 = new Person2();
    person1.setName("Bob");
    person1.setAge(40);

    final Person2 person2 = new Person2();
    person2.setName("Alice");
    person2.setAge(30);

    person1.setRelasjon(person2);
    person2.setRelasjon(person1);

    personRepository.save(person1);

This will yield nodes with edges connected to them.

没有丰富的关系

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM