JPA+Hibernate 3.3 学习小结——双向多对一关联及级联操作

关系映射
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 属性来维护关系
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;
    }
}



你可能感兴趣的:(JPA+Hibernate 3.3 学习小结——双向多对一关联及级联操作)