Saving/Updating Neo4j object with relationships in Spring Data Neo4j

I finally found solution by myself so lets see:

  1. There was this problem when I saved the email it was created with relation but when I queried for profile it was always null to fix you have to make your profileRepository query look like this:
    @Query("""
            MATCH (p:Profile)
            WHERE p.profileId = $profileId
            MATCH(p)-[r:HAS|BORN_ON|WORKS_AS|IS|STUDIED_AT|WENT_TO|LIVES_IN|FROM]->(e)
            RETURN p, collect(r) as rel, collect(e) as nodes
            """)
    Optional<Profile> findByProfileId(@Param(Params.PROFILE_ID) String profileId);

r - is a relationship you want to query if want only certain types you have to specify them,
e - is a target node of a some type that is connected with your node using relationship in our case email
I have no idea why I have to collect() relations and nodes and using the return as below does not work but the above wrorks.

RETURN p, e as email
// another
RETURN p, collect(e) as email
  1. Problem with the method in provided service is that when i was calling getProfileFromRepository method I did not receive relationships (they were null) so when I was calling profileRepository.save() it was removing all the relationships for my profile node. After fixing using the first query now it perfectly works.

The topic where I found a clue about the whole idea: Spring Data Neo4j 7.1.2 - relationships only return empty sets.