欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

EJB3实体映射之OneToMany

程序员文章站 2024-02-22 22:01:04
...
Order 类:

import java.io.Serializable;
import java.util.HashSet;
import java.util.Date;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name = "Orders")
public class Order implements Serializable {

private static final long serialVersionUID = 4970325922198249712L;
private Integer orderid;
private Float amount;
private Set<OrderItem> orderItems = new HashSet<OrderItem>();
private Date createdate;

@Id
@GeneratedValue
public Integer getOrderid() {
return orderid;
}
public void setOrderid(Integer orderid) {
this.orderid = orderid;
}

public Float getAmount() {
return amount;
}
public void setAmount(Float amount) {
this.amount = amount;
}
// ①Order中保留OrderItem集合 关联OrderItem中的order属性
// ②级联方式为全部级联 
// ③Many的一方延迟加载 fetch = FetchType.LAZY
// ④OrderBy 指定排序字段和方式
@OneToMany(mappedBy="order",cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@OrderBy(value = "id ASC")
public Set<OrderItem> getOrderItems() {
return orderItems;
}
public void setOrderItems(Set<OrderItem> orderItems) {
this.orderItems = orderItems;
}

@Temporal(value=TemporalType.TIMESTAMP)
public Date getCreatedate() {
return createdate;
}
public void setCreatedate(Date createdate) {
this.createdate = createdate;
}

public void addOrderItem(OrderItem orderitem) {
if (!this.orderItems.contains(orderitem)) {
this.orderItems.add(orderitem);
orderitem.setOrder(this);
}
}

public void removeOrderItem(OrderItem orderitem) {
if (this.orderItems.contains(orderitem)) {
orderitem.setOrder(null);
this.orderItems.remove(orderitem);
}
}

/**
* 返回对象的散列代码值。该实现根据此对象
* 中 orderid 字段计算散列代码值。
* @return 此对象的散列代码值。
*/
@Override
public int hashCode() {
int hash = 0;
hash += (this.orderid != null ? this.orderid.hashCode() : 0);
return hash;
}

/**
* 确定其他对象是否等于此 Order。当且仅当
* 参数不为 null 且该参数是具有与此对象相同 orderid 字段值的 Order 对象时,
* 结果才为 <code>true</code>。
* @param 对象,要比较的引用对象
* 如果此对象与参数相同,则 @return <code>true</code>;
* 否则为 <code>false</code>。
*/
@Override
public boolean equals(Object object) {
if (!(object instanceof Order)) {
return false;
}
Order other = (Order)object;
if (this.orderid != other.orderid && (this.orderid == null || !this.orderid.equals(other.orderid))) return false;
return true;
}

/**
* 返回对象的字符串表示法。该实现根据 orderid 字段
* 构造此表示法。
* @return 对象的字符串表示法。
*/
@Override
public String toString() {
return this.getClass().getName()+ "[orderid=" + orderid + "]";
}
}



OrderItem 类:

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
public class OrderItem implements Serializable {

private static final long serialVersionUID = -1166337687856636179L;
private Integer id;
private String productname;
private Float price;
private Order order;

public OrderItem() {}

public OrderItem(String productname, Float price) {
this.productname = productname;
this.price = price;
}

@Id
@GeneratedValue
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}

@Column(length=100, nullable=false)
public String getProductname() {
return productname;
}
public void setProductname(String productname) {
this.productname = productname;
}

public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
// ①OrderItem保留一个Order 映射OrderItem表中order_id外键字段
// ②级联方式为级联刷新 
// ③optional=false不可选,必须存在
@ManyToOne(cascade=CascadeType.REFRESH, optional=false)
@JoinColumn(name = "order_id")
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}

/**
* 返回对象的散列代码值。该实现根据此对象
* 中 id 字段计算散列代码值。
* @return 此对象的散列代码值。
*/
@Override
public int hashCode() {
int hash = 0;
hash += (this.id != null ? this.id.hashCode() : super.hashCode());
return hash;
}

/**
* 确定其他对象是否等于此 OrderItem。当且仅当
* 参数不为 null 且该参数是具有与此对象相同 id 字段值的 OrderItem 对象时,
* 结果才为 <code>true</code>。
* @param 对象,要比较的引用对象
* 如果此对象与参数相同,则 @return <code>true</code>;
* 否则为 <code>false</code>。
*/
@Override
public boolean equals(Object object) {
if (!(object instanceof OrderItem)) {
return false;
}
OrderItem other = (OrderItem)object;
if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) return false;
return true;
}

/**
* 返回对象的字符串表示法。该实现根据 id 字段
* 构造此表示法。
* @return 对象的字符串表示法。
*/
@Override
public String toString() {
return this.getClass().getName()+ "[id=" + id + "]";
}
}