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;

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.

1 Like

Thanks for quick response.