Is it okay to use multiple repositories from inside a single service implementation in Spring? I currently have two entities, Adress and Company. They both have DTOs to perform CRUD operations.

AddressCreateDto:

@Getter
@Setter
public class AddressCreateDto {

    @NotNull
    private String address1;

    private String address2;

    @NotNull
    private String city;

    @NotNull
    private String state;

    @NotNull
    private int zipCode;

    @JsonIgnore
    private final Long createdBy = 1L;

    @JsonIgnore
    private final Date createdDate = new Date();

    @JsonIgnore
    private final Long lastModifiedBy = 1L;

    @JsonIgnore
    private final Date lastModifiedDate = new Date();

}


CompanyCreateDto:

@Getter
@Setter
public class CompanyCreateDto {

    @NotNull
    private String name;

    @NotNull
    private AddressCreateDto address;

    @JsonIgnore
    private final Long createdBy = 1L;

    @JsonIgnore
    private final Date createdDate = new Date();

    @JsonIgnore
    private final Long lastModifiedBy = 1L;

    @JsonIgnore
    private final Date lastModifiedDate = new Date();

}


In my CompanyServiceImpl file, I am using both the CompanyRepository and the AddressRepository but I don't know if this is bad practice. The code works, but not sure if there is a better way of rewriting this so that it follows a specific set of conventions.

CompanyServiceImpl:

@Service
public class CompanyServiceImpl implements CompanyService {

    private final CompanyRepository companyRepo;

    private final AddressRepository addressRepo;

    @Autowired
    public CompanyServiceImpl(CompanyRepository companyRepo, AddressRepository addressRepo) {
        this.companyRepo = companyRepo;
        this.addressRepo = addressRepo;
    }

    @Override
    public CompanyReadDto create(CompanyCreateDto dto) {
        Address address = ObjectMapperUtils.map(dto.getAddress(), Address.class);
        address = addressRepo.save(address);

        Company company = ObjectMapperUtils.map(dto, Company.class);
        company.setAddress(address);

        return ObjectMapperUtils.map(companyRepo.save(company), CompanyReadDto.class);
    }

    ...

}


EDIT:

CompanyReadDto:

@Getter
@Setter
public class CompanyReadDto {

    @Id
    @NotNull
    private Long id;

    @NotNull
    private String name;

    @NotNull
    private AddressReadDto address;

}


AddressReadDto:

@Getter
@Setter
public class AddressReadDto {

    @Id
    @NotNull
    private Long id;

    @NotNull
    private String address1;

    private String address2;

    @NotNull
    private String city;

    @NotNull
    private String state;

    @NotNull
    private int zipCode;

}
Best practice to perform CRUD operations is to have this flow for an entity-

View --passing Dto--&gt; Controller -- passing Dto/Entity--&gt; Service --passing Entity--&gt; Repo

In your example you can have both repositories inside a service as both entities ( company and address ) are associated.

Also, to fall in line you can save address entity along with country entity using cascade