2013-07-16 77 views
0

我與產品和訂單之間有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 

這裏是ProductOrder:

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

所以基本上我有任何產品ALREADY persiste d in db ...當有些訂單即將訂購時,我剛剛列出了它們。所以我想堅持一個基於一些已經存在的對象(產品)的新對象(Order)。這是在managedbean上調用的用於保存訂單的方法。

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"; 

任何想法? 我很絕望..我需要這個爲昨天工作.. 請任何幫助?

btw我使用JSF 2.0,Hibernate與JPA 2.0和Postgres。 Regards,

回答

0

您已設置order-> productOrder-> product關係級聯persist(包含在cascadeType.ALL中)。當您按訂單調用persist時,您實際上仍然調用ProductOrder和Product上的持久化,如果它們中的任何一個已存在於數據庫中,則預期會引發異常。

要麼 1)刪除productOrder-> product關係中的cascade persist選項,以便persist不會被調用 - 如果您通過新訂單關聯新產品,則必須手動調用persist 。
2)使用product pk調用em.find,並將返回的實例關聯到productOrder-> product關係 3)改爲使用em.merge,它將級聯在每個關係上並自行決定實體是否存在或需要被插入。這會導致在產品實例中進行的更改也被合併。

+0

謝謝您的回答,@克里斯 ,但它沒有工作.. 我刪除了級聯型全從ProductOrder>產品並沒有奏效。 試圖設置ProductOrder>產品與此FIND對象(使用EM),並沒有工作... 它沒有工作.. 而我正在嘗試使用合併,而不是堅持。沒有工作,以及... 我不知道發生了什麼...請幫助 –

+0

你能解釋什麼是不工作?有沒有例外,數據不會持續? – Chris

+0

是的,數據不會持久,因爲我在例外的情況下,有一個回退.. 是的,沒有例外,但它太長,張貼在這裏.. 錯誤[標準錯誤(HTTP - 0.0.0.0- 8080-6)原因:org.hibernate.PersistentObjectException:傳遞給persist的分離實體:dom.product.entity.Product ERROR at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:141) ERROR at org.hibernate.event.internal.DefaultPersistEventListener。onPersist(DefaultPersistEventListener.java:78)在org.hibernate.tuple.entity.AbstractEntityTuplizer $ IncrediblySillyJpaMapsIdMappedIdentifierValueMarshaller.getIdentifier(AbstractEntityTuplize r.java:491)在org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier ERROR(AbstractEntityTuplizer –