Jboss Ejb3.0 Entity Bean

Source: Internet
Author: User
Tags key log log log return table name client jboss log4j





















Order.java



Package 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 isn ' t specified it defaults to the bean name



of the class. For instance, the LineItem EJB would is mapped to



The LINEITEM table



public class Order implements Java.io.Serializable



{



private int id;



Private double total;



Private collection<lineitem> 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)



{



if (lineitems = = null) LineItems = new arraylist<lineitem> ();



LineItem item = new LineItem ();



Item.setorder (this);



Item.setproduct (product);



Item.setquantity (quantity);



Item.setsubtotal (quantity * price);



Lineitems.add (item);



Total + = quantity * PRICE;



}







Cascadetype.all specifies that's created,



Any lineitems held in the LineItems collection would be created



As OK (cascadetype.persist). If The order was delete from



Persistence storage, all related lineitems would be



Deleted (Cascadetype.remove). If instance is



reattached to persistence storage, any changes to the



LineItems Collection would be merged with persistence



Storage (Cascadetype.merge).



Fetchtype.eager specifies that the ' when ' is loaded



Whether or not to prefetch the relationship as.



If you are want the lineitems to is loaded on demand, then specify.



The Mappedby attribute specifies this is a bi-directional



Relationship is managed by the LineItem entity Bean.



@OneToMany (cascade = cascadetype.all, fetch = Fetchtype.eager, mappedby= "order")



Public collection<lineitem> Getlineitems ()



{



return lineitems;



}







public void Setlineitems (collection<lineitem> lineitems)



{



This.lineitems = LineItems;



}



}







Added a lot of English notes, I hope to understand.



@Table (name = "Purchase_order")



Named database (JBoss database for hsqldb) in the corresponding table name, default to class name, such as the following LineItem there is no @table. (Order.java, Lineitem.java are both O/R maps, worth comparing)



@Id (generate = Generatortype.auto)



Increment, same as in the database. Must be annotated on the Getter method.



@OneToMany (cascade = cascadetype.all, fetch = Fetchtype.eager, mappedby= "order")



Relationship: Order and LineItem are 1 pairs of relationships. The cascadetype.all here is to indicate that when an order is instantiated, both the LineItem should be established at the same time. In the annotation, the different cascadetype are defined according to the difference of the time between the two sets.











Lineitem.java



Package 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;







@Entity



public class LineItem implements Java.io.Serializable



{



private int id;



private double subtotal;



private int quantity;



Private String product;



Private 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.java



Package Org.jboss.tutorial.entity.bean;







Import Javax.ejb.Remote;



Import Javax.ejb.Remove;







@Remote



public interface ShoppingCart



{



void Buy (String product, int quantity, double);







Order GetOrder ();







@Remove void Checkout ();



}















Shoppingcartbean.java



Package Org.jboss.tutorial.entity.bean;







Import Javax.persistence.EntityManager;



Import Javax.ejb.Inject;



Import Javax.ejb.Remove;



Import javax.ejb.Stateful;











@Stateful



public 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;







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);



}



}







Entitymanager can be used to query, create, delete entity beans, insert a entitymanager, and save data in a database when the last user leaves, which is equivalent to saving an object. After running the Client.java, you can see two tables with Purchase_order and LineItem on the hsqldb. Hypersonicdatabase, find Startdatabasemanager and then there is an invoke, click on the access to HSQLDB.











Client.java



Package org.jboss.tutorial.entity.client;











Import Org.jboss.tutorial.entity.bean.LineItem;



Import Org.jboss.tutorial.entity.bean.Order;



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 log4j.properties in Jboss-ejb-3.0_preview_5.zip without this always shows a lack of appender. With this, a record.log log file will be generated in this directory.







Log4j.properties



Log4j.appender.r=org.apache.log4j.rollingfileappender



Log4j.appender.r.file=record.log



Log4j.appender.r.layout=org.apache.log4j.patternlayout



Log4j.appender.r.layout.conversionpattern=%p%d{hh:mm:ss}%t%c{1}-%m%n



Log4j.appender.r.maxbackupindex=1



log4j.appender.r.maxfilesize=100kb



Log4j.appender.stdout.layout=org.apache.log4j.patternlayout



log4j.appender.stdout.layout.conversionpattern=%5p [%t] (%f:%l)-%m%n



Log4j.appender.stdout=org.apache.log4j.consoleappender



Log4j.rootlogger=stdout,r















Running: Reference installing.html



Under Windows



Open command Prompt cmd, to Jboss_home/bin



Run.bat–c All



With Ant



Just build and run.
























Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.