|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
微软什么都提供了。你可以试想一下,如果你是新手,你是希望你点一下按钮程序就能运行那,还是想自己一点一点的组织结构,然后打包发部,调错再打包......
Order.java
packageorg.jboss.tutorial.entity.bean;
importjavax.persistence.CascadeType;
importjavax.persistence.Entity;
importjavax.persistence.FetchType;
importjavax.persistence.GeneratorType;
importjavax.persistence.Id;
importjavax.persistence.OneToMany;
importjavax.persistence.Table;
importjavax.persistence.Id;
importjavax.persistence.CascadeType;
importjavax.persistence.FetchType;
importjava.util.ArrayList;
importjava.util.Collection;
@Entity
@Table(name="PURCHASE_ORDER")
//Ifthetablenameisntspecifieditdefaultstothebeanname
//oftheclass.Forinstance,theLineItemEJBwouldbemappedto
//theLINEITEMtable
publicclassOrderimplementsjava.io.Serializable
{
privateintid;
privatedoubletotal;
privateCollection<LineItem>lineItems;
@Id(generate=GeneratorType.AUTO)
publicintgetId()
{
returnid;
}
publicvoidsetId(intid)
{
this.id=id;
}
publicdoublegetTotal()
{
returntotal;
}
publicvoidsetTotal(doubletotal)
{
this.total=total;
}
publicvoidaddPurchase(Stringproduct,intquantity,doubleprice)
{
if(lineItems==null)lineItems=newArrayList<LineItem>();
LineItemitem=newLineItem();
item.setOrder(this);
item.setProduct(product);
item.setQuantity(quantity);
item.setSubtotal(quantity*price);
lineItems.add(item);
total+=quantity*price;
}
//CascadeType.ALLspecifiesthatwhenanOrderiscreated,
//anyLineItemsheldinthelineItemscollectionwillbecreated
//aswell(CascadeType.PERSIST).IftheOrderisdeletefrom
//persistencestorage,allrelatedLineItemswillbe
//deleted(CascadeType.REMOVE).IfanOrderinstanceis
//reattachedtopersistencestorage,anychangestothe
//LineItemscollectionwillbemergedwithpersistence
//storage(CascadeType.MERGE).
//FetchType.EAGERspecifiesthatwhentheOrderisloaded
//whetherornottoprefetchtherelationshipaswell.
//IfyouwanttheLineItemstobeloadedondemand,thenspecifyFetchType.LAZY.
//ThemappedByattributespecifiesthatthisisabi-directional
//relationshipthatismanagedbytheorderpropertyontheLineItementitybean.
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER,mappedBy="order")
publicCollection<LineItem>getLineItems()
{
returnlineItems;
}
publicvoidsetLineItems(Collection<LineItem>lineItems)
{
this.lineItems=lineItems;
}
}
加了很多英文正文,但愿能看得懂。
@Table(name="PURCHASE_ORDER")
指名数据库(jboss的数据库为hsqldb)中对应得表的名字,默许为class的名字,好比上面的LineItem就没有@table。(order.java,LineItem.java都为o/r映照,值得相互对比)
@Id(generate=GeneratorType.AUTO)
递增,同数据库里的不异。必需在getter办法上标注。
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER,mappedBy="order")
干系:order和LineItem为1对多的干系。在这里CascadeType.ALL是指明当创建一个Order被实例化后,都应当同时同时创建LineItem。正文里的就是依据二者创建工夫的分歧,界说了分歧的CascadeType。
LineItem.java
packageorg.jboss.tutorial.entity.bean;
importjavax.persistence.Entity;
importjavax.persistence.GeneratorType;
importjavax.persistence.Id;
importjavax.persistence.JoinColumn;
importjavax.persistence.ManyToOne;
importjavax.persistence.Entity;
@Entity
publicclassLineItemimplementsjava.io.Serializable
{
privateintid;
privatedoublesubtotal;
privateintquantity;
privateStringproduct;
privateOrderorder;
@Id(generate=GeneratorType.AUTO)
publicintgetId()
{
returnid;
}
publicvoidsetId(intid)
{
this.id=id;
}
publicdoublegetSubtotal()
{
returnsubtotal;
}
publicvoidsetSubtotal(doublesubtotal)
{
this.subtotal=subtotal;
}
publicintgetQuantity()
{
returnquantity;
}
publicvoidsetQuantity(intquantity)
{
this.quantity=quantity;
}
publicStringgetProduct()
{
returnproduct;
}
publicvoidsetProduct(Stringproduct)
{
this.product=product;
}
//The@JoinColumnspecifiestheforeignkeycolumnwithintheLineItemtable.
@ManyToOne
@JoinColumn(name="order_id")
publicOrdergetOrder()
{
returnorder;
}
publicvoidsetOrder(Orderorder)
{
this.order=order;
}
}
ShoppingCart.java
packageorg.jboss.tutorial.entity.bean;
importjavax.ejb.Remote;
importjavax.ejb.Remove;
@Remote
publicinterfaceShoppingCart
{
voidbuy(Stringproduct,intquantity,doubleprice);
OrdergetOrder();
@Removevoidcheckout();
}
ShoppingCartBean.java
packageorg.jboss.tutorial.entity.bean;
importjavax.persistence.EntityManager;
importjavax.ejb.Inject;
importjavax.ejb.Remove;
importjavax.ejb.Stateful;
@Stateful
publicclassShoppingCartBeanimplementsShoppingCart
{
@Inject
privateEntityManagermanager;//TheEntityManagerisusedtodoquerying,
//creating,findbyprimarykey,andremovalofentitybeans.
privateOrderorder;
publicvoidbuy(Stringproduct,intquantity,doubleprice)
{
if(order==null)order=newOrder();
order.addPurchase(product,quantity,price);
}
publicOrdergetOrder()
{
returnorder;
}
@Remove
publicvoidcheckout()
{
manager.persist(order);
}
}
EntityManager的感化可年夜了,能够用于查询,创立,删除实体Bean,这里拔出个EntityManager,次要是在最初用户分开时,在数据库里保留数据,相称于保留个object。运转完Client.java后就能够在hsqldb上看到有purchase_order和LineItem的两张表。HypersonicDatabase,找到startDatabaseManager然后上面有个invoke,点击就能够会见hsqldb了。
Client.java
packageorg.jboss.tutorial.entity.client;
importorg.jboss.tutorial.entity.bean.LineItem;
importorg.jboss.tutorial.entity.bean.Order;
importorg.jboss.tutorial.entity.bean.ShoppingCart;
importjavax.naming.InitialContext;
publicclassClient
{
publicstaticvoidmain(String[]args)throwsException
{
InitialContextctx=newInitialContext();
ShoppingCartcart=(ShoppingCart)ctx.lookup(ShoppingCart.class.getName());
System.out.println("Buying2memorysticks");
cart.buy("Memorystick",2,500.00);
System.out.println("Buyingalaptop");
cart.buy("Laptop",1,2000.00);
System.out.println("Printcart:");
Orderorder=cart.getOrder();
System.out.println("Total:$"+order.getTotal());
for(LineItemitem:order.getLineItems())
{
System.out.println(item.getQuantity()+""+item.getProduct()+""+item.getSubtotal());
}
System.out.println("Checkout");
cart.checkout();
}
}
这里附上log4j.properties在jboss-EJB-3.0_Preview_5.zip 内里没有这个总是显现短少appender。有了这个将在该目次下天生个record.log日记文件。
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
运转:参考installing.html
Windows下
翻开命令提醒符cmd,到jboss_home/bin
Run.bat–call
用ant
先build后run就好了。
他们对jsp,servlet,javabean进行封装就是为了展示他们的某个思想,与java的开发并没有必然的关系,也不见得在所以情况下,别人使用起来会简单。 |
|