Plain Java objects with persistence storage. They are not remotable and must be accessed through the new javax.persistence.EntityManager service. An entity bean implementation is provided in the following three files/classesBeansOrder.javaLineItem.javaEntity ManagerShoppingCartBean.java//Order.javaimport 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.OneToMany;import javax.persistence.Table;import javax.persistence.Id;import javax.persistence.CascadeType;import javax.persistence.FetchType;import java.util.ArrayList;import java.util.Collection;@Entity@Table(name = "PURCHASE_ORDER")public class Order implements java.io.Serializable{ private int id; private double total; private Collection lineItems; @Id @GeneratedValue(strategy=GenerationType.AUTO) public int getId() { return id; } public void setId(int id) { this.id = id; } public double getTotal() { return total; } public void setTotal(double total) { this.total = total; } public void addPurchase(String product, int quantity, double price) { if (lineItems == null) lineItems = new ArrayList(); LineItem item = new LineItem(); item.setOrder(this); item.setProduct(product); item.setQuantity(quantity); item.setSubtotal(quantity * price); lineItems.add(item); total += quantity * price; } @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy="order") public Collection getLineItems() { return lineItems; } public void setLineItems(Collection lineItems) { this.lineItems = lineItems; }}//LineItem.javaimport 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.Entity;@Entitypublic class LineItem implements java.io.Serializable{ private int id; private double subtotal; private int quantity; private String product; private Order order; @Id @GeneratedValue(strategy=GenerationType.AUTO) public int getId() { return id; } public void setId(int id) { this.id = id; } public double getSubtotal() { return subtotal; } public void setSubtotal(double subtotal) { this.subtotal = subtotal; } public int getQuantity() { return quantity; } public void setQuantity(int quantity) { this.quantity = quantity; } public String getProduct() { return product; } public void setProduct(String product) { this.product = product; } @ManyToOne @JoinColumn(name = "order_id") public Order getOrder() { return order; } public void setOrder(Order order) { this.order = order; }}//ShoppingCartBean.javaInteracts with Order as a plain Java object. The checkout() method stores orders with persistence storage by using the required EntityManager service.import javax.ejb.Remote;import javax.ejb.Remove;import javax.ejb.Stateful;import javax.persistence.EntityManager;import javax.persistence.PersistenceContext;import javax.persistence.PersistenceContext;@Stateful@Remote(ShoppingCart.class)public class ShoppingCartBean implements ShoppingCart, java.io.Serializable{ @PersistenceContext private EntityManager manager; private Order order; public void buy(String product, int quantity, double price) { if (order == null) order = new Order(); order.addPurchase(product, quantity, price); } public Order getOrder() { return order; } @Remove public void checkout() { manager.persist(order); }} GNU Distribution Without Warranty License
From: http://sitestree.com/?p=4834
Categories:Java Short Notes
Tags:
Post Data:2011-12-03 23:27:24
Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada