Order Insert

Insert an Order with details


package app.dataorder.test;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import app.data.test.Customer;
import app.data.test.Order;
import app.data.test.OrderDetail;
import app.data.test.OrderStatus;
import app.data.test.Product;
import app.data.test.ProductLookup;

import com.avaje.ebean.Ebean;
import com.avaje.ebean.FindByPredicates;
import com.avaje.ebean.Lookup;

public class OrderInsert {

    public static void main(String[] args) {

        OrderStatus statusNew = (OrderStatus)Ebean.getReference(OrderStatus.class, "NEW");
        
        Customer cust = (Customer)Ebean.getReference(Customer.class, 1);
        
        Order order = new Order();
        order.setStatus(statusNew);
        order.setOrderDate(new Date(System.currentTimeMillis()));
        order.setCustomer(cust);
        
        ArrayList details = new ArrayList();
        order.setDetails(details);
        
        
        OrderDetail line = new OrderDetail();
        line.setOrder(order);
        line.setProduct(getProductById(1));
        line.setOrderQty(5);
        details.add(line);
        
        line = new OrderDetail();
        line.setOrder(order);
        line.setProduct(findProductBySku("AA"));
        line.setOrderQty(3);
        details.add(line);
        
        line = new OrderDetail();
        line.setOrder(order);
        line.setProduct(lookupProductBySku("BB"));
        line.setOrderQty(4);
        details.add(line);
        
        
        Ebean.save(order);
    }

    /**
     * Normal getReference using the id.
     */
    private static Product getProductById(Integer prodId) {
        return (Product)Ebean.getReference(Product.class, prodId);
    }

    /**
     * Find a product given its sku.
     */
    private static Product findProductBySku(String sku) {
        FindByPredicates find = new FindByPredicates();
        find.setBeanType(Product.class);
        find.add("sku", find.EQ, sku);
        List list = Ebean.findList(find);
        if (list.size() != 1){
            throw new IllegalArgumentException("Invalid SKU");
        }
        return (Product)list.get(0);
    }

    /**
     * Use a Lookup to get the Product via sku.
     * This uses a server wide cache.
     */
    private static Product lookupProductBySku(String sku) {
        Lookup lookup = Ebean.getLookup(ProductLookup.class, null, null);
        return (Product)lookup.getSource(sku);
    }
}

Transaction Log

This log was modified with line breaks.

Note: Trans[1001] is the query for findProductBySku()

Note: Trans[1002] is the query to populate the ProductLookup. Lookup's are cached (similar to JPA L2 cache)

