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.

Spring Boot OGM - Relationship is not getting mapped in NodeEntity

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:

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

	@Property(name="ShortName")
	private String ShortName;

	@Property(name="LongName")
	private String LongName;

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

	public Product() {

	}

	public Product(String shortName)
	{
		this.ShortName = shortName;
	}
        
       //STANDARD SETTERS/GETTERS HERE

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

Here is the repository code:

@RepositoryRestResource(collectionResourceRel = "Product", path = "Product")
@Repository
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?

5 REPLIES 5

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)

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.

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.

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?

If you don't care about performance, here is a heavy, loose search:

MATCH (n) WHERE n.name =~ ('(?i).*'+{query}+'.*')
WITH n
// e.g., compile and return a list of distinct Products found directly or by traversal from a matching non-Product node

Obviously you can use the more efficient comparison functions as well, or identify the subset of node labels you care about.