Hibernate双向多对多映射关系配置代码实例

1、实体类

package com.yl.bean;

import java.io.Serializable;
import java.util.Set;

/**
 * 商品实体类
 */
public class Goods implements Serializable {
  private Integer id;//商品id
  private String goodsName;//商品名
  private Double price;//商品价格
  private String remark;//备注
  private Set orderSet;//商品所属订单

  public Goods() {
  }

  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public String getGoodsName() {
    return goodsName;
  }

  public void setGoodsName(String goodsName) {
    this.goodsName = goodsName;
  }

  public Double getPrice() {
    return price;
  }

  public void setPrice(Double price) {
    this.price = price;
  }

  public String getRemark() {
    return remark;
  }

  public void setRemark(String remark) {
    this.remark = remark;
  }

  public Set getOrderSet() {
    return orderSet;
  }

  public void setOrderSet(Set orderSet) {
    this.orderSet = orderSet;
  }

  @Override
  public String toString() {
    return "Goods{" +
        "goods_id=" + id +
        ", goodsName='" + goodsName + '\'' +
        ", price=" + price +
        ", remark='" + remark + '\'' +
        ", orderSet=" + orderSet +
        '}';
  }
}
package com.yl.bean;

import java.io.Serializable;
import java.util.Set;

/**
 * 商品订单实体类
 */
public class GoodsOrder implements Serializable {
  private Integer id;//订单id
  private String orderNo;//订单编号
  private Double price;//订单价格
  private Set goodsSet;//订单包含的商品

  public GoodsOrder() {
  }

  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public String getOrderNo() {
    return orderNo;
  }

  public void setOrderNo(String orderNo) {
    this.orderNo = orderNo;
  }

  public Double getPrice() {
    return price;
  }

  public void setPrice(Double price) {
    this.price = price;
  }

  public Set getGoodsSet() {
    return goodsSet;
  }

  public void setGoodsSet(Set goodsSet) {
    this.goodsSet = goodsSet;
  }

  @Override
  public String toString() {
    return "GoodsOrder{" +
        "id=" + id +
        ", orderNo='" + orderNo + '\'' +
        ", price=" + price +
        ", goodsSet=" + goodsSet +
        '}';
  }
}

2、全局配置文件(hibernate.cfg.xml)



  
    
    com.mysql.cj.jdbc.Driver
    jdbc:mysql://localhost:3306/hibernate?characterEncoding=utf8&serverTimezone=GMT%2B8
    root
    123456
    
    true
    
    update
    
    
    

3、商品类映射配置文件(Goods.hbm.xml)



  
  
    
    
      
    

    
    
    

    
    
      
      
    
  

4、订单类映射配置文件(GoodsOrder.hbm.xml)



  
  
    
    
      
    

    
    

    
    
      
      
    
  

5、测试

 @Test
  public void addTest(){
    Session session= HibernateUtils.getSession();

    Goods goods=new Goods();
    goods.setGoodsName("小米");
    goods.setPrice(3999.0);
    goods.setRemark("为发烧而生");

    Goods goods1=new Goods();
    goods1.setRemark("中华有为");
    goods1.setPrice(3999.0);
    goods1.setGoodsName("华为");

    GoodsOrder order=new GoodsOrder();
    order.setOrderNo("001");
    order.setPrice(7998.0);

    GoodsOrder order1=new GoodsOrder();
    order1.setOrderNo("002");
    order1.setPrice(7998.0);

    Set orderSet=new HashSet<>();
    orderSet.add(order);
    orderSet.add(order1);
    goods.setOrderSet(orderSet);
    goods1.setOrderSet(orderSet);

   /* Set orderSet1=new HashSet<>();
    orderSet1.add(order1);
    goods1.setOrderSet(orderSet1);*/

    Set goodsSet=new HashSet<>();
    goodsSet.add(goods);
    goodsSet.add(goods1);
//注意这里,只需要一方关联即可(上面已经关联了,所以不需要再次添加商品集合),两方关联会造成主键重复,报错
//    order.setGoodsSet(goodsSet);
//    order1.setGoodsSet(goodsSet);

    Transaction transaction=session.beginTransaction();

    session.save(goods);
    session.save(goods1);
    session.save(order);
    session.save(order1);

    transaction.commit();
    session.close();

  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(Hibernate双向多对多映射关系配置代码实例)