I am trying to build the following scenario using Neo4J SDN. There is an entity Person who has a list of Item entities. Each Item entity can have one or more child Items. I wish to have bidirectional relationships between a Person and the list of Items and another bidirectional relationship between Parent Item and Child Items. Till now, a parent Item can have a list of another set of child Items but each of the child items does not have any children under it.
I have tried to build the domain objects and relationships as
@NodeEntity
public class Person {
@GraphId
private Long id;
private String name;
private String email;
@Relationship(type = "ITEM_CREATED_BY",direction = Relationship.INCOMING)
List<PersonItemRelation> personItemRelations = new ArrayList<PersonItemRelation>();
}
@NodeEntity
public class Item {
@GraphId
Long id;
String uuid;
String name;
@Relationship(type = "ITEM_CREATED_BY",direction = Relationship.OUTGOING)
private PersonItemRelation personItemRelation ;
@Relationship(type = "IS_CHILD_ITEM_OF",direction = Relationship.OUTGOING)
private ItemParentChildRelation parentItemRelation;
@Relationship(type = "IS_CHILD_ITEM_OF",direction = Relationship.INCOMING)
private List<ItemParentChildRelation> subItemRelations = new ArrayList<ItemParentChildRelation>();
}
@RelationshipEntity(type = "IS_CHILD_ITEM_OF")
public class ItemParentChildRelation {
@GraphId
Long id;
@DateLong
Date creationDate;
@StartNode
Item childItem;
@EndNode
Item parentItem;
}
@RelationshipEntity(type = "ITEM_CREATED_BY")
public class PersonItemRelation implements Serializable {
@GraphId
Long id;
@Property
String uuid;
@StartNode
Item item;
@EndNode
Person person;
@Property
String status;
@DateLong
Date date;
}
All of the above entities have required getters and setters with proper annotations as well.
My first question is, is this the right way of implementing the model.
If so, I am facing some problems with it. If I want to update a subItem (ie an Item which is a child of a parent Item), then sometimes, the relation between the parent Item and the child Item is lost.
The way I performed the update is given below
public void deactivateItem(String itemUuid){
Item item=itemRepository.findActiveByUuid(itemUuid);
if(item != null){
item.setStatus("INACTIVE");
itemRepository.saveItem(item);
}
}
The function findActiveByUuid(String itemUuid) is implemented in ItemRepository as
@Query("MATCH (p:Item {status:'ACTIVE'}) "
+ "where "
+ "(p.uuid={0}) "
+ "return p")
Item findActiveByUuid(String uuid);
One more thing which I noticed was that if I am fetching data using cypher, as indicated above, the depth of the query is 0. This is indicated by the fact when I am trying to get the size of subItemRelations from the Item object, the result is zero.
But if I am fetching using spring-data built-in functions like
Item findByUuid(String uuid);
then the size of subItemRelations is coming correctly (>0 in cases where an Item has one or more child Items)
I am using spring-data-neo4j 4.1.2.RELEASE and Neo4J 3.0.3 Community Edition