简体   繁体   中英

Persisting already persisted object within a relationship dependency

I have a relationship n/n between Product and Order So I have a third table ProductOrder, because I need new columns when they are created.

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

here is the 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

also My ProcutOrderId (just in case)

public class ItemCompraId implements Serializable {

private Long compra;

private Long produto;

//get and set

and my Order entity:

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

So basically I have any Products ALREADY persisted in db... I just got a list of them when some order is about to be ordered. So I wanna persist a new object (Order) based on some already persisted object (products). This is the method invoked on managedbean to persist an 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";

Any ideas? I'm desperate.. I need this working for yesterday.. Any help please??

Btw I'm using JSF 2.0, Hibernate with JPA 2.0 and Postgres. Regards,

You have set the order->productOrder->product relationships to cascade the persist (included in cascadeType.ALL). When you call persist on order, you are in effect calling persist on ProductOrder and Product as well, which is expected to throw an exception if any of them already exist in the database.

Either 1) remove the cascade persist option on the productOrder->product relationship so that persist is not getting called - with the draw back that you will have to manually call persist if you ever associate new Products through a new Order.
2) Call em.find using the product pk, and associate the instance returned to the productOrder->product relationship 3) use em.merge instead which will cascade over each relationship and decide on its own if the entity exists or needs to be inserted. This will cause changes made within the product instance though to be merged as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM