title: Spring Data with Integration Test OneToMany ManyToOne
body: <p>I know that this topic is not new, but I few days try to solve task.
  I have 3 classes - Client, Account and Invoice.
  Client has many Accoutns, Account has many Invoices.
  I mapped them:<br/>
  <strong>Client</strong></p>
  
  <pre><code>@Entity
  @Table(name = "CLIENT")
  public final class Client implements Serializable {
  ...
      @Column(length = 36, nullable = false, unique = true, updatable = false)
      private String uuid;
  
      @OneToMany(mappedBy = "client", cascade = CascadeType.ALL)
      private List&lt;Account&gt; accounts;
  ...
  }
  </code></pre>
  
  <p><strong>Account</strong></p>
  
  <pre><code>@Entity
  @Table(name = "ACCOUNT")
  public final class Account implements Serializable {
  ...
      @ManyToOne
      @JoinColumn(name = "client_uuid", referencedColumnName = "uuid", nullable = false)
      private Client client;
  
      @OneToMany(mappedBy = "account", cascade = CascadeType.ALL)
      private List&lt;Invoice&gt; invoices;
  ...
  }
  </code></pre>
  
  <p><strong>Invoice</strong></p>
  
  <pre><code>@Entity
  @Table(name = "INVOICE")
  public final class Invoice implements Serializable {
      @ManyToOne
      @JoinColumn(name = "account_uuid", referencedColumnName = "uuid", nullable = false)
      private Account account;
  }
  </code></pre>
  
  <p>I use Spring Data Jpa:</p>
  
  <pre><code>@Repository
  public interface SpringDataClientRepository extends ClientRepository, JpaRepository&lt;Client, Integer&gt; {
  </code></pre>
  
  <p>Others the same.</p>
  
  <p>When I try to run ITests, test with client work fine:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(classes = { JpaConfig.class, ITDaoConfig.class })
  public class ITClientRepository {
  private static final Logger log = Logger.getLogger(ITClientRepository.class);
  
  @Autowired
  private ClientRepository clientRepository;
  
  @Test
  @Transactional
  public void testSaveClient() {
  log.info("testSaveClient start");
  
  Client client = new Client();
  client.setName("testSaveClient");
  client.setUuid("client-testSaveClient");
  client.setTelephone("12345679");
  
  Account account = new Account();
  account.setClient(client);
  account.setMoneyCount(10);
  account.setUuid("client-testSaveClient");
  client.addAccount(account);
  
  log.info(client.toString());
  Client getClient = clientRepository.save(client);
  log.info(client.toString());
  </code></pre>
  
  <p>It saves client and account in. But this test:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(classes = { JpaConfig.class, ITDaoConfig.class })
  public class ITAccountRepository {
      private static final Logger log = Logger.getLogger(ITAccountRepository.class);
  
      @Autowired
      private AccountRepository accountRepository;
  
      @Test
      @Transactional
      public void testSaveAccount() {
      log.info("testSaveAccount start");
  
      Client client = new Client();
      client.setName("testSaveAccount");
      client.setTelephone("12345679");
      client.setUuid("account-testSaveAccount");
      client.setId(200);
      // Client saved in db
  
      Account account = new Account();
      account.setClient(client);
      account.setMoneyCount(15);
      account.setUuid("account-testSaveAccount");
      client.addAccount(account);
  
      Invoice invoice = new Invoice();
      invoice.setAccount(account);
      invoice.setAmount(11);
      Date date = new Date();
      invoice.setCreated(date);
      invoice.setUuid("account-testSaveClient");
      invoice.setDescription("Description of invoice");
      account.addInvoice(invoice);
  
      log.info(account.toString());
      Account getAccount = accountRepository.save(account);
      log.info(account.toString());
  </code></pre>
  
  <p>fail with:</p>
  
  <pre><code> Caused by: org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation : projects.model.Invoice.account -&gt; projects.model.Account
  </code></pre>
  
  <p><strong>I want</strong> that all Invoices will be saved if I save Account of those invoices. And the same with Client - all Accounts will be saved if I save Client of them. <br/><strong>How I do this?</strong></p>