小程序三方平台开发: 解析小程序开发的未来趋势和机遇
523
2023-03-19
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
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
return orderSet;
}
public void setOrderSet(Set
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
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
return goodsSet;
}
public void setGoodsSet(Set
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-/hibernate-configuration-3.0.dtd">
3、商品类映射配置文件(Goods.hbm.xml)
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge-/hibernate-mapping-3.0.dtd">
4、订单类映射配置文件(GoodsOrder.hbm.xml)
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge-/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.add(order);
orderSet.add(order1);
goods.setOrderSet(orderSet);
goods1.setOrderSet(orderSet);
/* Set
orderSet1.add(order1);
goods1.setOrderSet(orderSet1);*/
OSLUjx
Set
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小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~