title: JPA: Correct way to use Service and multiple Repositories
body: <p>I've made a system that use JPA and Spring. For example, If I need to handle Accounts, I use a repository:</p>
  
  <pre><code>@Repository
  public interface AccountRepository extends JpaRepository&lt;Account, Long&gt;
  </code></pre>
  
  <p>Then I create a service that use the repository:</p>
  
  <pre><code>class AccountServiceImpl implements AccountService {
  
    @Autowired
    private AccountRepository repository;
  
    @Transactional
    public Account save(Account account){
      return repository.save(account);
    }
  
  ...
  </code></pre>
  
  <p>Now, I've created a controller that handle the POST method for Accounts:</p>
  
  <pre><code>@Controller
  public class AccountController {
  
  @Autowired    
  private final accountService service;
  
  @RequestMapping(value = "/account", method = RequestMethod.POST)
          public ModelAndView account(@Valid Account account, BindingResult bindingResult) {
      ...
  service.save(account);
  </code></pre>
  
  <p>The same for Customer, Products, Contacts, etc.</p>
  
  <p>Now, let suppose that I also have a class called "registration" that containts enough data to create a customer, with his accounts, contact data and products(a lot of data). The action "confirm" to a registration is the one dedicated to do that:</p>
  
  <pre><code>@RequestMapping(value = "/confirm", method = RequestMethod.POST)
      public ModelAndView confirmRegistration(@Valid Registration registration, BindingResult bindingResult) {
  </code></pre>
  
  <p><strong>Now my question:</strong> What is the correct way to call the <strong>save</strong> method for every repository? </p>
  
  <p>1) Should I create the classes in the <strong>controller</strong> and then call the save method for every class created:</p>
  
  <pre><code>@RequestMapping(value = "/confirm", method = RequestMethod.POST)
          public ModelAndView confirmRegistration(@Valid Registration registration, BindingResult bindingResult) {
  ...
  customerService.save(customer);
  accountService.save(account);
  contactDataService.save(contactData);
  productService.save(contactData);
  ...
  </code></pre>
  
  <p>2) Call the saves of each service <strong>in the RegistrationService</strong>:</p>
  
  <pre><code>class RegistrationServiceImpl implements RegistrationService {
  
    @Autowired
    private AccountService accountService;
    @Autowired
    private CustomerService customerService;
    ....
  
    @Transactional
    public void confirm(Registration registration){
    ... here I create the object
    customerService.save(customer);
    accountService.save(account);
    }
  </code></pre>
  
  <p>3) Call the saves of each <strong>repository</strong> in the RegistrationService:</p>
  
  <pre><code>class RegistrationServiceImpl implements RegistrationService {
  
    @Autowired
    private AccountRepository accountRepository;
    @Autowired
    private CustomerRepository customerRepository;
    ....
  
    @Transactional
    public void confirm(Registration registration){
    ... here I create the object
    customerRepository.save(customer);
    accountRepository.save(account);
    }
  </code></pre>
  
  <p>I undertand if I have to use (1). But confused about option (2) and (3). </p>
  
  <p><strong>The question again:</strong> </p>
  
  <p>Should/Can I use other services in a service? Or I have to use only repositories in a service?</p>
  
  <p>What is the correct way to google the explanation of this? Sorry English is not my native language and I can't find the correct way to ask about this kind of design.</p>