JPA初探05---一対多の双方向関連

6463 ワード

  :
   --->    
        

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);
	}
}