Jboss Ejb3.0 Entity Bean
2008-02-23 09:51:35来源:互联网 阅读 ()
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 be 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 price)
{
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 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<LineItem> getLineItems()
{
return lineItems;
}
public void setLineItems(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
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
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash