package com.avaje.tests.model.basic; import java.io.Serializable; import java.sql.Date; import java.sql.Timestamp; import java.util.ArrayList; import java.util.List; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.EnumType; import javax.persistence.Enumerated; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.OrderBy; import javax.persistence.Table; import javax.persistence.Transient; import javax.persistence.Version; import javax.xml.bind.annotation.XmlType; import com.avaje.ebean.annotation.CreatedTimestamp; import com.avaje.ebean.annotation.Formula; import com.avaje.ebean.annotation.Where; import com.avaje.ebean.validation.NotNull; /** * Order entity bean. */ @Entity @Table(name="o_order") public class Order implements Serializable { private static final long serialVersionUID = 1L; @XmlType(name="status") public enum Status { NEW, APPROVED, SHIPPED, COMPLETE } public Order(){ } @Id Integer id; /** * Derived total amount from the order details. Needs to be explicitly included in query as Transient. * Removing the Transient would mean by default it would be included in a order query. *
* NOTE: The join clause for totalAmount and totalItems is the same. If your query includes both * totalAmount and totalItems only the one join is added to the query. *
*/ @Transient @Formula( select="z_b${ta}.total_amount", join="join (select order_id, count(*) as total_items, sum(order_qty*unit_price) as total_amount from o_order_detail group by order_id) z_b${ta} on z_b${ta}.order_id = ${ta}.id") Double totalAmount; /** * Derived total item count from the order details. Needs to be explicitly included in query as Transient. */ @Transient @Formula( select="z_b${ta}.total_items", join="join (select order_id, count(*) as total_items, sum(order_qty*unit_price) as total_amount from o_order_detail group by order_id) z_b${ta} on z_b${ta}.order_id = ${ta}.id") Integer totalItems; @Enumerated(value=EnumType.ORDINAL) Status status = Status.NEW; Date orderDate = new Date(System.currentTimeMillis()); Date shipDate; @NotNull @ManyToOne @JoinColumn(name="kcustomer_id") Customer customer; //@Basic(fetch=FetchType.LAZY) @Column(name="name",table="o_customer") String customerName; @CreatedTimestamp Timestamp cretime; @Version Timestamp updtime; @Where(clause="${ta}.id > 0") @OneToMany(cascade=CascadeType.ALL, mappedBy="order") @OrderBy("id asc, orderQty asc, cretime desc") List