[jboss-user] [EJB 3.0] - Re: what happen with my entity bean , need expert help
ejbiva
do-not-reply at jboss.com
Wed Jul 18 22:50:30 EDT 2007
hi ALRubinger,the SessionBean just as follows
@Stateful
public class ShoppingCartBean implements ShoppingCart
{
@Inject
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;
}
public void checkout()
{
manager.create(order);
}
}
/// Entity bean
@Entity
@Table(name = "PURCHASE_ORDER")
public class Order implements java.io.Serializable
{
private int id;
private double total;
private Collection lineItems;
@Id(generate = GeneratorType.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)
@JoinColumn(name = "order_id")
public Collection getLineItems()
{
return lineItems;
}
public void setLineItems(Collection lineItems)
{
this.lineItems = lineItems;
}
}
@Entity
@Table(name = "order_line_item")
public class LineItem implements java.io.Serializable
{
private int id;
private double subtotal;
private int quantity;
private String product;
private Order order;
@Id(generate = GeneratorType.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;
}
}
/////// and testing console as follows
public class Client
{
public static void main(String[] args) throws Exception
{
InitialContext ctx = new InitialContext();
// ShoppingCart cart = (ShoppingCart) ctx.lookup(ShoppingCart.class.getName());
//Calculator calculator = (Calculator) ctx.lookup("CalculatorBean/remote");
ShoppingCart cart = (ShoppingCart) ctx.lookup("ShoppingCartBean/remote");
System.out.println("Buying 2 memory sticks");
cart.buy("Memory stick", 2, 500.00);
System.out.println("Buying a laptop");
cart.buy("Laptop", 1, 2000.00);
System.out.println("Print cart:");
Order order = cart.getOrder();
System.out.println("Total: $" + order.getTotal());
for (LineItem item : order.getLineItems())
{
System.out.println(item.getQuantity() + " " + item.getProduct() + " " + item.getSubtotal());
}
System.out.println("Checkout");
cart.checkout();
}
}
any defect?
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4065630#4065630
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4065630
More information about the jboss-user
mailing list