I have a entity that contains other entities. I'm getting data from ProductDTO, and i need get ids (each id can change or don't) and set it to entity.
In my Service i get the actual product, get each entity by id and set in product entity. But don't seems be the best approach fetch each entity on respective repository.
My entity:
@Entity
public class Product implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Status status;
private Location location;
private Provider provider;
}
My DTO:
public class ProductDTO {
private Long statusId;
private Long locationId;
private Long providerId;
}
My service:
Product p = productRepository.findById(id)
.orElseThrow(() -> new EntityNotFoundException("Product ID " + id + " not
found."));
var status = userRepo.findById(dto.getStatusId());
var location = locationRepo.findById(dto.getLocationId());
var provider = providerRepo.findById(dto.getProviderId());
p.setStatus(status);
p.setLocation(location);
p.setProvider(provider);
There is a best aprroach to update a entity that contains others entities without fetch each one?
Thanks.