0

在使用休眠运行单元测试尝试对我的存储库(spring boot + jpa 应用程序)进行一些测试时,我遇到了一个问题。在我正在使用的配置下方它由 3 个实体记录、订购和状态组成:

@Table(name = "T_RECORD")
public class Record implements Serializable {

    @Id
    @Column(name = "ID")
    private Long id;

    @Column(name = "CUSTOMER_ID")
    private String customerId;

    @Column(name = "CARD_ID")
    private Long cardId;

    @ManyToOne(optional = false)
    private Order order;
}

@Table(name = "T_ORDER")
public class Order implements Serializable {

    @Id
    @Column(name = "ID")
    private Long id;

    @Column(name = "CUSTOMER_ID")
    private String customerId;

    @Column(name = "EXTERNAL_ORDER_ID")
    private String externalOrderId;

    @Column(name = "INPUT_FILE_NAME")
    private String inputFileName;

    @Column(name = "QUANTITY")
    private Integer quantity;

    @ManyToOne(optional = false)
    @JoinColumn(
            name = "STATUS_CODE",
            referencedColumnName = "CODE",
            nullable = false
    )
    private Status statusCode;
}

@Table(name = "T_STATUS")
public class Status implements Serializable {
  @Id
  @Column(name = "ID", nullable = false)
  private Long id;

  @Column(name = "NAME", nullable = false, length = 50)
  private String name;

  @Column(name = "CODE", nullable = false)
  private Integer code;
}

我对记录存储库的单元测试如下:

private static final Status STATUS = Status.builder()
            .id(ID_STATUS)
            .name(STATUS_NAME)
            .code(STATUS_CODE)
            .build();

    private static final Order ORDER = Order.builder()
            .id(ID_ORDER)
            .customerId(CUSTOMER_ID)
            .externalOrderId(EXTERNAL_ORDER_ID)
            .inputFileName(INPUT_FILE_NAME)
            .quantity(QUANTITY)
            .statusCode(STATUS)
            .build();

    private static final Record RECORD = Record.builder()
            .id(ID_RECORD)
            .customerId(CUSTOMER_ID)
            .cardId(CARD_ID)
            .order(ORDER)            
            .build();

    @Test
    public void findByCustomerIdAndCardId() {

        statusRepository.save(STATUS);
        orderRepository.save(ORDER);
        recordRepository.save(RECORD);

        List<Record> newRecords = recordRepository.findByCustomerIdAndCardId(CUSTOMER_ID, CARD_ID);
        MatcherAssert.assertThat(newRecords.get(0), Matchers.is(Matchers.equalTo(RECORD)));
    }

运行测试时,我收到以下错误:

休眠:插入 T_ORDER (CUSTOMER_ID, CUSTOMER_ORDER_ID, EXTERNAL_ORDER_ID, INPUT_FILE_NAME, INPUTFILE_DATE, QUANTITY, STATUS_CODE, ID) 值 (?, ?, ?, ?, ?, ?, ?, ?) 2021-12-02 11:09:03.316 WARN 16512 --- [主] ohengine.jdbc.spi.SqlExceptionHelper:SQL 错误:23502,SQLState:23502 2021-12-02 11:09:03.316 错误 16512 --- [主] ohengine.jdbc.spi.SqlExceptionHelper: “STATUS_CODE”列不允许为 NULL;SQL 语句:插入 T_ORDER (CUSTOMER_ID, CUSTOMER_ORDER_ID, EXTERNAL_ORDER_ID, INPUT_FILE_NAME, INPUTFILE_DATE, QUANTITY, TATUS_CODE, ID) 值 (?, ?, ?, ?, ?, ?, ?, ?) [23502-200]

Hibernate 使用 NULL 作为状态代码(不是主键)的值而不是 Status.code 请我不确定实体之间使用的注释,所以我不知道我在做什么错?

问候

4

0 回答 0