Hibernate双向多对多映射关系配置代码实例
1、实体类
packagecom.yl.bean;
importjava.io.Serializable;
importjava.util.Set;
/**
*商品实体类
*/
publicclassGoodsimplementsSerializable{
privateIntegerid;//商品id
privateStringgoodsName;//商品名
privateDoubleprice;//商品价格
privateStringremark;//备注
privateSetorderSet;//商品所属订单
publicGoods(){
}
publicIntegergetId(){
returnid;
}
publicvoidsetId(Integerid){
this.id=id;
}
publicStringgetGoodsName(){
returngoodsName;
}
publicvoidsetGoodsName(StringgoodsName){
this.goodsName=goodsName;
}
publicDoublegetPrice(){
returnprice;
}
publicvoidsetPrice(Doubleprice){
this.price=price;
}
publicStringgetRemark(){
returnremark;
}
publicvoidsetRemark(Stringremark){
this.remark=remark;
}
publicSetgetOrderSet(){
returnorderSet;
}
publicvoidsetOrderSet(SetorderSet){
this.orderSet=orderSet;
}
@Override
publicStringtoString(){
return"Goods{"+
"goods_id="+id+
",goodsName='"+goodsName+'\''+
",price="+price+
",remark='"+remark+'\''+
",orderSet="+orderSet+
'}';
}
}
packagecom.yl.bean;
importjava.io.Serializable;
importjava.util.Set;
/**
*商品订单实体类
*/
publicclassGoodsOrderimplementsSerializable{
privateIntegerid;//订单id
privateStringorderNo;//订单编号
privateDoubleprice;//订单价格
privateSetgoodsSet;//订单包含的商品
publicGoodsOrder(){
}
publicIntegergetId(){
returnid;
}
publicvoidsetId(Integerid){
this.id=id;
}
publicStringgetOrderNo(){
returnorderNo;
}
publicvoidsetOrderNo(StringorderNo){
this.orderNo=orderNo;
}
publicDoublegetPrice(){
returnprice;
}
publicvoidsetPrice(Doubleprice){
this.price=price;
}
publicSetgetGoodsSet(){
returngoodsSet;
}
publicvoidsetGoodsSet(SetgoodsSet){
this.goodsSet=goodsSet;
}
@Override
publicStringtoString(){
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
publicvoidaddTest(){
Sessionsession=HibernateUtils.getSession();
Goodsgoods=newGoods();
goods.setGoodsName("小米");
goods.setPrice(3999.0);
goods.setRemark("为发烧而生");
Goodsgoods1=newGoods();
goods1.setRemark("中华有为");
goods1.setPrice(3999.0);
goods1.setGoodsName("华为");
GoodsOrderorder=newGoodsOrder();
order.setOrderNo("001");
order.setPrice(7998.0);
GoodsOrderorder1=newGoodsOrder();
order1.setOrderNo("002");
order1.setPrice(7998.0);
SetorderSet=newHashSet<>();
orderSet.add(order);
orderSet.add(order1);
goods.setOrderSet(orderSet);
goods1.setOrderSet(orderSet);
/*SetorderSet1=newHashSet<>();
orderSet1.add(order1);
goods1.setOrderSet(orderSet1);*/
SetgoodsSet=newHashSet<>();
goodsSet.add(goods);
goodsSet.add(goods1);
//注意这里,只需要一方关联即可(上面已经关联了,所以不需要再次添加商品集合),两方关联会造成主键重复,报错
//order.setGoodsSet(goodsSet);
//order1.setGoodsSet(goodsSet);
Transactiontransaction=session.beginTransaction();
session.save(goods);
session.save(goods1);
session.save(order);
session.save(order1);
transaction.commit();
session.close();
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。