-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: one to many relationship in hibernate
PostPosted: Thu Sep 05, 2013 7:41 am 
Newbie

Joined: Fri Aug 15, 2008 9:09 am
Posts: 12
I am very much new to hibernate . For testing purpose I have created two classes PurchaseOrder and OrderLine , having one to many relationship.

Following code inserts these two entities but do not update foreign key in orderLine table

PurchaseOrder.java
Code:

package orderpackage;

import java.util.HashSet;
import java.util.Set;

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(mappedBy="order",cascade=CascadeType.ALL)
   private Set <OrderLine> orderLine = new HashSet<OrderLine>();
   
   public PurchaseOrder(String orderName) {
      this.orderName = orderName;
   }

   public Set<OrderLine> getOrderLine() {
      return orderLine;
   }

   public void setOrderLine(Set<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

Code:
package orderpackage;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
public class OrderLine {
   
   
   private int id;
   private String itemName;
   private int qty;
   
   
   private PurchaseOrder order;
   
   
   public OrderLine(String itemName, int qty) {
      this.itemName = itemName;
      this.qty = qty;
   }
   
   @ManyToOne(fetch=FetchType.LAZY,cascade=CascadeType.ALL)
   @JoinColumn(name="ORDER_ID")
   public PurchaseOrder getOrder() {
      return order;
   }
   public void setOrder(PurchaseOrder order) {
      this.order = order;
   }
   
   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   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

Code:
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();
   }

}



Hibernate query log
Code:
ibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into PurchaseOrder (orderName, id) values (?, ?)
Hibernate: insert into OrderLine (itemName, ORDER_ID, qty, id) values (?, ?, ?, ?)
Hibernate: insert into OrderLine (itemName, ORDER_ID, qty, id) values (?, ?, ?, ?)
Hibernate: insert into OrderLine (itemName, ORDER_ID, qty, id) values (?, ?, ?, ?)
Hibernate: insert into OrderLine (itemName, ORDER_ID, qty, id) values (?, ?, ?, ?)


Top
 Profile  
 
 Post subject: Re: one to many relationship in hibernate
PostPosted: Tue Mar 03, 2015 12:48 pm 
Newbie

Joined: Tue Mar 03, 2015 12:12 pm
Posts: 2
thanks for the long query


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.