cascade = CascadeType.ALL不更新子表

时间:2014-10-14 12:17:30

标签: java hibernate

这些是我的pojo类

Orderdetail.java

package online.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "orderdetail")
public class OrderDetail {


     @Id    
     @Column(name="order_detail_id")
    private int order_detail_id;

     @Column(name="bill") 
    private float bill;

    @ManyToOne
    @JoinColumn(name = "p_id" )
    private Product p_id;

    @ManyToOne
    @JoinColumn(name = "o_id" )
    private Order o_id;




    public int getOrder_detail_id() {
        return order_detail_id;
    }
    public void setOrder_detail_id(int order_detail_id) {
        this.order_detail_id = order_detail_id;
    }
    public float getBill() {
        return bill;
    }
    public void setBill(float bill) {
        this.bill = bill;
    }

    public Product getP_id() {
        return p_id;
    }
    public void setP_id(Product p_id) {
        this.p_id = p_id;
    }
    public Order getO_id() {
        return o_id;
    }
    public void setO_id(Order o_id) {
        this.o_id = o_id;
    }
}

我的Order.java

package online.model;

import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name = "ordertable")
public class Order {

    @Id
    @Column(name = "order_id")
    private int order_id;


    @OneToMany(mappedBy = "o_id",cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<OrderDetail> orderdetail;

    @ManyToOne
    @JoinColumn(name = "u_id")
    private UserDetail u_id;





    public UserDetail getU_id() {
        return u_id;
    }

    public void setU_id(UserDetail u_id) {
        this.u_id = u_id;
    }

    @Column(name = "date")
    @Temporal(TemporalType.TIMESTAMP)
    private Date date;

    @Column(name = "totalbill")
    private Float totalbill;

    public Float getTotalbill() {
        return totalbill;
    }

    public void setTotalbill(Float totalbill) {
        this.totalbill = totalbill;
    }

    public List<OrderDetail> getOrderdetail() {
        return orderdetail;
    }

    public void setOrderdetail(List<OrderDetail> orderdetail) {
        this.orderdetail = orderdetail;
    }


    public int getOrder_id() {
        return order_id;
    }

    public void setOrder_id(int order_id) {
        this.order_id = order_id;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }



}

当我尝试保存订单类时,我希望我的orderdetail类也得到保存,但是当我尝试按顺序保存List时,Is没有得到保存,并且没有hibernate提供的错误可以帮助.. 。 谢谢你的帮助

当我试图坚持订单类

Hibernate: select orderdetai_.order_detail_id, orderdetai_.bill as bill7_, orderdetai_.o_id as o3_7_, orderdetai_.p_id as p4_7_ from orderdetail orderdetai_ where orderdetai_.order_detail_id=?

这就是我得到的输出。

这是我保存课程的代码

@Override
    public boolean payment(String username, Integer ordernumber, Date date,
            Float totalbill, List<Integer> list) {
        Session session = sessionFactory.openSession();
        Transaction tranction = session.beginTransaction();
        try {
            Query query = session
                    .createQuery("from UserDetail where user_username = :username");
            query.setParameter("username", username);
            List<UserDetail> userdetaillist = query.list();
            UserDetail userdetail = userdetaillist.get(0);

            query = session
                    .createQuery("from ProductDetail where product_detail_id in(:list)");
            query.setParameterList("list", list);
            List<ProductDetail> productdetail = query.list();
            Order order = new Order();
            order.setOrder_id(ordernumber);
            order.setDate(date);
            order.setU_id(userdetail);
            order.setTotalbill(totalbill);

            List<OrderDetail> orderdetail = new ArrayList<OrderDetail>();
            OrderDetail ordetail = new OrderDetail();
            for (ProductDetail pro : productdetail) {


                ordetail.setO_id(order);
                ordetail.setP_id(pro.getProduct_id());
                ordetail.setBill(pro.getProduct_id().getProduct_sell_price());
                orderdetail.add(ordetail);
            }
            System.out.print("totalbill" + totalbill);
            System.out.println(orderdetail);

            order.setOrderdetail(orderdetail);
            session.save(order);

             tranction.commit();

            return true;
        } catch (Exception e) {
            tranction.rollback();
            e.getStackTrace();
        }
        return false;
    }

2 个答案:

答案 0 :(得分:0)

我认为必须在for ...中创建ordetail。您正在为每个productdetail修改相同的对象。应该是这样的:

List<OrderDetail> orderdetail = new ArrayList<OrderDetail>();
OrderDetail ordetail = null;
for (ProductDetail pro : productdetail) {
   ordetail = new OrderDetail();
   ordetail.setO_id(order);
   ordetail.setP_id(pro.getProduct_id());
   ordetail.setBill(pro.getProduct_id().getProduct_sell_price());
   orderdetail.add(ordetail);
}

答案 1 :(得分:0)

嘿,我已经重新检查了我的pojo课,我发现了我所犯的错误。我做了改变,现在它正常工作。 我没有为Orderdetail表设置id。它是数据库中的自动增量。 所以它给了我错误“” 所以我在orderdetail iD上做了改变 “@GeneratedValue(strategy = GenerationType.AUTO)”所以现在它工作正常,因为现在hibernate知道id将从数据库中获取值。 感谢您的帮助和时间