保存具有一对多单向关系的对象不起作用

时间:2018-08-27 11:38:06

标签: java hibernate jpa

我有命令,它可以有很多状态。尝试保存新状态似乎不适用于单向关系。 难道我无法保存代码中从订单到状态的单向关系状态?有什么方法可以保存该文件?

表:ORDER(PK_ORDER_ID,ORDER_NO)

表:ORDER_STATUS(PK_ORDER_STATUS_ID,FK_ORDER_ID,STATUS)

像这样的订单类

@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类

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

}

其他文件位于

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

保存更改的数据库代码是-

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

出现以下错误

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

0 个答案:

没有答案