我正在为我的应用程序使用hibernate JPA。我有两个实体订单和发票。订单与发票类具有多对一映射。
@Entity
@Table(name="ORDERS",schema ="AMZ")
public class Orders {
@Id
@SequenceGenerator(name = "pk_sequence", sequenceName = "ORDERS_SEQ", schema = "AMZ", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "pk_sequence")
@Column(name = "ORDERS_ID")
private long id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "INVOICE_ID", referencedColumnName = "INVOICE_ID")
private Invoice invoice;
//getters and setters
@Entity
@Table(name="INVOICE",schema ="AMZ")
public class Invoice {
@Id
@SequenceGenerator(name = "pk_sequence", sequenceName = "INVOICE_SEQ", schema = "AMZ", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "pk_sequence")
@Column(name = "INVOICE_ID")
private long id;
@Column(name = "INVOICE_NUMBER")
private String invoiceNumber;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "invoice")
private Set<Orders> Orders= new HashSet<Orders>(0);
//getters & setters
当我多次坚持订单时,我会在发票表中提供发票,有时候它会为空。
我收到以下错误
org.hibernate.TransientPropertyValueException: object references an unsaved transient instance -
save the transient instance before flushing : com.domain.Orders.invoice ->
com.domain.Invoice; nested exception is java.lang.IllegalStateException:
org.hibernate.TransientPropertyValueException: object references an unsaved transient instance -
save the transient instance before flushing :
com.domain.Orders.invoice -> com.domain.Invoice
如果存在多对一映射,如何保持空值?