亲宝软件园·资讯

展开

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

Y_wee 人气:0
想了解Hibernate双向多对多映射关系配置代码实例的相关内容吗,Y_wee在本文为您仔细讲解Hibernate双向多对多的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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<GoodsOrder> 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<GoodsOrder> getOrderSet() {
    return orderSet;
  }

  public void setOrderSet(Set<GoodsOrder> 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<Goods> 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<Goods> getGoodsSet() {
    return goodsSet;
  }

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

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

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

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <!--数据源配置-->
    <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate?characterEncoding=utf8&serverTimezone=GMT%2B8</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">123456</property>
    <!--显示sql-->
    <property name="hibernate.show_sql">true</property>
    <!--自动创建表-->
    <property name="hibernate.hbm2ddl.auto">update</property>
    <!--指定映射配置文件的位置-->
    <mapping resource="com/yl/bean/Goods.hbm.xml"></mapping>
    <mapping resource="com/yl/bean/GoodsOrder.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>

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

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <!--配置Goods类-->
  <class name="com.yl.bean.Goods" table="t_goods">
    <!--主键-->
    <id name="id" column="id" type="java.lang.Integer">
      <generator class="identity"></generator>
    </id>

    <property name="goodsName" column="goodsName" type="java.lang.String"></property>
    <property name="price" column="price" type="java.lang.Double"></property>
    <property name="remark" column="remark"></property>

    <!--多对多关系映射-->
    <set name="orderSet" table="t_goods_order">
      <key column="goods_id"></key>
      <many-to-many class="com.yl.bean.GoodsOrder" column="order_id"></many-to-many>
    </set>
  </class>
</hibernate-mapping>

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

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <!--配置GoodsOrder类-->
  <class name="com.yl.bean.GoodsOrder" table="t_order">
    <!--主键-->
    <id name="id" column="id" type="java.lang.Integer">
      <generator class="identity"></generator>
    </id>

    <property name="orderNo" column="orderNo" type="java.lang.String"></property>
    <property name="price" column="price" type="java.lang.Double"></property>

    <!--多对多关系映射-->
    <set name="goodsSet" table="t_goods_order">
      <key column="order_id"></key>
      <many-to-many class="com.yl.bean.Goods" column="goods_id"></many-to-many>
    </set>
  </class>
</hibernate-mapping>

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<GoodsOrder> orderSet=new HashSet<>();
    orderSet.add(order);
    orderSet.add(order1);
    goods.setOrderSet(orderSet);
    goods1.setOrderSet(orderSet);

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

    Set<Goods> 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();

  }

加载全部内容

相关教程
猜你喜欢
用户评论