@joinedcolumn not correctly mapped by hibernate

88 Views Asked by At

I'm facing an issue when running unit test with hibernate trying to make some tests against my repository (spring boot+jpa application). below the config i'm using It consists of 3 entities Record, Order an Status:

@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;
}

my unit test against record repository is the following:

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)));
    }

When running the test, i'm getting the following error:

Hibernate: insert into T_ORDER (CUSTOMER_ID, CUSTOMER_ORDER_ID, EXTERNAL_ORDER_ID, INPUT_FILE_NAME, INPUTFILE_DATE, QUANTITY, STATUS_CODE, ID) values (?, ?, ?, ?, ?, ?, ?, ?) 2021-12-02 11:09:03.316 WARN 16512 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 23502, SQLState: 23502 2021-12-02 11:09:03.316 ERROR 16512 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : NULL not allowed for column "STATUS_CODE"; SQL statement: insert into T_ORDER (CUSTOMER_ID, CUSTOMER_ORDER_ID, EXTERNAL_ORDER_ID, INPUT_FILE_NAME, INPUTFILE_DATE, QUANTITY, TATUS_CODE, ID) values (?, ?, ?, ?, ?, ?, ?, ?) [23502-200]

Hibernate is using NULL for the value of the status code (which is not a primary key) intead of Status.code Please i'm not sure about the annotations used between the entities, so i don't know what i'm doing wrong ?

Regards

0

There are 0 best solutions below