我知道这个话题并不新鲜,但我几天尝试解决任务。
我有3个班级 - 客户,账户和发票。
客户有很多Accoutns,账户有很多发票。
我绘制了它们:
的客户端
@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
我希望如果我保存这些发票的帐户,则会保存所有发票。与客户端相同 - 如果我保存客户端,所有账户都将被保存。
我是怎么做的?
答案 0 :(得分:1)
我做了一个改变,与作文有单向关系:
<强>客户端强>
@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;
}
在测试中,我把一切都留下了。现在一切正常。