JBoss EJB3.0 Entity Bean

xiaoxiao2021-03-05  56

Order.javapackage org.jboss.tutorial.entity.bean; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratorType; 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") // If the table name is not specified it defaults to the bean name // of the class. For instance, the LineItem EJB would be mapped to // the LINEITEM tablepublic 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 {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; item.setsubtotal; lineItems.add (item); Total = Quantity * Price;

} //CascadeType.ALL specifies that when an Order is created, // any LineItems held in the lineItems collection will be created // as well (CascadeType.PERSIST). If the Order is delete from // persistence storage, all related LineItems will be // deleted (CascadeType.REMOVE). If an Order instance is // reattached to persistence storage, any changes to the // LineItems collection will be merged with persistence // storage (CascadeType.MERGE). //FetchType.EAGER specifies that when the Order is loaded // whether or not to prefetch the relationship as well. // If you want the LineItems to be loaded on demand, then specify FetchType.LAZY. // The mappedBy attribute specifies that this is a bi- directional // relationship that is managed by the order property on the LineItem entity bean @OneToMany (cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "order") public Collection getLineItems () {return lineItems.; Public void setline Items (Collection lineItems) {this.lineItems = lineItems;}} has added a lot of English comments, I hope to understand. @Table (name = "purchase_order") Name The name of the corresponding table in the name database (JBoss database is HSQLDB), the name of the Class is the name of the Class, such as the LineItem below. @Table. (Order.java, LineItem.java is O / R mapping, worthy of mutual control) @ID (generate = generatorType.Auto) increments, the same as the database. Must be marked on the Getter method. @ONETOMANY (cascade = cascadtype.all, fetch = fetchtype.eager, mappedby = "order") relationship: ORDER and LINEITEM are 1-to-many relationships. Here Cascadetype.all is specifying that LineItem should be created while establishing an Order instantiation. The comment is based on the difference between the two, defines different cascadetypes.

LineItem.javapackage org.jboss.tutorial.entity.bean; import javax.persistence.Entity; import javax.persistence.GeneratorType; 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 (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;} // The @JoinColumn specifies the foreign key column within the LineItem table @ManyToOne @JoinColumn (name = "order_id") public Order getOrder () {return order;}. Public void setOrder (Order order) {this.order = order;}} ShoppingCart.javapackage org.jboss.tutorial.entity.bean; import javax.ejb.Remote; import javax.ejb.Remove; @Remotepublic interface ShoppingCart {void buy (String product, int quantity, double price); Order getOrder (); @Remove void checkout ();} ShoppingCartBean.javapackage org.jboss.tutorial.entity.bean; import javax.persistence.EntityManager; import javax.ejb.Inject; import javax.ejb.Remove; import javax.ejb.Stateful; @

Statefulpublic class ShoppingCartBean implements ShoppingCart {@Inject private EntityManager manager; // The EntityManager is used to do querying, // creating, find by primary key, and removal of entity beans private Order order;. Public void buy (String product, int quantity Double price) {if (order == null) Order = new order (); Order.addpurchase;} public order getorder () {Return Order;} @remove public void checkout () {Manager .PERSIST (ORDER);}} EntityManager's role can be used, can be used for query, creation, delete entity bean, here inserting an EntityManager, mainly in the database, saving data in the database, equivalent to saving an Object . After running Client.java, you can see two tables with Purchase_order and LineItem on HSQLDB. HypersonicDatabase, find StartDatabaseManager and then have an invoke, click to access HSQLDB.

Client.javaPackage Org.jboss.Tutorial.Entity.Client; import org.jboss.tutorial.entity.bean.LineItem; import org.jboss.tutorial.entity.Bean.Rder; import Org.jboss.tutorial.Entity.bean. ShoppingCart; import javax.naming.InitialContext; public class Client {public static void main (String [] args) throws Exception {InitialContext ctx = new InitialContext (); ShoppingCart cart = (ShoppingCart) ctx.lookup (ShoppingCart.class.getName ( )); 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 ();}} here attached to log4j .properties does not have this antenge to show the lack of appender in jboss-ejb-3.0_preview_5.zip. With this will generate a replard.log log file in this directory.

转载请注明原文地址:https://www.9cbs.com/read-34580.html

New Post(0)