Hi, full disclosure, I am new to Neo4j. As a learning tool I am rewriting an app to use Neo4j with a view to leveraging it’s functionality for a reccommendation tool and eventually a machine learning engine. To that end I would like to implement a relationship between two nodes as follows.
What I'd like to do is to add one or more properties to the HAS_INGREDIENT relationship. For example quantity, such that Recipe is 'Pancakes' which has 'milk' as an Ingredient and the relationship attribute quantity is '200ml'. What I want to avoid is having to introduce another Node, Recipe_Ingredient, requiring a link to both Recipe and Ingredient.
package com.example.demo.recipe;
import org.springframework.data.annotation.Id;
import org.springframework.data.neo4j.core.schema.Node;
import org.springframework.data.neo4j.core.schema.Property;
import org.springframework.data.neo4j.core.schema.Relationship;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
@Node("Recipe")
public class RecipeEntity {
@Id
private String recipeId;
@Property
private String name;
@Property
private String description;
@Relationship(type = "HAS_INGREDIENT", direction = Relationship.Direction.OUTGOING)
private ArrayList<IngredientEntity> ingredients;
where IngredientEntity is defined as ...
package com.example.demo.recipe;
import org.springframework.data.neo4j.core.schema.Id;
import org.springframework.data.neo4j.core.schema.Node;
import org.springframework.data.neo4j.core.schema.Property;
import org.springframework.stereotype.Component;
import java.util.UUID;
@Component
@Node("Ingredient")
public class IngredientEntity implements Ingredient{
@Id
private String id;
@Property
private String name;
//default no arg constructor
public IngredientEntity() {}
//Constructor
public IngredientEntity(String name, int quantity, String unit) {
this.id = UUID.randomUUID().toString();
this.name = name;
}
@Override
public String getId() {
return id.toString();
}
@Override
public String getName() {
return name;
}
public String toString() {
return "Ingredient{id='" + id + "', name='" + name + "'}";
}
}
I have seen in older versions of the Neo4j driver code, the ability to add an attribute to relationship class using properties="<some value>" Firstly I'd like to know if this is still possible, secondly is there an example tutorial showing how to build the Relationship Property in Java, Spring-boot.
Thanks in advance for any help
FYI these are the imports from my POM
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-neo4j</artifactId>
<version>3.5.6</version>
</dependency>
<!-- Neo4j Driver -->
<!--dependency>
<groupId>org.neo4j.driver</groupId>
<artifactId>neo4j-java-driver</artifactId>
</dependency -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-docker-compose</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
</dependencies>