【问题标题】:Spring Data with Integration Test OneToMany ManyToOneSpring Data 与集成测试 OneToMany ManyToOne
【发布时间】:2014-11-13 03:47:21
【问题描述】:

我知道这个话题并不新鲜,但我几天尝试解决任务。 我有 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

我希望如果我保存这些发票的帐户,所有发票都将被保存。与客户相同 - 如果我保存客户,所有帐户都将被保存。
我该怎么做?

【问题讨论】:

    标签: jpa integration-testing one-to-many spring-data many-to-one


    【解决方案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;
     }
    

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

    【讨论】:

    • 所以基本上,您只是从Invoice 实体中删除了JoinColumn 注释?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-19
    • 1970-01-01
    • 2020-04-04
    • 2017-09-22
    • 2010-09-21
    相关资源
    最近更新 更多