简体   繁体   中英

cascade = CascadeType.ALL not updating the child table

These are my pojo class

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

My 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;
    }



}

When ever I am trying to save order class I want my orderdetail class also get saved but when I am trying to save the List in order,Is is not getting saved and there is not error provided by hibernate that can help... Thanks for the help

when i am trying to to persist the order class

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=?

This what I am getting output.

This is my code which save the class

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

I think ordetail has to be created inside the for.. You are modifying the same object for each productdetail. Should be like this:

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);
}

Hey I have recheck my pojo class and I found out the mistake I have done. I have made change and it work properly now. I was not setting the the id for Orderdetail table. It was auto increment in database. So it was giving me error "" So I have made change in orderdetail iD "@GeneratedValue(strategy=GenerationType.AUTO)" So now It is working fine cause now hibernate know that the id will get value from database. Thanks for the help and for your time

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