1-m:多的一方为关系维护端,关系维护端负责外键纪录的更新,关系被维护端没有权力更新外键纪录.
维护端注解
- @OneToMany(cascade = { CascadeType.PERSIST, CascadeType.REFRESH,
- CascadeType.MERGE, CascadeType.REMOVE },
- fetch = FetchType.EAGER,
- mappedBy = "order")
被维护端注解
- @ManyToOne(cascade = { CascadeType.MERGE, CascadeType.REFRESH }, optional = false)
- @JoinColumn(name = "order_id")
对于一对多和多对一关系的现实中的例子是,网上购物时的订单和订单项。一个订单有多个订单项。多个订单项属于一个订单。
订单实体类:
- package com.taoistwar.jpa.entity.onetomany;
-
- import java.util.HashSet;
- import java.util.Set;
-
- import javax.persistence.CascadeType;
- 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;
-
- @Entity
- @Table(name = "order_info")
- public class OrderInfo {
- private Integer id;
- private String name;
-
- private Set<OrderItem> items = new HashSet<OrderItem>();
-
- @Id
- @GeneratedValue(strategy = GenerationType.AUTO)
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- @OneToMany(cascade = { CascadeType.PERSIST, CascadeType.REFRESH,
- CascadeType.MERGE, CascadeType.REMOVE }, fetch = FetchType.EAGER,
- 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);
- }
-
- }
重点在于:
- @OneToMany(cascade = { CascadeType.PERSIST, CascadeType.REFRESH,
- CascadeType.MERGE, CascadeType.REMOVE }, fetch = FetchType.EAGER,
- mappedBy = "order")
- ublic Set<OrderItem> getItems() {
- return items;
订单项实体类:
- package com.taoistwar.jpa.entity.onetomany;
-
- import javax.persistence.CascadeType;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.JoinColumn;
- import javax.persistence.ManyToOne;
- import javax.persistence.Table;
-
- @Entity
- @Table(name = "order_item")
- public class OrderItem {
- private Integer Id;
- private String name;
-
- private OrderInfo order;
-
- @Id
- @GeneratedValue(strategy = GenerationType.AUTO)
- public Integer getId() {
- return Id;
- }
-
- public void setId(Integer id) {
- Id = id;
- }
-
- @Column(length = 20, nullable = true)
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- @ManyToOne(cascade = { CascadeType.MERGE, CascadeType.REFRESH }, optional = false)
- @JoinColumn(name = "order_id")
- public OrderInfo getOrder() {
- return order;
- }
-
- public void setOrder(OrderInfo order) {
- this.order = order;
- }
-
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((Id == null) ? 0 : Id.hashCode());
- return result;
- }
-
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- OrderItem other = (OrderItem) obj;
- if (Id == null) {
- if (other.Id != null)
- return false;
- } else if (!Id.equals(other.Id))
- return false;
- return true;
- }
-
- }
重点在于:
- @ManyToOne(cascade = { CascadeType.MERGE, CascadeType.REFRESH }, optional = false)
- @JoinColumn(name = "order_id")
- public OrderInfo getOrder() {
- return order;
- }
测试类:
- package com.taoistwar.jpa.entity.onetomany;
-
- import javax.persistence.EntityManager;
- import javax.persistence.EntityManagerFactory;
- import javax.persistence.Persistence;
-
- import org.junit.Test;
-
-
- public class OneToMany {
- @Test
- public void save() {
- EntityManagerFactory emf = Persistence
- .createEntityManagerFactory("JPAPU");
- EntityManager em = emf.createEntityManager();
- em.getTransaction().begin();
- OrderInfo o = new OrderInfo();
- o.setName("订单一");
- OrderItem oi1 = new OrderItem();
- oi1.setName("产品一");
- o.addOrderItem(oi1);
- OrderItem oi2 = new OrderItem();
- oi2.setName("产品一");
- o.addOrderItem(oi2);
- OrderItem oi3 = new OrderItem();
- oi3.setName("产品一");
- o.addOrderItem(oi3);
- OrderItem oi4 = new OrderItem();
- oi4.setName("产品一");
- o.addOrderItem(oi4);
- em.persist(o);
-
- em.getTransaction().commit();
- emf.close();
- }
- }