Spring Boot OGM - Relationship is not getting mapped in NodeEntity

(D Modi) #1

Hello Everyone,

I am using Spring Boot with Neo4j-OGM. I am facing a problem while mapping a relationship inside @NodeEntity.

Here is what my code looks like:

public class Product {
	@Id @GeneratedValue private Long id;

	private String ShortName;

	private String LongName;

 	@Relationship(type = "RELATES",direction = Relationship.OUTGOING)
	private ProductGroup prodGroup;

	public Product() {


	public Product(String shortName)
		this.ShortName = shortName;

	public ProductGroup getProdGroup() {
		return RmsStyleGroup;
	public void setProdGroup(ProdGroup val) {
		this.prodGroup = val;

Here is the repository code:

@RepositoryRestResource(collectionResourceRel = "Product", path = "Product")
public interface ProductRepository extends Neo4jRepository<Product, Long> {

	@Query("MATCH (prod:Product{ShortName:{0}}) MATCH(prodgroup:ProductGroup)--(prod) RETURN prod, prodgroup")
    Product findByShortName(@Param("ShortName") String ShortName);
	@Query("MATCH (prod:Product {LongName:{0}}) RETURN prod")
	Collection<Product>findByLongName(@Param("LongName") String LongName);



Finally, when my controller tries to return a "Product" using one of the above services - findByLongName/findByShortName, all the properties of the Product NodeEntity reflect the values from Neo4j DB, but the ProductGroup relationship shows 'null'.

What can I do to reflect the value of the relationship(s) that I want to be there in the domain of a NodeEntity?

(Gerrit Meier) #2

You also have to return the relationships in your return statement.
For your query this should be something like
MATCH (prod:Product{ShortName:{0}})-[r:RELATES]->(prodgroup:ProductGroup)--(prod) RETURN prod, r, prodgroup
A more general version that would also work with your domain is
MATCH (prod:Product{ShortName:{0}})-[r:RELATES]->(prodgroup:ProductGroup)--(prod) RETURN prod, collect(r), collect(prodgroup)

(D Modi) #3

Thanks! It works only if I return the relationship as you describe while defining the @RelationshipEntity and @NodeEntity for each of the related node I want to store. I wonder if there is a way to do it in a more simple manner.

(Gerrit Meier) #4

Of course you could drop your custom queries and let Spring Data Neo4j work for you.
If you example above is all you want, you could go with the finder methods described in the documentation. If you do net specify any depth attribute they will always query with a depth of 1. This implies loading the node in question and one level of relationship.

(D Modi) #5

Thanks @gerrit.meier
How about combinational search with multiple parameters? I have around 10 properties at the moment that the user can use to search a Product node. These properties either belong to Product itself or its related nodes, and allow the user to find the product with any one of those.

What would be the right way to do a search with multiple search attributes?