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 (?, ?, ?, ?)