How to CRUD @RelationshipEntity in SDN 4.0

爷,独闯天下 提交于 2019-12-11 12:16:09

问题


If I have a rich relationship entity, for example

@NodeEntity
public class Actor {
    Long id;
    private Role playedIn;
}

@RelationshipEntity(type="PLAYED_IN")
public class Role {
    @GraphId   private Long relationshipId;
    @Property  private String title;
    @StartNode private Actor actor;
    @EndNode   private Movie movie;
}

@NodeEntity
public class Movie {
    private Long id;
    private String title;
}

To CRUD both @NodeEntity, just simply create a @Repository each, for example

@Repository
public interface ActorRepository extends GraphRepository<Actor>{

}

to do CRUD are simply

@Autowired
ActorRepository actorRepository

actorRepository.save(new Actor(....))

My question is, how we do CRUD for the @RelationshipEntity Role ?

Are we create one @Repository for Role? (I tried, it is not working)


回答1:


MicTech is right.

A RelationshipEntity is represented by an edge in the graph, not a node, and currently Repository implementations are applicable only for objects which can be persisted as nodes. This should not cause you any problems.

The OGM will persist all reachable objects from the one you explicitly save (unless you tell it not to). This behaviour means that edges between connected objects are created/updated automatically whenever you save a NodeEntity, regardless of whether those edges are explicitly represented by a RelationshipEntity or implicitly by direct references between NodeEntity instances.

The OGM's session object is slightly less restrictive than when using SDN's Repository methods in that you can invoke

session.save(...)

on an object annotated as a RelationshipEntity, and it will do as you expect. But, there is actually no need to do this: saving either the start or end node of a RelationshipEntity will ensure the graph is correctly persisted.




回答2:


SDN does CRUD for @RelationshipEntity automatically, because you aren't able to store Relationship in Neo4j without start and end node.



来源:https://stackoverflow.com/questions/32621464/how-to-crud-relationshipentity-in-sdn-4-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!