我知道这个话题并不新鲜,但我几天尝试解决任务。
我有 3 个类 - 客户、帐户和发票。
客户有很多账户,账户有很多发票。
我映射了它们:
客户端
@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 Data 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
我想要 如果我保存这些发票的帐户,所有发票都将被保存。和客户一样 - 如果我保存他们的客户,所有账户都将被保存。 我该怎么做?
最佳答案
我进行了更改以与组合建立单向关系:
客户
@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;
}
发票
@Entity
@Table(name = "INVOICE")
public final class Invoice extends BaseEntity {
@Column(length = 36)
private String uuid;
}
帐号
@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;
}
在测试中,我把一切都保持原样。现在一切正常。
关于jpa - Spring Data 与集成测试 OneToMany ManyToOne,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25918830/