3
我知道这个话题并不新鲜,但是我花了几天的时间尝试解决任务。 我有3个课程 - 客户,帐户和发票。 客户有很多Accoutns,帐户有很多发票。 我映射他们:
客户Spring Data with Integration Test OneToMany ManyToOne
@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;
...
}
帐户
@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;
...
}
发票
@Entity
@Table(name = "INVOICE")
public final class Invoice implements Serializable {
@ManyToOne
@JoinColumn(name = "account_uuid", referencedColumnName = "uuid", nullable = false)
private Account account;
}
我使用Spring数据JPA:
@Repository
public interface SpringDataClientRepository extends ClientRepository, JpaRepository<Client, Integer> {
其他相同。
当我尝试运行ITests,测试与客户端做工精细:
@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());
它节省了客户和帐户,但该测试:
@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());
失败:
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
我想要所有发票将被保存,如果我保存帐户这些发票。客户端也一样 - 如果我保存它们的客户端,所有帐户都将被保存。
我该怎么做?