我與產品和訂單之間有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,
謝謝您的回答,@克里斯 ,但它沒有工作.. 我刪除了級聯型全從ProductOrder>產品並沒有奏效。 試圖設置ProductOrder>產品與此FIND對象(使用EM),並沒有工作... 它沒有工作.. 而我正在嘗試使用合併,而不是堅持。沒有工作,以及... 我不知道發生了什麼...請幫助 –
你能解釋什麼是不工作?有沒有例外,數據不會持續? – Chris
是的,數據不會持久,因爲我在例外的情況下,有一個回退.. 是的,沒有例外,但它太長,張貼在這裏.. 錯誤[標準錯誤(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 –