Jpa Spring数据与集成测试一对多对一

Jpa Spring数据与集成测试一对多对一,jpa,integration-testing,one-to-many,spring-data,many-to-one,Jpa,Integration Testing,One To Many,Spring Data,Many To One,我知道这个话题并不新鲜,但我用了几天的时间试图解决这个问题。 我有三门课——客户、账户和发票。 客户有很多账户,账户有很多发票。 我将它们映射为: 客户端 @Entity @Table(name = "CLIENT") public final class Client implements Serializable { ... @Column(length = 36, nullable = false, unique = true, updatable = false) pri

我知道这个话题并不新鲜,但我用了几天的时间试图解决这个问题。 我有三门课——客户、账户和发票。 客户有很多账户,账户有很多发票。 我将它们映射为:
客户端

@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 = "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 = "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;
 }
我使用Spring数据Jpa:

@Repository
public interface SpringDataClientRepository extends ClientRepository, JpaRepository<Client, Integer> {
它将客户端和帐户保存在中。但是这个测试:

@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 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 = "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 = "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;
 }
账户

@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 = "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 = "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;
 }
@实体
@表(name=“ACCOUNT”)
公共最终类帐户扩展BaseEntity{
@列(长度=36)
私有字符串uuid;
@OneToMany(cascade=CascadeType.ALL,orphanRemoving=true,fetch=FetchType.LAZY)
@JoinColumn(name=“account\u id”,referencedColumnName=“id”)
私人清单发票;
}

在测试中,我把一切都保持原样。现在一切都正常了。

那么基本上,您只从
发票
实体中删除了
JoinColumn
注释?