我在 Product 和 Order 之间有 n/n 关系所以我有第三个表 ProductOrder,因为在创建新列时我需要它们。
public class Order implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ORDER_SEQ")
@Column(unique = true, nullable = false, updatable = false)
private Long idOrder;
@OneToMany(mappedBy = "order", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
private Set<ProductOrder> productOrder;
//get and setter
这是产品订单:
@Entity
@IdClass(ProductOrderId.class)
public class ProductOrder implements Serializable {
private static final long serialVersionUID = 3943799614725570559L;
@Id
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "product_id")
private Product product;
@Id
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "order_id")
private Order order;
private Integer qtdProduct;
private Double unitValueProductOrder;
//get and setter
还有我的 ProcutOrderId(以防万一)
public class ItemCompraId implements Serializable {
private Long compra;
private Long produto;
//get and set
和我的订单实体:
@Entity
@SequenceGenerator(name = "ORDER_SEQ", sequenceName = "s_compra", initialValue = 1, allocationSize = 1)
public class Order implements Serializable {
private static final long serialVersionUID = 3943799614725570559L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ORDER_SEQ")
@Column(unique = true, nullable = false, updatable = false)
private Long idOrder;
private Double orderValue;
private Date creatingDate;
@OneToMany(mappedBy = "order", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
private Set<ProductOrder> productOrder;
所以基本上我有任何产品已经保存在数据库中......当即将订购一些订单时,我刚刚得到了它们的列表。所以我想基于一些已经持久化的对象(产品)来持久化一个新的对象(订单)。这是在 managedbean 上调用以持久化 Order 的方法。
public String doOrder() throws Exception {
try {
Order order = new Order();
compra.setCreatingDate(new Date());
compra.setOrderValue(null);
if (compra.getProductOrder() == null)
compra.setProductOrder(new HashSet<ProductOrder>());
for (Product product : listOfMyCartOfProducts) {
ProductOrder productOrder = new ProductOrder();
productOrder.setQtdProduct(100);
productOrder.unitValueProductOrder(null);
productOrder.setOrder(order);
productOrder.setProduct(product); //I THINK THAT THE PROBLEM IT'S HERE
order.getOrderProduct().add(productOrder);
}
ejbInvoke.persist(order); //tryed .merge and it doesn't work aswell
return "stuff";
有任何想法吗?我很绝望..我昨天需要这个工作..有什么帮助吗??
顺便说一句,我使用的是 JSF 2.0、Hibernate 和 JPA 2.0 和 Postgres。问候,