JPA OneToMany映射

二话不说上代码:

package com.jvwl.model;

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

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
/**
 * 订单实体
 * @author Jerval
 *
 */
@Entity
@Table(name="t_order")
public class Order {

	private String orderId;
	private Float amount=0f;
	private Set<OrderItem> items = new HashSet<OrderItem>();

	@Id @Column(length=40)
	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	@Column(nullable=false)
	public Float getAmount() {
		return amount;
	}

	public void setAmount(Float amount) {
		this.amount = amount;
	}

	@OneToMany(cascade={CascadeType.ALL},
			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);
		items.add(orderItem);
	}
}

 

package com.jvwl.model;

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;
import javax.persistence.Table;
/**
 * 订单项实体
 * @author Jerval
 *
 */
@Entity
@Table(name="t_orderItem")
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 = 40, 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;
	}

	@ManyToOne(cascade = { CascadeType.REFRESH, CascadeType.MERGE }, optional = false)
	@JoinColumn(name = "order_id")
	public Order getOrder() {
		return order;
	}

	public void setOrder(Order order) {
		this.order = order;
	}

}

 最后来个测试:

package junit.test;

import java.util.List;
import java.util.UUID;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.junit.Test;
import com.jvwl.model.Order;
import com.jvwl.model.OrderItem;

public class JPATest {
	@Test
	public void test() {
		EntityManagerFactory factory = Persistence
				.createEntityManagerFactory("jvwl");
		EntityManager em = factory.createEntityManager();
		em.getTransaction().begin();

		Order order = new Order();
		order.setAmount(108f);
		order.setOrderId(UUID.randomUUID().toString());

		OrderItem orderItem1 = new OrderItem();
		orderItem1.setOrder(order);
		orderItem1.setProductName("kk");
		orderItem1.setSellPrice(24f);
		order.addOrderItem(orderItem1);

		OrderItem orderItem2 = new OrderItem();
		orderItem2.setOrder(order);
		orderItem2.setProductName("ooo");
		orderItem2.setSellPrice(222f);
		order.addOrderItem(orderItem2);

		em.persist(order);
		em.getTransaction().commit();
		em.close();
		factory.close();
	}

	@SuppressWarnings("unchecked")
	@Test
	public void testQuery() {
		EntityManagerFactory factory = Persistence
				.createEntityManagerFactory("jvwl");
		EntityManager em = factory.createEntityManager();
		em.getTransaction().begin();

		List<Order> list = em.createQuery("select o from Order o")
				.getResultList();
		for (Order o : list) {
			System.out.println(o.getOrderId());
			System.out.println(o.getItems().size());
		}
		em.getTransaction().commit();
		em.close();
		factory.close();
	}
}

 

你可能感兴趣的:(jpa,JUnit)