– 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