Spring Data Neo4j 4.0.0:无法在具有相同标签的节点之间创建关系
Spring Data Neo4j 4.0.0: Can't Create Relationship Between Nodes with the Same Label
我正在使用 Spring Data Neo4j 4.0.0 和 Neo4j 2.2.1,我正在尝试在具有完全相同标签的两个节点之间创建关系。
所以,我有一个 NodeEntity class,我有一个内部变量,其类型与 class 本身相同,并将其注释为 Relationship。
但是,当我使用存储库对象的 save() 方法将对象保存到数据库时,无法创建关系。
提前致谢,非常感谢您的建议!
编辑
这里是节点实体classes
public class ArchitectureUnitState extends UnitState {
public ArchitectureUnitState()
{
super();
}
public ArchitectureUnitState(String name, String description, String parentArchitectureUnitName)
{
super(name, description);
this.parentArchitectureUnitName = parentArchitectureUnitName;
}
@Relationship(type="PART_OF", direction = Relationship.OUTGOING)
private ArchitectureUnitState architectureUnitState;
@Relationship(type="STATE_OF", direction = Relationship.OUTGOING)
private ArchitectureUnit architectureUnit;
@Transient
private String parentArchitectureUnitName;
public void partOf(ArchitectureUnitState architectureUnitState) {
this.architectureUnitState = architectureUnitState;
}
public void stateOf(ArchitectureUnit architectureUnit) {
this.architectureUnit = architectureUnit;
}
public void childOf(String parentArchitectureUnitName) {
this.parentArchitectureUnitName = parentArchitectureUnitName;
}
public String getParentName() {
return parentArchitectureUnitName;
}
}
@NodeEntity
public class UnitState {
@GraphId
protected Long id;
private String name;
private String description;
public UnitState() {
}
public UnitState(String name, String description) {
this.name = name;
this.description = description;
}
public void setName(String name) {
this.name = name;
}
public void setDescription(String description) {
this.description = description;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
}
因此,顺序是:我创建了 ArchitectureUnitState 对象,将一个对象映射到另一个对象,然后使用 ArchitectureUnitStateRepository 的 save() 方法保存。
如果我这样做,PART_OF 关系不会创建,尽管我在调试中看到值在那里。
我现在的解决方法是先保存所有 ArchitectureUnitState 节点,再次从数据库中检索它们,将它们映射到另一个,然后再次保存。这样就可以创建关系了,但是我需要保存两次。
这是我使用上面的 类 的测试用例。
@Test
public void testArchitectureState() {
ArchitectureUnitState state1 = new ArchitectureUnitState("one","desc one","root");
ArchitectureUnitState state2 = new ArchitectureUnitState("two","desc two","root");
ArchitectureUnit unit1 = new ArchitectureUnit("unit1");
ArchitectureUnit unit2 = new ArchitectureUnit("unit2");
state1.partOf(state2);
state1.stateOf(unit1);
state2.stateOf(unit2);
architectureUnitStateRepository.save(state1);
state1 = architectureUnitStateRepository.findByName("one");
assertEquals("two", state1.getArchitectureUnitState().getName());
assertEquals("unit1", state1.getArchitectureUnit().getName());
state2 = architectureUnitStateRepository.findByName("two");
assertNull(state2.getArchitectureUnitState());
assertEquals("unit2", state2.getArchitectureUnit().getName());
}
它确实按预期通过了,图中创建的节点似乎也表明了这一点。
请注意 assertNull(state2.getArchitectureUnitState());
成立,因为关系的方向指定为 OUTGOING。没有来自 state2 的传出 PART_OF 关系,因此将加载 none。
如果我将测试更改为
@Test
public void testArchitectureBothWays() {
ArchitectureUnitState state1 = new ArchitectureUnitState("one","desc one","root");
ArchitectureUnitState state2 = new ArchitectureUnitState("two","desc two","root");
ArchitectureUnit unit1 = new ArchitectureUnit("unit1");
ArchitectureUnit unit2 = new ArchitectureUnit("unit2");
state1.partOf(state2);
state2.partOf(state1);
state1.stateOf(unit1);
state2.stateOf(unit2);
architectureUnitStateRepository.save(state1);
state1 = architectureUnitStateRepository.findByName("one");
assertEquals("two", state1.getArchitectureUnitState().getName());
assertEquals("unit1", state1.getArchitectureUnit().getName());
state2 = architectureUnitStateRepository.findByName("two");
assertEquals("one",state2.getArchitectureUnitState().getName());
assertEquals("unit2", state2.getArchitectureUnit().getName());
}
然后我们在两个方向上都有关系,现在 state2 与 state1 有关系。
我正在使用 Spring Data Neo4j 4.0.0 和 Neo4j 2.2.1,我正在尝试在具有完全相同标签的两个节点之间创建关系。
所以,我有一个 NodeEntity class,我有一个内部变量,其类型与 class 本身相同,并将其注释为 Relationship。 但是,当我使用存储库对象的 save() 方法将对象保存到数据库时,无法创建关系。
提前致谢,非常感谢您的建议!
编辑
这里是节点实体classes
public class ArchitectureUnitState extends UnitState {
public ArchitectureUnitState()
{
super();
}
public ArchitectureUnitState(String name, String description, String parentArchitectureUnitName)
{
super(name, description);
this.parentArchitectureUnitName = parentArchitectureUnitName;
}
@Relationship(type="PART_OF", direction = Relationship.OUTGOING)
private ArchitectureUnitState architectureUnitState;
@Relationship(type="STATE_OF", direction = Relationship.OUTGOING)
private ArchitectureUnit architectureUnit;
@Transient
private String parentArchitectureUnitName;
public void partOf(ArchitectureUnitState architectureUnitState) {
this.architectureUnitState = architectureUnitState;
}
public void stateOf(ArchitectureUnit architectureUnit) {
this.architectureUnit = architectureUnit;
}
public void childOf(String parentArchitectureUnitName) {
this.parentArchitectureUnitName = parentArchitectureUnitName;
}
public String getParentName() {
return parentArchitectureUnitName;
}
}
@NodeEntity
public class UnitState {
@GraphId
protected Long id;
private String name;
private String description;
public UnitState() {
}
public UnitState(String name, String description) {
this.name = name;
this.description = description;
}
public void setName(String name) {
this.name = name;
}
public void setDescription(String description) {
this.description = description;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
}
因此,顺序是:我创建了 ArchitectureUnitState 对象,将一个对象映射到另一个对象,然后使用 ArchitectureUnitStateRepository 的 save() 方法保存。
如果我这样做,PART_OF 关系不会创建,尽管我在调试中看到值在那里。
我现在的解决方法是先保存所有 ArchitectureUnitState 节点,再次从数据库中检索它们,将它们映射到另一个,然后再次保存。这样就可以创建关系了,但是我需要保存两次。
这是我使用上面的 类 的测试用例。
@Test
public void testArchitectureState() {
ArchitectureUnitState state1 = new ArchitectureUnitState("one","desc one","root");
ArchitectureUnitState state2 = new ArchitectureUnitState("two","desc two","root");
ArchitectureUnit unit1 = new ArchitectureUnit("unit1");
ArchitectureUnit unit2 = new ArchitectureUnit("unit2");
state1.partOf(state2);
state1.stateOf(unit1);
state2.stateOf(unit2);
architectureUnitStateRepository.save(state1);
state1 = architectureUnitStateRepository.findByName("one");
assertEquals("two", state1.getArchitectureUnitState().getName());
assertEquals("unit1", state1.getArchitectureUnit().getName());
state2 = architectureUnitStateRepository.findByName("two");
assertNull(state2.getArchitectureUnitState());
assertEquals("unit2", state2.getArchitectureUnit().getName());
}
它确实按预期通过了,图中创建的节点似乎也表明了这一点。
请注意 assertNull(state2.getArchitectureUnitState());
成立,因为关系的方向指定为 OUTGOING。没有来自 state2 的传出 PART_OF 关系,因此将加载 none。
如果我将测试更改为
@Test
public void testArchitectureBothWays() {
ArchitectureUnitState state1 = new ArchitectureUnitState("one","desc one","root");
ArchitectureUnitState state2 = new ArchitectureUnitState("two","desc two","root");
ArchitectureUnit unit1 = new ArchitectureUnit("unit1");
ArchitectureUnit unit2 = new ArchitectureUnit("unit2");
state1.partOf(state2);
state2.partOf(state1);
state1.stateOf(unit1);
state2.stateOf(unit2);
architectureUnitStateRepository.save(state1);
state1 = architectureUnitStateRepository.findByName("one");
assertEquals("two", state1.getArchitectureUnitState().getName());
assertEquals("unit1", state1.getArchitectureUnit().getName());
state2 = architectureUnitStateRepository.findByName("two");
assertEquals("one",state2.getArchitectureUnitState().getName());
assertEquals("unit2", state2.getArchitectureUnit().getName());
}
然后我们在两个方向上都有关系,现在 state2 与 state1 有关系。