我正在尝试JPA,但我不能按照我理解的方式工作。
它是Order和OrderInvoice类之间的onetoone双向映射,这是必需的 协会
我的实体被标记为此
@Entity
@Table(name = "Orders")
public class Order {
@Id
@GeneratedValue
@Column(name = "ORDER_ID")
private int orderId;
@OneToOne(optional=false,cascade=CascadeType.ALL, mappedBy="order", targetEntity=OrderInvoice.class)
private OrderInvoice invoice;
}
@Entity
@Table(name = "ORDER_INVOICE")
public class OrderInvoice {
@Id
@GeneratedValue
@Column(name = "INVOICE_ID", nullable = false)
private int invoiceId;
@OneToOne(optional = false)
@JoinColumn(name="ORDER_ID")
private Order order;
}
我的测试类是这样的。
@Test
public void createOrder() {
Order order = createOrderImpl();
assertNotNull(order);
}
private Order createOrderImpl() {
OrderInvoice orderInvoice = new OrderInvoice(new Date(), 100.0, null,
null, new Date());
Order order = new Order(100.0, "JOHN Doe's Order", new Date(), new Date(),orderInvoice);
orderDao.create(order);
return order;
}
但是当我运行我的测试时,我遇到了以下问题
javax.persistence.PersistenceException: org.hibernate.PropertyValueException: not-null property references a null or transient value: order.OrderInvoice.order
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:614)
Caused by: org.hibernate.PropertyValueException: not-null property references a null or transient value: order.OrderInvoice.order
at org.hibernate.engine.Nullability.checkNullability(Nullability.java:95)
答案 0 :(得分:1)
尝试
orderInvoice.setOrder(order);
orderDao.create(order);