package com.example.spring.bank.customer; import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.example.spring.bank.account.Account; import jakarta.transaction.Transactional; @Service public class CustomerManagerImpl implements CustomerManager { @Autowired private CustomerRepository customerRepository; @Override @Transactional public Customer create(Customer customer) { return customerRepository.save(customer); } @Override public Optional findById(Long customerId) { return customerRepository.findById(customerId); } @Override public Iterable findAll() { return customerRepository.findAll(); } @Transactional @Override public void addAccount(Customer customer, Account account) { // May not work is the customer is detached. // customer.getAccounts().add(account); // Access the Customer again - so that it would not be a detached entity. customerRepository.findById(customer.getId()).get().getAccounts().add(account); } // Other methods }