欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Hibernate雙向多對多映射關(guān)系配置代碼實(shí)例

 更新時(shí)間:2020年10月28日 09:38:38   作者:Y_wee  
這篇文章主要介紹了Hibernate雙向多對多映射關(guān)系配置代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

1、實(shí)體類

package com.yl.bean;

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

/**
 * 商品實(shí)體類
 */
public class Goods implements Serializable {
  private Integer id;//商品id
  private String goodsName;//商品名
  private Double price;//商品價(jià)格
  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;

/**
 * 商品訂單實(shí)體類
 */
public class GoodsOrder implements Serializable {
  private Integer id;//訂單id
  private String orderNo;//訂單編號(hào)
  private Double price;//訂單價(jià)格
  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>
    <!--數(shù)據(jù)源配置-->
    <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate?characterEncoding=utf8&amp;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>
    <!--自動(dòng)創(chuàng)建表-->
    <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>

    <!--多對多關(guān)系映射-->
    <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>

    <!--多對多關(guān)系映射-->
    <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("為發(fā)燒而生");

    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);
//注意這里,只需要一方關(guān)聯(lián)即可(上面已經(jīng)關(guān)聯(lián)了,所以不需要再次添加商品集合),兩方關(guān)聯(lián)會(huì)造成主鍵重復(fù),報(bào)錯(cuò)
//    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();

  }

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論