簡體   English   中英

Neo4J OGM:節點之間的關系變成與實體(節點)有邊的節點

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

我正在使用 Spring 引導和 OGM。 我有一個 @Nodeentity (Person) 和一個帶有類型的 @RelationshipEntity (Familyrelation)。 當我將節點和關系(兩個人之間的簡單家庭關系)放在一起時,我得到四個節點,兩個“額外”節點的屬性是關系 ID。 這是代碼(摘錄):

@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

這是我創建圖表的方式:


 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);

關系正在成為一個節點

我在這里想念什么?

編輯:省略持久 person2 將產生相同的結果。

似乎我選擇添加所謂的“豐富的關系”( https://docs.spring.io/spring-data/neo4j/docs/4.2.0.RC1/reference/html/#__relationshipentity_rich_relationships 忽略這種關系 class 將只產生節點之間的邊。 因此,與其擁有一個包含關系的集合,不如修改該集合以包含 Person。

改變這個


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

@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);

這將產生具有連接到它們的邊的節點。

沒有豐富的關系

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM