1 Reply Latest reply on Nov 10, 2007 10:53 AM by wayne baylor

    Relationship Only Saving 1 Child Object

    David Durst Newbie

      I am running JBoss 4.0.4.GA and JDK 1.5.0_06.

      I have a One To Many relationship Order->OrderLine
      I collect all the OrderLines and add them to the order and then
      call the session facade to save. Unfortunately only one of my OrderLines
      gets persisted. Can anyone tell me why?

      Code to save Order

      order.setOrderLines(orderLines);
      OrderFCD obf = new OrderFCD();
      order = obf.create(order);
      


      Order
      package beltmasta.ejb.persistance;
      
      import java.io.Serializable;
      import java.util.Calendar;
      import java.util.Collection;
      import java.util.Vector;
      import javax.persistence.CascadeType;
      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      import javax.persistence.Id;
      import javax.persistence.NamedQueries;
      import javax.persistence.NamedQuery;
      import javax.persistence.OneToMany;
      import javax.persistence.OneToOne;
      import javax.persistence.Table;
      import javax.persistence.Temporal;
      import javax.persistence.TemporalType;
      
      /**
       * Entity class Order
       * @author David
       */
      @Entity
      @Table(name="belt_order")
      @NamedQueries( {
      @NamedQuery(name="order.getOrdersByCustomer", query="select object(o) from Order as o where o.status = 'O' and o.orderHeader.customer = :customer"),
      @NamedQuery(name="order.getQuotesByCustomer", query="select object(o) from Order as o where o.status = 'B' and o.orderHeader.customer = :customer"),
      @NamedQuery(name="order.getInvoicesByCustomer", query="select object(o) from Order as o where o.status = 'I' and o.orderHeader.customer = :customer"),
      @NamedQuery(name="order.getHoldOrdersByCustomer", query="select object(o) from Order as o where o.status = 'H' and o.orderHeader.customer = :customer"),
      @NamedQuery(name="order.getConsignmentOrdersByCustomer", query="select object(o) from Order as o where o.status = 'C' and o.orderHeader.customer = :customer")
      })
      public class Order implements Serializable {
       public static String BID = "B";
       public static String ORDER = "O";
       public static String INVOICE = "I";
       public static String HOLD = "H";
       public static String CONSIGNMENT = "C";
      
      
       @Id
       @GeneratedValue(strategy = GenerationType.AUTO)
       private Long id;
       private String eclipseId;
       @Column(length = 1)
       private String status;
       @Temporal(TemporalType.TIMESTAMP)
       private Calendar quoteTime;
       @Temporal(TemporalType.TIMESTAMP)
       private Calendar orderTime;
       @Temporal(TemporalType.TIMESTAMP)
       private Calendar invoiceTime;
      
       @OneToOne(cascade = CascadeType.ALL)
       private OrderHeader orderHeader;
      
       @OneToMany(mappedBy="parentOrder", cascade=CascadeType.ALL)
       private Collection <OrderLine>orderLines = new Vector<OrderLine>();
      
       public Order() {
       }
      
      
       //<editor-fold defaultstate="collapsed" desc="Properties Accessors">
       public Long getId() {
       return this.id;
       }
       public void setId(Long id) {
       this.id = id;
       }
       public String getEclipseId() {
       return eclipseId;
       }
       public void setEclipseId(String eclipseId) {
       this.eclipseId = eclipseId;
       }
       public String getStatus() {
       return status;
       }
       public void setStatus(String status) {
       this.status = status;
       }
       public Calendar getQuoteTime() {
       return quoteTime;
       }
       public void setQuoteTime(Calendar quoteTime) {
       this.quoteTime = quoteTime;
       }
       public Calendar getOrderTime() {
       return orderTime;
       }
       public void setOrderTime(Calendar orderTime) {
       this.orderTime = orderTime;
       }
       public Calendar getInvoiceTime() {
       return invoiceTime;
       }
       public void setInvoiceTime(Calendar invoiceTime) {
       this.invoiceTime = invoiceTime;
       }
      
       //</editor-fold>
       //<editor-fold defaultstate="collapsed" desc="Method Overides">
       @Override
       public int hashCode() {
       int hash = 0;
       hash += (this.id != null ? this.id.hashCode() : 0);
       return hash;
       }
       @Override
       public boolean equals(Object object) {
       // TODO: Warning - this method won't work in the case the id fields are not set
       if (!(object instanceof Order)) {
       return false;
       }
       Order other = (Order)object;
       if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) return false;
       return true;
       }
       @Override
       public String toString() {
       return id.toString();
       }
      
       //</editor-fold>
       //<editor-fold defaultstate="collapsed" desc="Logic Methods">
       public void setQuoted() {
       this.setQuoteTime(Calendar.getInstance());
       }
       public void setOrdered() {
       this.setOrderTime(Calendar.getInstance());
       }
       public void setInvoiced(){
       this.setInvoiceTime(Calendar.getInstance());
       }
       //</editor-fold>
       //<editor-fold defaultstate="collapsed" desc="Relationships">
       public OrderHeader getOrderHeader() {
       return orderHeader;
       }
       public void setOrderHeader(OrderHeader orderHeader) {
       this.orderHeader = orderHeader;
       }
       public Collection<OrderLine> getOrderLines() {
       return orderLines;
       }
       public void setOrderLines(Collection<OrderLine> orderLines) {
      System.out.println("Before # of orderlines: "+orderLines.size());
       this.orderLines = new Vector(orderLines);
      System.out.println("After # of orderlines: "+this.orderLines.size());
      
       }
       //</editor-fold>
      
      }
      


      OrderLine
      package beltmasta.ejb.persistance;
      
      import beltmasta.ejb.persistance.embeddable.LineQuantity;
      import beltmasta.ejb.persistance.embeddable.PriceCost;
      import java.io.Serializable;
      import java.util.ArrayList;
      import java.util.Calendar;
      import java.util.Collection;
      import javax.persistence.AttributeOverride;
      import javax.persistence.AttributeOverrides;
      import javax.persistence.CascadeType;
      import javax.persistence.Column;
      import javax.persistence.Embedded;
      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      import javax.persistence.Id;
      import javax.persistence.JoinColumn;
      import javax.persistence.ManyToOne;
      import javax.persistence.NamedQueries;
      import javax.persistence.NamedQuery;
      import javax.persistence.OneToMany;
      import javax.persistence.OneToOne;
      import javax.persistence.Table;
      import javax.persistence.Temporal;
      import javax.persistence.TemporalType;
      
      /**
       * Entity class OrderLine
       * @author David
       */
      @Entity
      @Table(name="belt_order_line")
      @NamedQueries( {
      @NamedQuery(name="orderline.bybelttype.bydaterange", query="select object(ol) from OrderLine as ol where ol.belt.beltType = :type and (ol.parentOrder.orderTime >= :startDate and ol.parentOrder.orderTime <= :endDate)")
      })
      
      public class OrderLine implements Serializable {
       public static String PICKUP = "P";
       public static String SHIP_WHEN_COMP = "H";
       public static String SHIP_WHEN_SPEC = "S";
       public static String CALL_WHEN_COMP = "C";
       public static String CALL_WHEN_SPEC = "W";
       public static String CANCEL = "X";
       public static String INVOICE = "I";
       public static String CONSIGN = "N";
       public static String HOLD = "L";
      
       @Id
       @GeneratedValue(strategy = GenerationType.AUTO)
       private Long id;
       private String lineStatus = OrderLine.SHIP_WHEN_COMP;
       @Temporal(TemporalType.TIMESTAMP)
       private Calendar requiredDate;
      
       @Embedded
       @AttributeOverrides({
       @AttributeOverride(name = "order", column=@Column(name="order_qty")),
       @AttributeOverride(name = "process", column=@Column(name="process_qty")),
       @AttributeOverride(name = "released", column=@Column(name="released_qty"))
       })
       private LineQuantity quantity;
       @Embedded
       private PriceCost priceCost;
       @OneToOne(cascade = CascadeType.ALL)
       private Belt belt;
       @ManyToOne
       @JoinColumn(name="parentOrder")
       private Order parentOrder;
       @OneToMany(mappedBy="orderLine", cascade = CascadeType.ALL)
       private Collection <SlabAssignment>slabAssignments = new ArrayList<SlabAssignment>();
       public OrderLine() {
       requiredDate = Calendar.getInstance();
       requiredDate.set(Calendar.DATE,requiredDate.get(Calendar.DATE)+3);
       }
       //<editor-fold defaultstate="collapsed" desc="Properties Accessors">
       public Long getId() {
       return this.id;
       }
       public void setId(Long id) {
       this.id = id;
       }
       public String getLineStatus() {
       return lineStatus;
       }
       public void setLineStatus(String lineStatus) {
       if(lineStatus.equals(OrderLine.PICKUP) ||
       lineStatus.equals(OrderLine.SHIP_WHEN_COMP) ||
       lineStatus.equals(OrderLine.SHIP_WHEN_SPEC) ||
       lineStatus.equals(OrderLine.CALL_WHEN_SPEC) ||
       lineStatus.equals(OrderLine.CALL_WHEN_COMP) ||
       lineStatus.equals(OrderLine.CANCEL) ||
       lineStatus.equals(OrderLine.INVOICE)||
       lineStatus.equals(OrderLine.CONSIGN)||
       lineStatus.equals(OrderLine.HOLD)){
      
       this.lineStatus = lineStatus;
       }
       }
       public LineQuantity getQuantity() {
       return quantity;
       }
       public void setQuantity(LineQuantity quantity) {
       this.quantity = quantity;
       }
       public PriceCost getPriceCost() {
       return priceCost;
       }
       public void setPriceCost(PriceCost priceCost) {
       this.priceCost = priceCost;
       }
       public Calendar getRequiredDate() {
       return requiredDate;
       }
       public void setRequiredDate(Calendar requiredDate) {
       this.requiredDate = requiredDate;
       }
       //</editor-fold>
       //<editor-fold defaultstate="collapsed" desc="Method Overides">
       @Override
       public int hashCode() {
       int hash = 0;
       hash += (this.getId() != null ? this.getId().hashCode() : 0);
       return hash;
       }
       @Override
       public boolean equals(Object object) {
       // TODO: Warning - this method won't work in the case the id fields are not set
       if (!(object instanceof OrderLine)) {
       return false;
       }
       OrderLine other = (OrderLine)object;
       if (this.getId() != other.getId() && (this.getId() == null || !this.getId().equals(other.getId()))) return false;
       return true;
       }
       @Override
       public String toString() {
       return "beltmasta.ejb.persistance.OrderLine[id=" + getId() + "]";
       }
       //</editor-fold>
       //<editor-fold defaultstate="collapsed" desc="Relationships">
       public Belt getBelt() {
       return belt;
       }
       public void setBelt(Belt belt) {
       this.belt = belt;
       }
       public Collection<SlabAssignment> getSlabAssignments() {
       return slabAssignments;
       }
       public void setSlabAssignments(Collection<SlabAssignment> slabAssignments) {
       this.slabAssignments = slabAssignments;
       }
       public void addSlabAssignment(SlabAssignment assignment) {
       int open = quantity.getOpen();
       int assign = assignment.getAssignedQuantity();
       if(assign <= open) {
       quantity.setProcess(quantity.getProcess()+assign);
       assignment.setOrderLine(this);
       slabAssignments.add(assignment);
       }
       }
       public Order getParentOrder() {
       return parentOrder;
       }
       public void setParentOrder(Order parentOrder) {
       this.parentOrder = parentOrder;
       }
       //</editor-fold>
       //<editor-fold defaultstate="collapsed" desc="Logic Methods">
       public double getTotalOrderPrice() {
       return (double)(priceCost.getPrice() * quantity.getOrder());
       }
       public double getTotalOrderCost() {
       return (double)(priceCost.getCost() * quantity.getOrder());
       }
       public double getTotalOpenPrice() {
       return (double)(priceCost.getPrice() * quantity.getOpen());
       }
       public double getTotalOpenCost() {
       return (double)(priceCost.getCost() * quantity.getOpen());
       }
       //</editor-fold>
      
      }
      


        • 1. Re: Relationship Only Saving 1 Child Object
          wayne baylor Apprentice

          since you're using the mappedBy attribute your code should look something like:

          Order order = ...;
          OrderLine ol1 = ...;
          OrderLine ol2 = ...;
          
          ol1.setParentOrder(order);
          ol2.setParentOrder(order);
          
          Collection<OrderLine> orderLines = order.getOrderLines();
          orderLines.add(ol1);
          orderLines.add(ol2);
          
          order.setOrderLines(orderLines);
          
          -----------------
          //save code
          em.persist(order); //or em.merge(order)