我有两个实体Person and Building。两者都由两个关系实体和#34; PERSON_HAS_ACCESS"(多人可以访问多个建筑物)和" PERSON_OWNS" (一个建筑物只能由一个人拥有,但一个人可以拥有多个建筑物)。我能够坚持每种类型的多个关系(2具体),也可以在neo4j浏览器中看到。但是,当我使用会话API访问Person实体时:
session.loadAll(klazz, new Filter("uuid", uuid), 1).getAccessRelations.size()
总是重新回归1,不管它有多少关系。
我总是只有一种关系。
这是我的域名模型。我添加了equals和hashcode,但问题仍然存在。此外,我试过只有一个关系,但问题再次相同。这让我疯狂!!。任何帮助将不胜感激。
@NodeEntity
public class Building {
@GraphId
private Long id;
private String uuid;
@Relationship(type = "PERSON_OWNS", direction = Relationship.INCOMING)
private OwnsBuilding ownsRelation;
@Relationship(type = "PERSON_HAS_ACCESS", direction = Relationship.INCOMING)
private Collection<CanAccess> accessRelations = new HashSet<CanAccess>();
public Collection<CanAccess> getAccessRelations() {
return accessRelations;
}
public Building setAccessRelations(Collection<CanAccess> accessRelations) {
this.accessRelations = accessRelations;
return this;
}
public String getUuid() {
return uuid;
}
public Building setUuid(String uuid) {
this.uuid = uuid;
return this;
}
public OwnsBuilding getOwnsRelation() {
return ownsRelation;
}
public Building setOwnsRelation(OwnsBuilding ownsRelation) {
this.ownsRelation = ownsRelation;
return this;
}
@Override
public boolean equals(Object obj) {
if (obj == null || !(obj instanceof Building)) return false;
if(id == null) return false;
return obj == this || this.id.equals(((Building) obj).id);
}
@Override
public int hashCode() {
return id != null ? id.hashCode() : super.hashCode();
}
}
@NodeEntity
public class Person {
private String uuid;
@GraphId
private Long id;
@Relationship(type = "PERSON_HAS_ACCESS", direction = Relationship.OUTGOING)
private Collection<CanAccess> accessRelations = new HashSet<CanAccess>();
@Relationship(type = "PERSON_OWNS", direction = Relationship.OUTGOING)
private Collection<OwnsBuilding> ownsRelations = new HashSet<OwnsBuilding>();
public Collection<CanAccess> getAccessRelations() {
return accessRelations;
}
public Person setAccessRelations(Collection<CanAccess> accessRelations) {
this.accessRelations = accessRelations;
return this;
}
public String getUuid() {
return uuid;
}
public Person setUuid(String uuid) {
this.uuid = uuid;
return this;
}
public Collection<OwnsBuilding> getOwnsRelations() {
return ownsRelations;
}
public Person setOwnsRelations(Collection<OwnsBuilding> ownsRelations) {
this.ownsRelations = ownsRelations;
return this;
}
@Override
public boolean equals(Object obj) {
if (obj == null || !(obj instanceof Person)) return false;
if (id == null) return false;
return obj == this || this.id.equals(((Person) obj).id);
}
@Override
public int hashCode() {
return id != null ? id.hashCode() : super.hashCode();
}
}
@RelationshipEntity(type = "PERSON_HAS_ACCESS")
public class CanAccess {
@GraphId
private Long id;
@StartNode
private Person person;
public CanAccess(){}
public CanAccess(Person person, Building building) {
this.person = person;
this.building = building;
}
@EndNode
private Building building;
public Person getPerson() {
return person;
}
public CanAccess setPerson(Person person) {
this.person = person;
return this;
}
public Building getBuilding() {
return building;
}
public CanAccess setBuilding(Building building) {
this.building = building;
return this;
}
@Override
public boolean equals(Object obj) {
if (obj == null || !(obj instanceof CanAccess)) return false;
if (id == null) return false;
return obj == this || this.id.equals(((CanAccess) obj).id);
}
@Override
public int hashCode() {
return id != null ? id.hashCode() : super.hashCode();
}
}
@RelationshipEntity(type = "PERSON_OWNS")
public class OwnsBuilding {
@GraphId
private Long id;
public OwnsBuilding() {
}
public OwnsBuilding(Person owner, Building building) {
this.owner = owner;
this.building = building;
}
@StartNode
private Person owner;
@EndNode
private Building building;
public Person getOwner() {
return owner;
}
public OwnsBuilding setOwner(Person owner) {
this.owner = owner;
return this;
}
public Building getBuilding() {
return building;
}
public OwnsBuilding setBuilding(Building building) {
this.building = building;
return this;
}
@Override
public boolean equals(Object obj) {
if (obj == null || !(obj instanceof OwnsBuilding)) return false;
if (id == null) return false;
return obj == this || this.id.equals(((OwnsBuilding) obj).id);
}
@Override
public int hashCode() {
return id != null ? id.hashCode() : super.hashCode();
}
}
答案 0 :(得分:2)
请将您的Collection
更改为Set
-
@Relationship(type = "PERSON_HAS_ACCESS", direction = Relationship.INCOMING)
private Collection<CanAccess> accessRelations = new HashSet<CanAccess>();
到
@Relationship(type = "PERSON_HAS_ACCESS", direction = Relationship.INCOMING)
private Set<CanAccess> accessRelations = new HashSet<CanAccess>();
OGM可以创建数组或Set
,List
,Vector
。
此外,虽然与此问题无关,但请确保当您的关系方向为INCOMING
时,您明确注释任何属性/访问者/更改者。
在你的情况下:
@Relationship(type = "PERSON_HAS_ACCESS", direction = Relationship.INCOMING)
private Set<CanAccess> accessRelations = new HashSet<CanAccess>();
@Relationship(type = "PERSON_HAS_ACCESS", direction = Relationship.INCOMING)
public Set<CanAccess> getAccessRelations() {...}
@Relationship(type = "PERSON_HAS_ACCESS", direction = Relationship.INCOMING)
public Building setAccessRelations(Set<CanAccess> accessRelations) {...}
另一个是make sure your object model is navigable,特别是在关系实体的情况下。创建关系实体时,请记住在参与节点上设置它,例如
public CanAccess(Person person, Building building) {
this.person = person;
this.building = building;
this.person.getAccessRelations().add(this);
this.building.getAccessRelations().add(this);
}