首页 > 代码库 > JPA一对多和多对一关系
JPA一对多和多对一关系
1-m:多的一方为关系维护端,关系维护端负责外键纪录的更新,关系被维护端没有权力更新外键纪录.
维护端注解
Java代码
- @OneToMany(cascade = { CascadeType.PERSIST, CascadeType.REFRESH,
- CascadeType.MERGE, CascadeType.REMOVE },
- fetch = FetchType.EAGER,
- mappedBy = "order")
被维护端注解
Java代码
- @ManyToOne(cascade = { CascadeType.MERGE, CascadeType.REFRESH }, optional = false)
- @JoinColumn(name = "order_id")
对于一对多和多对一关系的现实中的例子是,网上购物时的订单和订单项。一个订单有多个订单项。多个订单项属于一个订单。
订单实体类:
Java代码
- 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);
- }
- }
重点在于:
Java代码
- @OneToMany(cascade = { CascadeType.PERSIST, CascadeType.REFRESH,
- CascadeType.MERGE, CascadeType.REMOVE }, fetch = FetchType.EAGER,
- mappedBy = "order")
- ublic Set<OrderItem> getItems() {
- return items;
订单项实体类:
Java代码
- 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;
- }
- }
重点在于:
Java代码
- @ManyToOne(cascade = { CascadeType.MERGE, CascadeType.REFRESH }, optional = false)
- @JoinColumn(name = "order_id")
- public OrderInfo getOrder() {
- return order;
- }
测试类:
Java代码
- 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);
- // UUID.randomUUID().toString();
- em.getTransaction().commit();
- emf.close();
- }
- }
- JPA.zip (5.7 MB)
- 下载次数: 537
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。