首页 > 代码库 > JPA实体属性one-to-many关系映射
JPA实体属性one-to-many关系映射
(1)下边有两个实体类Order.java和OrderItem.java 前者是作为one 后者是many的一方(也就是说一个订单可以包含多个订单项)
下边是他们的映射关系
(2)Order.java代码如下
package com.lc.bean; import java.util.HashSet; 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.Id; import javax.persistence.OneToMany; import javax.persistence.Table; @Entity @Table(name="orders") public class Order { private String orderid; private Float amount = 0f; private Set<OrderItem> items = new HashSet<OrderItem>(); @Id @Column(length = 12) 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; } // 级联操作 若使用全部的方法可以使用all // @OneToMany(cascade = { CascadeType.REFRESH, CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REMOVE}) //fetch的默认值是:如果是得到many的一方 默认的是延迟加载 //mappedBy表示关系被维护端,是在OrderItem类总的哪一个属性维护的 @OneToMany(cascade = CascadeType.ALL,fetch=FetchType.LAZY,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); } }
(3)OrderItem.java 代码
package com.lc.bean; 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; @Entity public class OrderItem { private Integer id; private String produceName; private Float sellPrice; 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 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; } // fetch 在one的一方默认为立即加载 // optional表示是否可以为空 true表示可以为空 // JoinColumn表示外键的名称,通过name指定 @ManyToOne(cascade = { CascadeType.MERGE, CascadeType.REFRESH }, optional = false) @JoinColumn(name = "order_id") public Order getOrder() { return order; } public void setOrder(Order order) { this.order = order; } }
(4)测试程序
package com.lc.junitTest; import java.util.UUID; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import org.junit.Test; import com.lc.bean.Order; import com.lc.bean.OrderItem; public class OneToManyTest { @Test public void save(){ EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpatest"); EntityManager em = factory.createEntityManager(); em.getTransaction().begin(); //创建一个订单 Order order = new Order(); order.setAmount(34f); order.setOrderid("999"); //创建一个订单项 OrderItem orderItem1 = new OrderItem(); orderItem1.setProduceName("足球"); orderItem1.setSellPrice(90f); order.addOrderItem(orderItem1); //创建第二个订单项 OrderItem orderItem2 = new OrderItem(); orderItem2.setProduceName("羽毛球"); orderItem2.setSellPrice(56f); order.addOrderItem(orderItem2); //保存订单 em.persist(order); em.getTransaction().commit(); factory.close(); } }
(5)检验结果
JPA实体属性one-to-many关系映射
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。