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

网友投稿 298 2022-11-15


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)

"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

com.mysql.cj.jdbc.Driver

jdbc:mysql://localhost:3306/hibernate?characterEncoding=utf8&serverTimezone=GMT%2B8

root

123456

true

update

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

"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

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

"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

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);*/

OSLUjx

Set goodsSet=new HashSet<>();

goodsSet.add(goods);

goodsSet.add(goods1);

//注意这里,只需要一方关联即可(上面已经关联了,所以不需要再次添加商品集合),两方关联会造成主键重复,报错

// order.setGoodsSet(goodsSet);

// order1.setGoodsSet(goodsSet);

Transaction transactOSLUjxion=session.beginTransaction();

session.save(goods);

session.save(goods1);

session.save(order);

session.save(order1);

transaction.commit();

session.close();

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SpringBoot使用阿里OSS实现文件云存储的方法
下一篇:Spring内部bean和级联属性用法详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~