![](/img/trans.png)
[英]JPA persists already persisted Objects from a ManyToMany relationship
[英]Persisting already persisted object within a relationship dependency
我在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
這是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
和我的Order實體:
@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。 問候,
您已經設置了order-> productOrder-> product關系來級聯持久性(包括在cascadeType.ALL中)。 當您在訂單上調用持久化時,實際上是在ProductOrder和Product上也都調用了持久化,如果數據庫中已經存在它們,則這將引發異常。
1)刪除productOrder-> product關系上的cascade持久化選項,以使持久化不會被調用-如果您通過新的Order關聯新產品,則必須手動調用persist的缺點。
2)使用產品pk調用em.find,並將返回的實例關聯到productOrder-> product關系3)使用em.merge代替,它將層疊每個關系並自行決定實體是否存在或需要插入實體。 這將導致在產品實例中進行的更改也將被合並。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.