当我更新单个现有Neo4j RelationshipEntity
的属性时,相同类型的各种其他丰富关系的实际值突然被其旧的,以前的值状态所取代。似乎回滚了一个事务。我的期望是只更新受检查的RelationshipEntity
的属性,并且所有其他关系都不受影响。
根据图模式的简化用例可归纳如下:
ClassC
节点ClassB
(通过ClassA
)ClassB
创建一个ClassD
包括与ClassB
的关系和丰富的关系CDMapping
到ClassC
)完整描述的块在第一次运行时工作正常。设置RelationshipEntitys
和ClassC
之间的各种ClassD
,其属性为“Default Value”,最后一个丰富的关系接收意图值“Special Value”。
当谈到第二个块周期时 - 恰好通过在RelationshipEntity
和ClassC
之间保存第一个新的ClassD
- 前一次运行的属性是意外设置回“默认值”,取代原来的“特殊值”。
此时由Neo4j执行以下查询,由相关的cdMappingDAO.save(cdMapping);
触发:
UNWIND {rows} as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId CREATE (startNode)-[rel:`MAPS_TO`]->(endNode) SET rel += row.props RETURN row.relRef as ref, ID(rel) as id, {type} as type with params {type=rel, rows=[{startNodeId=91, relRef=-45, endNodeId=115, props={attribute=Default Value}}]}
UNWIND {rows} as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`CONTAINS`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, {type} as type with params {type=rel, rows=[{startNodeId=88, relRef=-49, endNodeId=115, props={}}, {startNodeId=92, relRef=-51, endNodeId=91, props={}}, {startNodeId=88, relRef=-54, endNodeId=93, props={}}, {startNodeId=89, relRef=-56, endNodeId=94, props={}}, {startNodeId=92, relRef=-57, endNodeId=90, props={}}]}
UNWIND {rows} as row MATCH (n) WHERE ID(n)=row.nodeId SET n:`ClassA`:`Entity` SET n += row.props RETURN row.nodeId as ref, ID(n) as id, {type} as type with params {type=node, rows=[{nodeId=92, props={name=Class A}}]}
UNWIND {rows} as row MATCH (n) WHERE ID(n)=row.nodeId SET n:`ClassB`:`Entity` SET n += row.props RETURN row.nodeId as ref, ID(n) as id, {type} as type with params {type=node, rows=[{nodeId=88, props={name=Class B1}}, {nodeId=89, props={name=Class B2}}]}
UNWIND {rows} as row MATCH (n) WHERE ID(n)=row.nodeId SET n:`ClassD`:`Entity` SET n += row.props RETURN row.nodeId as ref, ID(n) as id, {type} as type with params {type=node, rows=[{nodeId=115, props={}}, {nodeId=93, props={}}, {nodeId=94, props={}}]}
UNWIND {rows} as row MATCH (n) WHERE ID(n)=row.nodeId SET n:`ClassC`:`Entity` SET n += row.props RETURN row.nodeId as ref, ID(n) as id, {type} as type with params {type=node, rows=[{nodeId=90, props={name=Class C1}}, {nodeId=91, props={name=Class C2}}]}
UNWIND {rows} AS row MATCH ()-[r]-() WHERE ID(r) = row.relId SET r += row.props RETURN ID(r) as ref, ID(r) as id, {type} as type with params {rows=[{relId=104, props={attribute=Default Value}}, {relId=106, props={attribute=Default Value}}], type=rel}
您能否请给我一个建议,为什么以及通过什么组件重置属性值?我怎样才能确保被检查的RelationshipEntity
仅更新?非常感谢您指出我正确的方向!
@Component
public class GraphHandler implements CommandLineRunner {
private ClassADAO classADAO;
private ClassBDAO classBDAO;
private ClassCDAO classCDAO;
private ClassDDAO classDDAO;
private CDMappingDAO cdMappingDAO;
private SessionFactory sessionFactory;
@Autowired
public GraphHandler(ClassADAO classADAO, ClassBDAO classBDAO, ClassCDAO classCDAO, ClassDDAO classDDAO, CDMappingDAO cdMappingDAO, SessionFactory sessionFactory) {
this.classADAO = classADAO;
this.classBDAO = classBDAO;
this.classCDAO = classCDAO;
this.classDDAO = classDDAO;
this.cdMappingDAO = cdMappingDAO;
this.sessionFactory = sessionFactory;
}
public void run(String... args) {
createInitialModel();
runUseCase();
}
private void createInitialModel() {
ClassA classA = new ClassA("Class A");
ClassB classB1 = new ClassB("Class B1");
ClassB classB2 = new ClassB("Class B2");
ClassC classC1 = new ClassC("Class C1");
ClassC classC2 = new ClassC("Class C2");
classA.getClassBs().addAll(Arrays.asList(classB1, classB2));
classA.getClassCs().addAll(Arrays.asList(classC1, classC2));
classADAO.save(classA);
classBDAO.save(classB1);
classBDAO.save(classB2);
classCDAO.save(classC1);
classCDAO.save(classC2);
}
private void runUseCase() {
Iterable<ClassC> classCs = classCDAO.findAll();
for (ClassC classC : classCs) {
ClassD rememberedClassD = null;
List<ClassB> classBs = classBDAO.findClassBSelection(classC.getId());
for (ClassB classB : classBs) {
ClassD classD = new ClassD();
classD.setClassB(classB);
classB.getClassDs().add(classD);
classDDAO.save(classD);
rememberedClassD = classD;
CDMapping cdMapping = new CDMapping(classC, classD, "Default Value");
cdMappingDAO.save(cdMapping); // <-- here the problem occurs
}
// choosing the last created relationship (ClassC-ClassD) and mark it
CDMapping relationship = cdMappingDAO.getRelationshipByClassCAndClassD(classC.getId(), rememberedClassD.getId());
relationship.setAttribute("Special Value");
cdMappingDAO.save(relationship);
}
}
}
@RelationshipEntity(type = "MAPS_TO")
public class CDMapping {
@Id
@GeneratedValue
private Long id;
@StartNode
private ClassC classC;
@EndNode
private ClassD classD;
private String attribute;
public CDMapping(ClassC classC, ClassD classD, String attribute) {
this.classC = classC;
this.classD = classD;
this.attribute = attribute;
classC.getCdMappings().add(this);
classD.getCdMappings().add(this);
}
// default constructor, getter and setter here
}
@NodeEntity
public class ClassA extends Entity {
private String name;
@Relationship(type = "CONTAINS")
private List<ClassC> classCs = new ArrayList<>();
@Relationship(type = "MAPS_TO")
private List<ClassB> classBs = new ArrayList<>();
// default constructor, getter and setter here
}
@NodeEntity
public class ClassB extends Entity {
private String name;
@Relationship(type = "MAPS_TO", direction = Relationship.INCOMING)
private List<ClassA> classAs = new ArrayList<>();
@Relationship(type = "CONTAINS")
private List<ClassD> classDs = new ArrayList<>();
// default constructor, getter and setter here
}
@NodeEntity
public class ClassC extends Entity {
private String name;
@Relationship(type = "CONTAINS", direction = Relationship.INCOMING)
private ClassA classA;
@Relationship(type = "MAPS_TO")
private List<CDMapping> cdMappings = new ArrayList<>();
// default constructor, getter and setter here
}
@NodeEntity
public class ClassD extends Entity {
@Relationship(type = "CONTAINS", direction = Relationship.INCOMING)
private ClassB classB;
@Relationship(type = "MAPS_TO", direction = Relationship.INCOMING)
private List<CDMapping> cdMappings = new ArrayList<>();
// default constructor, getter and setter here
}
@Repository
public interface CDMappingDAO extends Neo4jRepository<CDMapping, Long> {
@Query("MATCH (classC:ClassC)-[relationship:MAPS_TO]-(classD:ClassD) WHERE id(classC)={classCId} AND id(classD)={classDId} RETURN classC, relationship, classD;")
CDMapping getRelationshipByClassCAndClassD(@Param("classCId") Long classCId, @Param("classDId") Long classDId);
}
@Repository
public interface ClassADAO extends Neo4jRepository<ClassA, Long> {
}
除了第一个Neo4jRepository
类型,ClassCDAO
和ClassDDAO
是相同的。
@Repository
public interface ClassBDAO extends Neo4jRepository<ClassB, Long> {
@Query("MATCH (classC:ClassC)<-[:CONTAINS]-(:ClassA)-[:MAPS_TO]->(classB:ClassB) WHERE id(classC)={classCId} RETURN classB;")
List<ClassB> findClassBSelection(@Param("classCId") Long classCId);
}
目前在OGM中似乎有一个错误,但我找到了两个解决您问题的方法。
解决方法之一就是将代码保留原样:
改变runUseCase
一个在你的GraphHandler
中以保存深度为0的CDMapping
:
private void runUseCase() {
Iterable<ClassC> classCs = classCDAO.findAll();
for (ClassC classC : classCs) {
ClassD rememberedClassD = null;
List<ClassB> classBs = classBDAO.findClassBSelection(classC.getId());
for (ClassB classB : classBs) {
ClassD classD = new ClassD();
classD.setClassB(classB);
classB.getClassDs().add(classD);
classDDAO.save(classD);
rememberedClassD = classD;
CDMapping cdMapping = new CDMapping(classC, classD, "Default Value");
cdMappingDAO.save(cdMapping, 0); // <-- here the problem occurs
}
// choosing the last created relationship (ClassC-ClassD) and mark it
CDMapping relationship = cdMappingDAO.getRelationshipByClassCAndClassD(classC.getId(), rememberedClassD.getId());
relationship.setAttribute("Special Value");
cdMappingDAO.save(relationship, 0);
}
}
这使得“特殊”的完整。
我宁愿建议以下更改,添加一些更正确的事务边界。
引入像SomeGraphBasedService
这样的服务类。由于Springs声明式事务的工作原理,因此必须使用专用类。这两种方法,createInitialModel
和runUseCase
现在每个都有一个事务,所有DAO方法都参与其中。请特别注意评论,我只保存createInitialModel
中最顶级的父类:
import java.util.Arrays;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class SomeGraphBasedService {
private final ClassADAO classADAO;
private final ClassBDAO classBDAO;
private final ClassCDAO classCDAO;
private final ClassDDAO classDDAO;
private final CDMappingDAO cdMappingDAO;
public SomeGraphBasedService(ClassADAO classADAO, ClassBDAO classBDAO,
ClassCDAO classCDAO, ClassDDAO classDDAO, CDMappingDAO cdMappingDAO) {
this.classADAO = classADAO;
this.classBDAO = classBDAO;
this.classCDAO = classCDAO;
this.classDDAO = classDDAO;
this.cdMappingDAO = cdMappingDAO;
}
@Transactional
public void createInitialModel() {
ClassA classA = new ClassA("Class A");
ClassB classB1 = new ClassB("Class B1");
ClassB classB2 = new ClassB("Class B2");
ClassC classC1 = new ClassC("Class C1");
ClassC classC2 = new ClassC("Class C2");
classA.getClassBs().addAll(Arrays.asList(classB1, classB2));
classB1.getClassAs().add(classA);
classB2.getClassAs().add(classA);
classA.getClassCs().addAll(Arrays.asList(classC1, classC2));
classADAO.save(classA);
// No need to save them one by one, the releationships
// take care of that while saving the parent class at the top
/*
classBDAO.save(classB1);
classBDAO.save(classB2);
classCDAO.save(classC1);
classCDAO.save(classC2);
*/
}
@Transactional
public void runUseCase() {
Iterable<ClassC> classCs = classCDAO.findAll();
for (ClassC classC : classCs) {
ClassD rememberedClassD = null;
List<ClassB> classBs = classBDAO.findClassBSelection(classC.getId());
for (ClassB classB : classBs) {
ClassD classD = new ClassD();
classD.setClassB(classB);
classB.getClassDs().add(classD);
classDDAO.save(classD);
rememberedClassD = classD;
CDMapping cdMapping = new CDMapping(classC, classD, "Default Value");
cdMappingDAO.save(cdMapping); // <-- here the problem occurs
}
// choosing the last created relationship (ClassC-ClassD) and mark it
CDMapping relationship = cdMappingDAO
.getRelationshipByClassCAndClassD(classC.getId(), rememberedClassD.getId());
relationship.setAttribute("Special Value");
cdMappingDAO.save(relationship);
}
Iterable<CDMapping> f = cdMappingDAO.findAll();
for (CDMapping ff : f) {
System.out.println(ff);
}
}
}
然后您的命令行运行器变为:
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class GraphHandler implements CommandLineRunner {
private final SomeGraphBasedService someGraphBasedService;
public GraphHandler(SomeGraphBasedService someGraphBasedService) {
this.someGraphBasedService = someGraphBasedService;
}
public void run(String... args) {
this.someGraphBasedService.createInitialModel();
this.someGraphBasedService.runUseCase();
}
}
虽然第一个显然是一种解决方法,但另一个解决方案是我在现实世界场景中更喜欢的解决方案。
无论如何,两者的输出现在都符合预期:
CDMapping{id=21, classC=ClassC{name='Class C1'}, classD=ClassD{classB=ClassB{name='Class B1'}}, attribute='Default Value'}
CDMapping{id=23, classC=ClassC{name='Class C1'}, classD=ClassD{classB=ClassB{name='Class B2'}}, attribute='Special Value'}
CDMapping{id=25, classC=ClassC{name='Class C2'}, classD=ClassD{classB=ClassB{name='Class B1'}}, attribute='Default Value'}
CDMapping{id=27, classC=ClassC{name='Class C2'}, classD=ClassD{classB=ClassB{name='Class B2'}}, attribute='Special Value'}