Ask Your Question
1

How to use @RelationshipProperties and @TargetNode to create a relationship entity in Spring Data Neo4j?

asked 2023-06-27 19:57:49 +0000

devzero gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2023-06-27 20:21:01 +0000

david gravatar image

To use @RelationshipProperties and @TargetNode to create a relationship entity in Spring Data Neo4j, follow the steps below:

  1. Create a POJO class that represents the relationship entity. This class should have the following annotations:
  • @RelationshipProperties: This annotation is used to mark a class as a relationship entity. It should be placed on top of the class definition.

  • @StartNode: This annotation is used to mark a field that represents the start node of the relationship.

  • @EndNode: This annotation is used to mark a field that represents the end node of the relationship.

  1. Define the fields of the class that represent the properties of the relationship.

  2. Implement getters and setters for all the fields.

  3. Use the relationship entity class in the domain object class to define the relationship between nodes.

For example, let's consider a simple relationship between a Person node and a Car node. We can create a relationship entity named "Owns" to represent this relationship. Here's how we can define the Owns class:

@RelationshipProperties
public class Owns {
    @Id
    @GeneratedValue
    private Long id;

    @Property
    private String ownershipType;

    @StartNode
    private Person owner;

    @EndNode
    private Car car;

    // constructors, getters and setters
}

Here, we have defined a relationship entity called "Owns" that has the following fields:

  • id: The unique ID of the relationship.

  • ownershipType: A property of the relationship that represents the type of ownership (for example, "owned" or "leased").

  • owner: A reference to the Person node that is the start node of the relationship.

  • car: A reference to the Car node that is the end node of the relationship.

We can now use the Owns relationship entity in the Person and Car domain object classes to define the relationship between them.

@Node
public class Person {
    @Id
    @GeneratedValue
    private Long id;

    @Property
    private String name;

    @Relationship(type = "OWNS")
    private List<Owns> owns = new ArrayList<>();

    // constructors, getters and setters
}

@Node
public class Car {
    @Id
    @GeneratedValue
    private Long id;

    @Property
    private String make;

    @Property
    private String model;

    @Relationship(type = "OWNS", direction = Relationship.INCOMING)
    private List<Owns> ownershipHistory = new ArrayList<>();

    // constructors, getters and setters
}

Here, we have added a List of Owns relationship entities to both the Person and Car classes to represent the relationship between them. We have also specified the type of the relationship as "OWNS" and the direction of the relationship as "INCOMING" for the Car class, since the "Owns" relationship is defined starting from the Person node.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-27 19:57:49 +0000

Seen: 9 times

Last updated: Jun 27 '23