JPA初探05---一对多的双向关联

关系:
订单 ---> 订单项
一对多的双向关联

JPA规范:一对多的双向关联,多的一方是负责关系维护,负责外键的更新。关系的被维护段是没有权利更新外键的。其实我们仔细理解:本来一对多的关系中多方是记录一方的外键,那么肯定是由多方去更新自己的外键。


【Java类设计,包含数据库设计】
package OneToMany;

import java.util.Date;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/**
 * 订单类
 * @author 青山
 * @date 2014年10月19日
 * @file name: Order.java
 * 
 *  create table t_order(
		orderid int primary key,
		orderdate date,
		summoney numeric(10,2)
	);
	和订单明细时一对多的双向关联
 */
@Entity
@Table(name="t_order")
public class Order {
	@Id
	@Column(name="orderid")
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private int orderid;
	
	@Temporal(TemporalType.DATE)
	@Column(name="orderdate")
	private Date date;
	
	//mappedBy相当于在hibernate中配置set的时候的inverse的配置
	@OneToMany(mappedBy="order",fetch=FetchType.LAZY,cascade={CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REFRESH,CascadeType.REMOVE})
	private Set<OrderItem> items;
	
	@Column(name="summoney")
	private double sum;
	
	public int getOrderid() {
		return orderid;
	}
	public void setOrderid(int orderid) {
		this.orderid = orderid;
	}
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	public Set<OrderItem> getItems() {
		return items;
	}
	public void setItems(Set<OrderItem> items) {
		this.items = items;
	}
	public double getSum() {
		return sum;
	}
	public void setSum(double sum) {
		this.sum = sum;
	}
}

【订单项】
package OneToMany;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/**
 * 订单明细类
 * @author 青山
 * @date 2014年10月19日
 * @file name: OrderItem.java
 * 
 *  create table t_orderitem(
		itemid int primary key AUTO_INCREMENT,
		productname varchar(45),
		productcount int,
		price numeric(10,2),
		pk_orderid int
	);

 */
@Entity
@Table(name="t_orderitem")
public class OrderItem {
	@Id
	@Column(name="itemid")
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private int oiid;
	
	@Column(name="productname")
	private String productName;
	
	@Column(name="productcount")
	private int count;
	
	@Column(name="price")
	private float price;
	
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="pk_orderid")
	private Order order;
	
	
	public int getOiid() {
		return oiid;
	}
	public void setOiid(int oiid) {
		this.oiid = oiid;
	}
	public String getProductName() {
		return productName;
	}
	public void setProductName(String productName) {
		this.productName = productName;
	}
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
	public float getPrice() {
		return price;
	}
	public void setPrice(float price) {
		this.price = price;
	}
	public Order getOrder() {
		return order;
	}
	public void setOrder(Order order) {
		this.order = order;
	}
	
	
}
【测试】
package OneToMany;

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

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import util.JPAManagerFactory;

public class TestOneMany {
	public static void main(String[] args) {
		EntityManager manager = JPAManagerFactory.getMFInstance().getManager();
		EntityTransaction et = manager.getTransaction();
		et.begin();
		
//		add(manager);
//		query(manager);
		query2(manager);
		
		et.commit();
		manager.close();
	}
	
	 static void query(EntityManager manager) {
		Order order = manager.find(Order.class, 2);
		System.out.println(order.getDate());
		Set<OrderItem> set = order.getItems();
		System.out.println(set.size());
		for(OrderItem item : set){
			System.out.println(item.getProductName());
		}
	}
	
	 static void query2(EntityManager manager) {
		OrderItem item = manager.find(OrderItem.class, 3);
		System.out.println(item.getProductName());
		Order order = item.getOrder();
		System.out.println(order.getDate()+" "+order.getSum());
		System.out.println(order.getItems().size());
	}

	static void add(EntityManager manager){
		Order order = new Order();
		order.setDate(new Date());
		order.setSum(10);
		
		Set<OrderItem> set = new HashSet<OrderItem>();
		OrderItem item1 = new OrderItem();
		item1.setCount(5);
		item1.setPrice(15.5f);
		item1.setProductName("IPhone4S");
		
		
		OrderItem item2 = new OrderItem();
		item2.setCount(5);
		item2.setPrice(15.5f);
		item2.setProductName("IPhone6");
		
		set.add(item2);
		set.add(item1);
		
		order.setSum(item1.getPrice()*item1.getCount()+item2.getPrice()*item2.getCount());
		
		order.setItems(set);
		
		item1.setOrder(order);
		item2.setOrder(order);
		
		manager.persist(order);
	}
}

 

你可能感兴趣的:(jpa)