0

I have order and it can have many status. Trying to save new status seems not working with unidirectional relationship. Is that right I can not save status with unidirectional relation from order to status as shown in my code? What are possible way to get that saved?

Table : ORDER(PK_ORDER_ID, ORDER_NO)

Table : ORDER_STATUS(PK_ORDER_STATUS_ID, FK_ORDER_ID, STATUS)

Order class like this

@Entity
@Table(name = "ORDER")
public class Order {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "PK_ORDER_ID", unique = true, nullable = false)
private Long id;

@JoinColumn(name = "FK_ORDER_ID", referencedColumnName = "PK_ORDER_ID")
private Set<OrderStatus> orderStatus;
}

OrderStatus class like this

@Entity
@Table(name = "ORDER_STATUS")
public class OrderStatus {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "PK_ORDER_STATUS_ID")
private Long id;

@Column(name = "STATUS")
private String status;

public OrderStatus(String status) {
    this.status = status;
}

}

Somewhere is other file

order.getStatus().add(new OrderStatus("new"));

The database code which save the changes is -

@Transactional(TxType.REQUIRED)
public U merge(U EntityToUpdate) {
    return em.merge(EntityToUpdate);
}

It give following error

java.sql.SQLException: (conn:40) Field 'FK_ORDER_ID' doesn't have value
Query is: insert into ORDER_STATUS (STATUS) values (?), parameters ['new']
anna
  • 265
  • 1
  • 3
  • 17

0 Answers0