jpa onetomany
2009-12-30 21:10:07 来源:WEB开发网核心提示:view plaincopy to clipboardPRint?package com.hujuan.bean; import java.util.HashSet; import java.util.Set; import javax.persistence.CascadeType; import javax.per
view plaincopy to clipboardPRint?
package com.hujuan.bean;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="orders")
public class Order {
private String id;
private Float amount;
private Set<OrderItem> items = new HashSet<OrderItem>();
@Id @Column(length=10)
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Column(nullable=false)
public Float getAmount() {
return amount;
}
public void setAmount(Float amount) {
this.amount = amount;
}
//级联操作
@OneToMany(cascade= {CascadeType.REFRESH,CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REMOVE},fetch=FetchType.LAZY,mappedBy="order")
public Set<OrderItem> getItems() {
return items;
}
public void setItems(Set<OrderItem> items) {
this.items = items;
}
public void addOrderItem(OrderItem orderItem){
orderItem.setOrder(this);
this.items.add(orderItem);
}
}
package com.hujuan.bean;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="orders")
public class Order {
private String id;
private Float amount;
private Set<OrderItem> items = new HashSet<OrderItem>();
@Id @Column(length=10)
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Column(nullable=false)
public Float getAmount() {
return amount;
}
public void setAmount(Float amount) {
this.amount = amount;
}
//级联操作
@OneToMany(cascade= {CascadeType.REFRESH,CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REMOVE},fetch=FetchType.LAZY,mappedBy="order")
public Set<OrderItem> getItems() {
return items;
}
public void setItems(Set<OrderItem> items) {
this.items = items;
}
public void addOrderItem(OrderItem orderItem){
orderItem.setOrder(this);
this.items.add(orderItem);
}
}
Cascade 级联操作
CascadeType. PERSIST 级联持久化 ( 保存 ) 操作
CascadeType. MERGE 级联更新 ( 合并 ) 操作
CascadeType. REFRESH 级联刷新操作,只会查询获取操作
CascadeType. REMOVE 级联删除操作
CascadeType. ALL 级联以上全部操作
Fetch 抓取是否延迟加载,默认情况一的方为立即加载,多的一方为延迟加载
mappedBy 关系维护
mappedBy= "order" 表示在 OrderItem 类中的 order 属性来维护关系
view plaincopy to clipboardprint?
package com.hujuan.bean;
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 {
private Integer id;
private String productName;
private Float sellPrice = 0f;
private Order order;
@Id @GeneratedValue
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(length=30,nullable=false)
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
@Column(nullable=false)
public Float getSellPrice() {
return sellPrice;
}
public void setSellPrice(Float sellPrice) {
this.sellPrice = sellPrice;
}
//optional=true表示该对象(Order)是可选的,对象(Order)可以不存在
//值为false表示不可选即该对象(Order)必须存在,则在数据库中的外键就不能为null)
@ManyToOne(cascade={CascadeType.MERGE,CascadeType.REFRESH},optional=false)
//定义外键名称
@JoinColumn(name="order_id")
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/SuperBoo/archive/2009/12/30/5103913.aspx
package com.hujuan.bean;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="orders")
public class Order {
private String id;
private Float amount;
private Set<OrderItem> items = new HashSet<OrderItem>();
@Id @Column(length=10)
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Column(nullable=false)
public Float getAmount() {
return amount;
}
public void setAmount(Float amount) {
this.amount = amount;
}
//级联操作
@OneToMany(cascade= {CascadeType.REFRESH,CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REMOVE},fetch=FetchType.LAZY,mappedBy="order")
public Set<OrderItem> getItems() {
return items;
}
public void setItems(Set<OrderItem> items) {
this.items = items;
}
public void addOrderItem(OrderItem orderItem){
orderItem.setOrder(this);
this.items.add(orderItem);
}
}
package com.hujuan.bean;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="orders")
public class Order {
private String id;
private Float amount;
private Set<OrderItem> items = new HashSet<OrderItem>();
@Id @Column(length=10)
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Column(nullable=false)
public Float getAmount() {
return amount;
}
public void setAmount(Float amount) {
this.amount = amount;
}
//级联操作
@OneToMany(cascade= {CascadeType.REFRESH,CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REMOVE},fetch=FetchType.LAZY,mappedBy="order")
public Set<OrderItem> getItems() {
return items;
}
public void setItems(Set<OrderItem> items) {
this.items = items;
}
public void addOrderItem(OrderItem orderItem){
orderItem.setOrder(this);
this.items.add(orderItem);
}
}
Cascade 级联操作
CascadeType. PERSIST 级联持久化 ( 保存 ) 操作
CascadeType. MERGE 级联更新 ( 合并 ) 操作
CascadeType. REFRESH 级联刷新操作,只会查询获取操作
CascadeType. REMOVE 级联删除操作
CascadeType. ALL 级联以上全部操作
Fetch 抓取是否延迟加载,默认情况一的方为立即加载,多的一方为延迟加载
mappedBy 关系维护
mappedBy= "order" 表示在 OrderItem 类中的 order 属性来维护关系
view plaincopy to clipboardprint?
package com.hujuan.bean;
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 {
private Integer id;
private String productName;
private Float sellPrice = 0f;
private Order order;
@Id @GeneratedValue
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(length=30,nullable=false)
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
@Column(nullable=false)
public Float getSellPrice() {
return sellPrice;
}
public void setSellPrice(Float sellPrice) {
this.sellPrice = sellPrice;
}
//optional=true表示该对象(Order)是可选的,对象(Order)可以不存在
//值为false表示不可选即该对象(Order)必须存在,则在数据库中的外键就不能为null)
@ManyToOne(cascade={CascadeType.MERGE,CascadeType.REFRESH},optional=false)
//定义外键名称
@JoinColumn(name="order_id")
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/SuperBoo/archive/2009/12/30/5103913.aspx
赞助商链接