I am using spring data neo4j version 4.1.2 with bolt driver 2.0.6. I am facing issue of breaking relationship between entities automatically.
I have an domain named organization and organization belongs to many entities but i am mentioning few.
Organization schema
public class Organization {
private String formalName;
private String shortName;
@Relationship(type = COUNTRY)
private Country country;
@Relationship(type = HAS_GROUP)
private List<Group> groupList = new ArrayList<>();
}
And country schema is
public class Country {
@Relationship(type = HAS_HOLIDAY)
private List< CountryHolidayCalender> countryHolidayCalenderList;
}
When i am updating organization it breaks all relationship of country like breaks country-holiday relationship etc
Organization update code
Organization unit = organizationGraphRepository.findOne(unitId);
unit.setFormalName(organizationGeneral.getFormalName());
unit.setShortName(organizationGeneral.getShortName());
organizationGraphRepository.save(unit);
I am unable to find, what i am doing wrong. Please help me, we can't afford this kind of bug at this time.
Thanks
The default depth for loading entities is 1. Which means that
findOne(id)
will only return the given entity + its direct connected entities. In your caseorganization --> country --> CountryHolidayCalender
, only organization and related country will be loaded.That's maybe why you are not seeing the attached nodes.
save
tracks object states, to detect changes and optimize the updates. There will be no database update for objects / properties that didn't change betweenfind
andsave
.So, as long as you don't touch at
@Relationship
annotated attributes, you can save safely, they will not be modified even if they were not loaded.Regarding your previous comment, there is no lazy loading in SDN for dynamic depth retrieval (existed, was deprecated). So, yes, you have to manage the depth yourself. If your volume of data is not too big, your might want to load everything at once (with
find(id, -1)
). Another option could be to load on-demand, manually querying missing pieces of graph.You may also be interested in this ticket for finer control on entity loading.