Note: Trans[1003] is the insert of the order and its 3 details


 INFO, 18:55:52.265, Trans[1001] SELECT p.id, p.cretime, p.name, p.sku, p.updtime
       FROM or_product p  WHERE p.sku = ? 
 INFO, 18:55:52.312, Trans[1001] FindByPredicates rows[1] app.data.test.Product  predicates[sku = ?] bind[AA] exe[109] 
 INFO, 18:55:52.312, Trans[1002] SELECT p.id, p.cretime, p.name, p.sku, p.updtime  FROM or_product p 
 INFO, 18:55:52.312, Trans[1002] FindByPredicates rows[2] app.data.test.Product  predicates[null] bind[null] exe[0] 
 INFO, 18:55:52.343, Trans[1003] insert into or_order (cretime, order_date, ship_date,
       updtime, customer_id, status_code) values (?, ?, ?, ?, ?, ?) 
 INFO, 18:55:52.343, Trans[1003] Binding Insert [or_order]  set[cretime=2006-11-28 
       18:55:52.343, orderDate=2006-11-28, shipDate=null, updtime=2006-11-28 18:55:52.343, id=1, code=NEW, ] 
 INFO, 18:55:52.343, Trans[1003] Inserted [app.data.test.Order] [1] 
 INFO, 18:55:52.359, Trans[1003] insert into or_order_detail (cretime, order_qty, 
       ship_qty, updtime, order_id, product_id) values (?, ?, ?, ?, ?, ?) 
 INFO, 18:55:52.359, Trans[1003] Binding Insert [or_order_detail]  set[cretime=2006-11-28
       18:55:52.359, orderQty=5, shipQty=null, updtime=2006-11-28 18:55:52.359, id=1, id=1, ] 
 INFO, 18:55:52.359, Trans[1003] Inserted [app.data.test.OrderDetail] [1] 
 INFO, 18:55:52.359, Trans[1003] insert into or_order_detail (cretime, order_qty, ship_qty,
       updtime, order_id, product_id) values (?, ?, ?, ?, ?, ?) 
 INFO, 18:55:52.359, Trans[1003] Binding Insert [or_order_detail]  set[cretime=2006-11-28
       18:55:52.359, orderQty=3, shipQty=null, updtime=2006-11-28 18:55:52.359, id=1, id=1, ] 
 INFO, 18:55:52.359, Trans[1003] Inserted [app.data.test.OrderDetail] [2] 
 INFO, 18:55:52.359, Trans[1003] insert into or_order_detail (cretime, order_qty, ship_qty, 
       updtime, order_id, product_id) values (?, ?, ?, ?, ?, ?) 
 INFO, 18:55:52.375, Trans[1003] Binding Insert [or_order_detail]  set[cretime=2006-11-28 
       18:55:52.359, orderQty=4, shipQty=null, updtime=2006-11-28 18:55:52.375, id=1, id=2, ] 
 INFO, 18:55:52.375, Trans[1003] Inserted [app.data.test.OrderDetail] [3] 

Transaction Log - with transparent Statement Batching

This log was truncated.

If you run in transparent batch mode (ebean.batch.mode=true) then the details will use JDBC statement batching.


 INFO, 09:43:59.734, Trans[1001] SELECT p.id, p.cretime, p.name, p.sku, p.updtime...
 INFO, 09:44:00.078, Trans[1001] FindByPredicates rows[1] app.data.test.Product  ...
 INFO, 09:44:00.109, Trans[1002] SELECT p.id, p.cretime, p.name, p.sku, p.updtime...
 INFO, 09:44:00.109, Trans[1002] FindByPredicates rows[2] app.data.test.Product  ...
 INFO, 09:44:00.234, Trans[1003] insert into or_order (cretime, order_date, ship ...
 INFO, 09:44:00.250, Trans[1003] Binding Insert [or_order]  set[cretime=2006-11-2...
 INFO, 09:44:00.265, Trans[1003] Inserted [app.data.test.Order] [2] 
 INFO, 09:44:00.359, Trans[1003] BatchControl flush 
 INFO, 09:44:00.359, Trans[1003] Batched Beans depth[1] type[app.data.test.OrderDetail] executing 
 INFO, 09:44:00.359, Trans[1003] insert into or_order_detail (cretime, order_qty, ...
 INFO, 09:44:00.390, Trans[1003] Binding Insert [or_order_detail]  set[cretime=200...
 INFO, 09:44:00.406, Trans[1003] Inserted [app.data.test.OrderDetail] [4] 
 INFO, 09:44:00.406, Trans[1003] insert into or_order_detail (cretime, order_qty, ...
 INFO, 09:44:00.406, Trans[1003] Binding Insert [or_order_detail]  set[cretime=200...
 INFO, 09:44:00.406, Trans[1003] Inserted [app.data.test.OrderDetail] [5] 
 INFO, 09:44:00.406, Trans[1003] insert into or_order_detail (cretime, order_qty, ...
 INFO, 09:44:00.406, Trans[1003] Binding Insert [or_order_detail]  set[cretime=200...
 INFO, 09:44:00.406, Trans[1003] Inserted [app.data.test.OrderDetail] [6] 
 INFO, 09:44:00.406, Trans[1003] Batch flush 

Introduction User Guide (pdf) Install/Configure Public JavaDoc Whitepapers
General Database Specific Byte Code Deployment Annotations Features
Top Bugs Top Enhancements
woResponse