Spring Data Integration Test OneToMany ManyToOne
I know this topic is not new, but I have been trying to solve the problem for several days. I have 3 classes - customer, account and invoice. The client has many Accunts, Uchet has many invoices. I mapped them out:
Client
@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<Account> accounts;
...
}
Score
@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<Invoice> invoices;
...
}
Invoice
@Entity
@Table(name = "INVOICE")
public final class Invoice implements Serializable {
@ManyToOne
@JoinColumn(name = "account_uuid", referencedColumnName = "uuid", nullable = false)
private Account account;
}
I am using Spring Data Jpa:
@Repository
public interface SpringDataClientRepository extends ClientRepository, JpaRepository<Client, Integer> {
Others.
When I try to run ITests, check that the client is working fine:
@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());
Saves client and account. But this test:
@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());
crash:
Caused by: org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation : projects.model.Invoice.account -> projects.model.Account
I want all invoices to be saved if I keep an account of those invoices. It's the same with the client - all accounts will be saved if I save them.
How do I do it?
+3
source to share
1 answer
I did one way communication and composition:
Client
@Entity
@Table(name = "CLIENT")
public final class Client extends BaseEntity {
@Column(length = 36)
private String uuid;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
@JoinColumn(name = "client_id", referencedColumnName = "id")
private List<Account> accounts;
}
Invoice
@Entity
@Table(name = "INVOICE")
public final class Invoice extends BaseEntity {
@Column(length = 36)
private String uuid;
}
Score
@Entity
@Table(name = "ACCOUNT")
public final class Account extends BaseEntity {
@Column(length = 36)
private String uuid;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
@JoinColumn(name = "account_id", referencedColumnName = "id")
private List<Invoice> invoices;
}
In the test, everything I left as it was. Everything is working
+1
source to share