cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Storing unique node with spring boot

Hi this is my Receipt entity which contains Unit object with the following properties. My problem is that I dont want to create duplicate Unit nodes each time new Receipt is created instead just connect this Receipt to the already stored Unit with same properties.

Now I am using default save method inherited from Neo4jRepository class, but I would like to create query to accomplish that requirement I described above. Thanks.

@NodeEntity
public class Receipt {

    @Id
    @GeneratedValue
    private Long id;

    private Long ownerID;

    private String friendlyName;

    private String shop;

    private String createdDateTime;

    @Relationship("CONTAINS")
    private ArrayList<Item> items;

    @Relationship("ISSUED_AT")
    private Unit unit;

    private Double totalPrice;
@NodeEntity
public class Unit {

    @Id
    @GeneratedValue
    private Long id;

    private String buildingNumber;

    private String municipality;

    private String postalCode;

    private String country;

    private String street;
1 ACCEPTED SOLUTION

amsilf
Node Clone

The easiest way I can think of is to create custom search query (see https://neo4j.com/developer/spring-data-neo4j/, section "Repository for Queries") that will return existing Unit id if unit does exists or null in other case; each time you save receipt you have to check if Unit exists, and if it does pass reference to the Unit into the Receipt.

View solution in original post

2 REPLIES 2

amsilf
Node Clone

The easiest way I can think of is to create custom search query (see https://neo4j.com/developer/spring-data-neo4j/, section "Repository for Queries") that will return existing Unit id if unit does exists or null in other case; each time you save receipt you have to check if Unit exists, and if it does pass reference to the Unit into the Receipt.

Thanks for quick response.