繁体   English   中英

在关系依赖项中持久存储已持久的对象

[英]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.

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