双向一对多要注意一下几个知识点:
1-m 多的一方为关系维护端,关系维护端负责外键记录的更新,关系被维护端是没有权利更新外键记录
onetomany or manytomany 后面的many 默认都是延迟加载
manytoone or onetoone 立即加载
JPA,在@OneToMany里加入mappedBy属性避免生成中间表 (设置mappedBy的那一方是被维护端)
使用JPA的时候,如果A B两个实体间是一对多,多对一的关系,如果不在@OneToMany里加入mappedBy属性(相当于inverse=”true”)会导致自动生成一个多余的中间表。
通过下面的例子来学习JPA-的双向一对多
一的一方
Order.java
package com.olay.entity;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="MyOrder")
public class Orders implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String orderid;
private float account=0f;
private Set<OrderItem> items = new HashSet<OrderItem>();
@Id @Column(length=50)
public String getOrderid() {
return orderid;
}
public void setOrderid(String orderid) {
this.orderid = orderid;
}
@Column(nullable=false)
public float getAccount() {
return account;
}
public void setAccount(float account) {
this.account = account;
}
@OneToMany(cascade={CascadeType.ALL},mappedBy="orders")
public Set<OrderItem> getItems() {
return items;
}
public void setItems(Set<OrderItem> items) {
this.items = items;
}
//添加OrderItem对象
public void addOrderItem(OrderItem orderItem){
orderItem.setOrders(this);
this.items.add(orderItem);
}
}
在一的一方设置
@OneToMany(cascade={CascadeType.ALL},mappedBy="orders")
maooedBy=“orders” 表示被维护,通过多的那方的属性orders来维护
cascade={CascadeType.ALL} 表示级联关系
多的一方
OrderItem.java
package com.olay.entity;
import java.io.Serializable;
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 implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private int id;
private String produceName;
private float sellPrice=0f;
private Orders orders;
@Id @GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(length=50,nullable=false)
public String getProduceName() {
return produceName;
}
public void setProduceName(String produceName) {
this.produceName = produceName;
}
@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="orders_id")
public Orders getOrders() {
return orders;
}
public void setOrders(Orders orders) {
this.orders = orders;
}
}
设置:
@ManyToOne(cascade={CascadeType.REFRESH,CascadeType.MERGE},optional=false)
@JoinColumn(name="orders_id")
cascade={CascadeType.REFRESH,CascadeType.MERGE} 设置级联关系一定要更加需求设定有关的级联类型
optional=false 这里表示是否可选,false表示不可选(即不能为空,这里必须为false,因为我们要求外键不能为空)
@JoinColumn(name="orders_id") 这是关联一的一方,这会在数据库表中添加orders_id字段(外键),这字段与一的一方的id相对应
测试类
package com.olay.junit;
import java.util.UUID;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import com.olay.entity.OrderItem;
import com.olay.entity.Orders;
import junit.framework.TestCase;
public class OneToMany extends TestCase {
public void test(){
EntityManagerFactory emf = Persistence.createEntityManagerFactory("olayjpa");
EntityManager em=emf.createEntityManager();
em.getTransaction().begin();
OrderItem oi1 = new OrderItem();
oi1.setProduceName("足球1");
oi1.setSellPrice(90f);
OrderItem oi2 = new OrderItem();
oi2.setProduceName("篮球1");
oi2.setSellPrice(80f);
Orders orders = new Orders();
orders.setOrderid(UUID.randomUUID().toString());
orders.setAccount(345);
orders.addOrderItem(oi1);
orders.addOrderItem(oi2);
em.persist(orders);
em.getTransaction().commit();
emf.close();
}
}
结果: