The company I work for is converting over to Spring Data and most of us are still learning. I keep running into a problem that I cant quite wrap my head around. How to handle saving or updating an entity that has a relationship to detached entities where values in those entities might need to change.
I have service, rate_plan, and service_rate_plan tables. the rate_plan table joins the service and rate_plan tables and it has a column for the cost of the service in that rate plan.
Lets assume that a user wants to create a Rate Plan. I create a Rate Plan DTO and add to it a list of all possible serviceRatePlan. I present this to the user. The user then adds information for the Rate Plan and sets cost for 1 or more of the services and submits the form.
Now I have a Rate Plan DTO with some information filled in and a list of serviceRatePlan that may or may not have a cost.
For an update the current practice at my company is fetch a list of the serviceRatePlan and iterating it and the new list updating the existing list with the new information.
That just doesn't seem right to me. If the service list of was 10 items I guess it might be ok. But if the list was 1000 items or something like that it would, seem to me, to be really inefficient.
So what is the correct, most efficient, way of doing this?
This is an example of how I am currently merging i
protected void buildServiceListFromDto(RatePlan ratePlan, List<RatePlanServiceClassDto> serviceClasses)
{
Set<ServiceRatePlan> servicesForRatePlan = new HashSet<>();
ratePlan.setServicesForRatePlan(servicesForRatePlan);
if (serviceClasses != null)
{
for (RatePlanServiceClassDto serviceClassDto : serviceClasses)
{
if (serviceClassDto.getServices() != null)
{
for (RatePlanServiceDto serviceDto : serviceClassDto.getServices())
{
/*
* Looking at the servrate table it looks as though empty values are not stored.
*/
if (StringUtils.isNotEmpty(serviceDto.getAmount().toString()))
{
ServiceRatePlan serviceRatePlan = new ServiceRatePlan();
serviceRatePlan.setAmount(serviceDto.getAmount().getValue());
serviceRatePlan.setRatePlan(ratePlan);
Service service = serviceRepository.findOne(serviceDto.getNumber());
serviceRatePlan.setService(service);
servicesForRatePlan.add(serviceRatePlan);
}
}
}
}
}
}