1 Reply Latest reply: Sep 9, 2013 10:38 AM by gimbal2 RSS

    one to many relationship in hibernate

    Prem

      I am very much new to hibernate,

      for testing purpose I am using JPA annotations and hibernate.

      While testing one to many relationship , I got a problem

       

      I have a PurchaseOrder class and OrderLine class having one to many relationship.

       

      package orderpackage;

       

       

      import java.util.ArrayList;

      import java.util.List;

       

       

      import javax.persistence.CascadeType;

      import javax.persistence.Entity;

      import javax.persistence.GeneratedValue;

      import javax.persistence.GenerationType;

      import javax.persistence.Id;

      import javax.persistence.OneToMany;

       

       

      @Entity

      public class PurchaseOrder {

        @Id

        @GeneratedValue(strategy=GenerationType.AUTO)

        private int id;

       

        @OneToMany(cascade=CascadeType.ALL,mappedBy="order")

        private List <OrderLine> orderLine = new ArrayList<OrderLine>();

       

        public PurchaseOrder(String orderName) {

        this.orderName = orderName;

        }

       

       

        public List<OrderLine> getOrderLine() {

        return orderLine;

        }

       

       

        public void setOrderLine(List<OrderLine> orderLine) {

        this.orderLine = orderLine;

        }

       

       

        private String orderName;

       

       

        public int getId() {

        return id;

        }

       

       

        public void setId(int id) {

        this.id = id;

        }

       

       

        public String getOrderName() {

        return orderName;

        }

       

       

        public void setOrderName(String orderName) {

        this.orderName = orderName;

        }

       

       

       

      }

       

       

      OrderLine.java

       

      package orderpackage;

       

       

      import javax.persistence.CascadeType;

      import javax.persistence.Entity;

      import javax.persistence.GeneratedValue;

      import javax.persistence.GenerationType;

      import javax.persistence.Id;

      import javax.persistence.ManyToOne;

       

       

      @Entity

      public class OrderLine {

       

        @Id

        @GeneratedValue(strategy=GenerationType.AUTO)

        private int id;

        private String itemName;

        private int qty;

       

        @ManyToOne(cascade=CascadeType.ALL)

        private PurchaseOrder order;

       

       

        public OrderLine(String itemName, int qty) {

        this.itemName = itemName;

        this.qty = qty;

        }

        public PurchaseOrder getOrder() {

        return order;

        }

        public void setOrder(PurchaseOrder order) {

        this.order = order;

        }

        public int getId() {

        return id;

        }

        public void setId(int id) {

        this.id = id;

        }

        public String getItemName() {

        return itemName;

        }

        public void setItemName(String itemName) {

        this.itemName = itemName;

        }

        public int getQty() {

        return qty;

        }

        public void setQty(int qty) {

        this.qty = qty;

        }

       

       

       

       

      }

       

       

           OrderTest.java

       

      package orderpackage;
      
      
      import org.hibernate.Session;
      import org.hibernate.SessionFactory;
      import org.hibernate.cfg.Configuration;
      
      
      public class OrderTest {
      
        public static void main(String[] args) {
         
        PurchaseOrder order = new PurchaseOrder("order no 1");
        OrderLine orderLine1 = new OrderLine("item1",25);
        OrderLine orderLine2 = new OrderLine("item2",28);
        OrderLine orderLine3 = new OrderLine("item3",38);
        OrderLine orderLine4 = new OrderLine("item4",48);
      
        order.getOrderLine().add(orderLine1);
        order.getOrderLine().add(orderLine2);
        order.getOrderLine().add(orderLine3);
        order.getOrderLine().add(orderLine4);
      
        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
      
        session.beginTransaction();
        session.persist(order);
        session.getTransaction().commit();
        session.close();
        }
      
      
      }
      

       

       

      I can see Order and Order Line are get persisted but Order Line table don't have order_id populated automatically. Please help me out.