在关系依赖项中持久保留对象

时间:2013-07-16 16:46:13

标签: java hibernate jpa jboss

我在产品和订单之间有一个关系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持久存在于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";

有什么想法吗? 我很绝望..我需要这个工作昨天.. 请帮忙吗?

顺便说一下,我正在使用JSF 2.0,Hibernate和JPA 2.0以及Postgres。 的问候,

1 个答案:

答案 0 :(得分:0)

您已将order-&gt; productOrder-&gt;产品关系设置为级联persist(包含在cascadeType.ALL中)。当您在订单上调用persist时,您实际上也会在ProductOrder和Product上调用persist,如果数据库中已存在任何异常,则会产生异常。

无论哪种 1)删除productOrder-&gt;产品关系中的cascade persist选项,以便不会调用persist - 如果您通过新订单关联新产品,则必须手动调用persist。 2)使用产品pk调用em.find,并将返回的实例关联到productOrder-&gt;产品关系 3)使用em.merge代替,它将在每个关系上级联,并在实体存在或需要插入时自行决定。这将导致在产品实例中进行的更改也会合并。