Hibernate 表关联 -- 双边多对多2

– Start
还有一种方式可以配置双边多对多,可以把它们配置成两个多对一。

package shangbo.hibernate.demo026;

import java.io.Serializable;
import java.util.Objects;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

@Entity
public class OrdersGoods implements Serializable{
	private static final long serialVersionUID = 3633684631928432573L;

	@Id
	@ManyToOne
	private Orders orders;

	@Id
	@ManyToOne
	private Goods goods;

	public OrdersGoods() {

	}

	public OrdersGoods(Orders orders, Goods goods) {
		this.orders = orders;
		this.goods = goods;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}

		if (o == null || getClass() != o.getClass()) {
			return false;
		}

		OrdersGoods that = (OrdersGoods) o;
		return Objects.equals(goods, that.goods) && Objects.equals(orders, that.orders);
	}

	@Override
	public int hashCode() {
		return Objects.hash(orders, goods);
	}

	public Orders getOrders() {
		return orders;
	}

	public void setOrders(Orders orders) {
		this.orders = orders;
	}

	public Goods getGoods() {
		return goods;
	}

	public void setGoods(Goods goods) {
		this.goods = goods;
	}
}

package shangbo.hibernate.demo026;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;

@Entity
public class Orders {
	@Id
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "orderId-generator")
	@SequenceGenerator(name = "orderId-generator", sequenceName = "ORDER_ID_SEQ")
	private Integer orderId;
	private String customerName;

	@OneToMany(mappedBy = "orders")
	private List<OrdersGoods> ordersGoods;

	public Orders() {
	}

	public Orders(String customerName) {
		this.customerName = customerName;
	}

	public void addGoods(Goods goods) {
		if (ordersGoods == null) {
			ordersGoods = new ArrayList<>();
		}
		OrdersGoods ogs = new OrdersGoods(this, goods);
		ordersGoods.add(ogs);

		if (goods.getOrdersGoods() == null) {
			goods.setOrdersGoods(new ArrayList<>());
		}
		goods.getOrdersGoods().add(ogs);
	}

	public void removeGoods(Goods goods) {
		OrdersGoods ogs = new OrdersGoods(this, goods);
		ordersGoods.remove(ogs);
		goods.getOrdersGoods().remove(ogs);

		ogs.setGoods(null);
		ogs.setOrders(null);
	}

	public Integer getOrderId() {
		return orderId;
	}

	public String getCustomerName() {
		return customerName;
	}

	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}

}

package shangbo.hibernate.demo026;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;

@Entity
public class Goods {
	@Id
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "goodId-generator")
	@SequenceGenerator(name = "goodId-generator", sequenceName = "GOOD_ID_SEQ")
	private Integer goodId;
	private String name;

	@OneToMany(mappedBy = "goods")
	private List<OrdersGoods> ordersGoods;

	public Goods() {
	}

	public Goods(String name) {
		this.name = name;
	}

	public Integer getGoodId() {
		return goodId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<OrdersGoods> getOrdersGoods() {
		return ordersGoods;
	}

	public void setOrdersGoods(List<OrdersGoods> ordersGoods) {
		this.ordersGoods = ordersGoods;
	}

}

– 更多参见:Hibernate 精萃
– 声 明:转载请注明出处
– Last Updated on 2019-07-02
– Written by ShangBo on 2019-07-02
– End

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值