I'm facing this problem that whenever I'm fetching a node from Neo4j using the findById( ) method of Spring JPA, it's also populating the relationships list. For example this is the node entity that I'm trying to retrieve from the database:
@Node
public class Location extends Transaction{
public final static String featureID = "12116";
public final static String featureVariantID = "000";
@Relationship("Parent")
private Set<BaseRelationship<Location>> parents;
@Relationship("Type")
private Set<BaseRelationship<LocationType>> types;
@Relationship("Workplace")
private Set<BaseRelationship<Workplace>> workplaces;
@PersistenceCreator
public Location() {
}
public Location(String transactionID, String tenantID) {
super(featureID, featureVariantID, transactionID, tenantID);
// initializing the lists
}
// Getters and Setters
private static final Map<String, String> allowedRelationships = new HashMap<>();
static {
allowedRelationships.put("Parent", "Data.ParentLocationID");
allowedRelationships.put("Type", "Data.LocationLocationTypeID");
allowedRelationships.put("Workplace", "Data.LocationWorkplaceID");
}
public Map<String, String> getAllowedRelationships() {
return Location.allowedRelationships;
}
}
The relationship class is like this:
@RelationshipProperties
public class BaseRelationship <Target extends Transaction>{
@Id
@GeneratedValue
private Long id;
@Property("EffectiveTillTimestamp")
private String effectiveTillTimestamp;
@Property("EffectiveFromTimestamp")
private String effectiveFromTimestamp;
@Property("Status")
private String status;
@TargetNode
private Target targetNode;
public BaseRelationship(String effectiveTillTimestamp, String effectiveFromTimestamp, String status, Target targetNode) {
// initializing the data members
}
public BaseRelationship() {
}
// Getters and Setters
}
So whenever I pass in the nodeID into the findByID( ) method of the repository, it also populates the types, parents and workplaces relationship lists. The objects in the list also have their relationship lists populated. According to me this is adding an extra overhead while retrieving data from Neo4j.
This is the repository interface being used and the repository interfaces of each individual node classes inherits from it.
@Repository
public interface AbstractTransactionRepository<T extends Transaction> extends Neo4jRepository<T, String> {
@Query("MATCH (n) WHERE n.NodeID= $nodeID RETURN n")
Optional<T> findOnlyNode(String nodeID);
}
I tried making use of a custom query as defined in the findOnlyNode
method in the above repository class to fetch only the node excluding its relationships. However, after creating and saving a new relationship with the fetched node as the Target Node and another newly created node, the already existing relationships of the Target Node(the one that I fetched using findOnlyNode
method) got destroyed. For example:
If earlier the structure was:
Target Node->Parent1->Parent2
After saving:
New Node->Target Node
Instead it should have been like this:
New Node->Target Node->Parent1->Parent2 (Correct)
If I just use the findById( )
method of Spring JPA then it works correctly but the time taken to fetch the node increases. So if the node has several children and those children also have other children then the time taken to fetch the node crosses 2 sec, however if the node does not have any children then then time taken to fetch the node is always less than 700 ms.
Is there a way to just fetch the node object from Neo4j without populating the relationships of that node and also save it without destroying the already exisiting relationships?