?
双向一对多关系中,必须存在一个关系维护端,在 JPA 规范中,要求 many 的一方作为关系的维护端(owner side), one 的一方作为被维护端(inverse side)。
?可以在 one 方指定 @OneToMany 注释并设置 mappedBy 属性,以指定它是这一关联中的被维护端,many 为维护端。
?在 many 方指定 @ManyToOne 注释,并使用 @JoinColumn 指定外键名称
- package com.morris.entity;
- 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.JoinColumn;
- import javax.persistence.ManyToOne;
- import javax.persistence.Table;
- @Entity
- @Table(name="order_item")
- public class OrderItem {
- @Id
- @GeneratedValue
- @Column(name="order_item_id")
- private Integer orderItemId;
- @Column(name="product_name",length=50,nullable=false)
- private String productName;
- @Column(name="product_price",nullable=false)
- private Double productPrice;
- /**
- * ManyToOne:多对一的配置
- * cascade(级联):all(所有),merge(更新),refresh(查询),persistence(保存),remove(删除)
- * fetch: eager:立即加载 one的一方默认是立即加载
- * lazy:懒加载 many的一方默认是懒加载
- * optional:是否可选,外键是否允许为空
- *
- * JoinColumn:指定外键名
- *
- */
- @ManyToOne(cascade={CascadeType.MERGE,CascadeType.REFRESH},fetch=FetchType.EAGER,optional=false)
- @JoinColumn(name="order_id")
- private Order order;
- public OrderItem() {
- super();
- // TODO Auto-generated constructor stub
- }
- @Override
- public String toString() {
- return "OrderItem [orderItemId=" + orderItemId + ", productName="
- + productName + ", productPrice=" + productPrice + ", order="
- + order + "]";
- }
- public Integer getOrderItemId() {
- return orderItemId;
- }
- public void setOrderItemId(Integer orderItemId) {
- this.orderItemId = orderItemId;
- }
- public String getProductName() {
- return productName;
- }
- public void setProductName(String productName) {
- this.productName = productName;
- }
- public Double getProductPrice() {
- return productPrice;
- }
- public void setProductPrice(Double productPrice) {
- this.productPrice = productPrice;
- }
- public Order getOrder() {
- return order;
- }
- public void setOrder(Order order) {
- this.order = order;
- }
- public OrderItem(String productName,
- Double productPrice) {
- super();
- this.productName = productName;
- this.productPrice = productPrice;
- }
- }
- ---------------------------------------------------------------------------------
- 双向一对多
- package com.morris.entity;
- import java.util.Set;
- import javax.persistence.CascadeType;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.FetchType;
- import javax.persistence.Id;
- import javax.persistence.OneToMany;
- import javax.persistence.Table;
- @Entity
- @Table(name="orders")
- public class Order {
- @Id
- @Column(name="order_id",length=32)
- private String orderId;
- @Column(name="order_name",length=50)
- private String orderName;
- /**
- * OneToMany:一对多的配置
- * mappedBy="order":指定由多的一方的order属性维护关联关系
- *
- */
- @OneToMany(cascade={CascadeType.REFRESH,CascadeType.MERGE,CascadeType.REMOVE,CascadeType.PERSIST},fetch=FetchType.LAZY,mappedBy="order")
- private Set<OrderItem> orderItems;
- public Order(String orderId, String orderName, Set<OrderItem> orderItems) {
- super();
- this.orderId = orderId;
- this.orderName = orderName;
- this.orderItems = orderItems;
- }
- public Set<OrderItem> getOrderItems() {
- return orderItems;
- }
- public void setOrderItems(Set<OrderItem> orderItems) {
- this.orderItems = orderItems;
- }
- public Order() {
- super();
- // TODO Auto-generated constructor stub
- }
- public Order(String orderId, String orderName) {
- super();
- this.orderId = orderId;
- this.orderName = orderName;
- }
- @Override
- public String toString() {
- return "Order [orderId=" + orderId + ", orderName=" + orderName + "]";
- }
- public String getOrderId() {
- return orderId;
- }
- public void setOrderId(String orderId) {
- this.orderId = orderId;
- }
- public String getOrderName() {
- return orderName;
- }
- public void setOrderName(String orderName) {
- this.orderName = orderName;
- }
- }
原文地址:https://www.cnblogs.com/MAPO/p/8507406.html
时间: 2024-10-10 06:50:20