jasonfungsing
jasonfungsing

Reputation: 1655

How to CRUD @RelationshipEntity in SDN 4.0

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)

Upvotes: 1

Views: 184

Answers (2)

Vince
Vince

Reputation: 2181

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.

Upvotes: 3

MicTech
MicTech

Reputation: 45143

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

Upvotes: 1

Related Questions