Hibernate多表的关联插入
创始人
2024-02-06 06:35:59

一、一对多关系:

“一”的一方为主表,“多”的一方为副表,主表关联副表,应该在主表中加入副表对象作为属性。
根据顾客ID查询顾客信息 (一) ,同时将顾客名下所有订单查出 (多)

实现思路:
在这里插入图片描述

  • 数据库关系:
    在这里插入图片描述
  • 创建顾客实体类,存储顾客信息:
    注意我们在顾客类中用List集合封装了顾客名下的订单信息。
package org.example.Entity;import java.util.List;
public class CustomerEntity {private int id;private String name;private Integer age;//在顾客表中加入订单集合private List orders;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public List getOrders() {return orders;}public void setOrders(List orders) {this.orders = orders;}
}
  • 创建订单类,存储订单信息:
    订单类中应该有顾客类,用来指明该订单属于哪个顾客。
package org.example.Entity;public class OrdersEntity {private int id;private String orderNumber;private Double orderPrice;//订单中有外键customer_id,所以需要添加顾客类private CustomerEntity customer;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getOrderNumber() {return orderNumber;}public void setOrderNumber(String orderNumber) {this.orderNumber = orderNumber;}public Double getOrderPrice() {return orderPrice;}public void setOrderPrice(Double orderPrice) {this.orderPrice = orderPrice;}public CustomerEntity getCustomer() {return customer;}public void setCustomer(CustomerEntity customer) {this.customer = customer;}
}
  • 创建顾客关系类的映射文件:




  • 创建订单实体类的映射文件:




  • 测试类:
    注意要建立关联关系。
package org.example;import org.example.Entity.CustomerEntity;
import org.example.Entity.OrdersEntity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;public class AppTest{@Testpublic void test(){//创建SessionFactory,从根路径下获取核心配置文件SessionFactory factory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();//创建sessionSession session = factory.openSession();//创建事务Transaction transaction = session.beginTransaction();//创建顾客类对象并给相应属性赋值CustomerEntity customer = new CustomerEntity();customer.setId(4);customer.setName("44");customer.setAge(18);//创建订单类对象并给相应属性赋值OrdersEntity order = new OrdersEntity();order.setId(4);order.setOrderNumber("4");order.setOrderPrice(4.0);//建立关联关系,若不建立关联关系order表的数据为空order.setCustomer(customer);//保存,所有对象都要保存session.save(customer);session.save(order);//提交事务transaction.commit();//关闭SessionFactoryfactory.close();}
}
  • 运行结果:
    在这里插入图片描述

二、多对多关系:

多对多关系通过一个中间表来维护。

实现思路:
在这里插入图片描述

  • 数据库关系:
    在这里插入图片描述
  • 创建Accounts实体类:
package org.example.Entity;import java.util.Set;public class AccountsEntity {private int aid;private String aname;private Set courses;public int getAid() {return aid;}public void setAid(int aid) {this.aid = aid;}public String getAname() {return aname;}public void setAname(String aname) {this.aname = aname;}public Set getCourses() {return courses;}public void setCourses(Set courses) {this.courses = courses;}
}
  • 创建Courses实体类:
package org.example.Entity;import java.util.Set;public class CoursesEntity {private int cid;private String cname;private Set accounts;public int getCid() {return cid;}public void setCid(int cid) {this.cid = cid;}public String getCname() {return cname;}public void setCname(String cname) {this.cname = cname;}public Set getAccounts() {return accounts;}public void setAccounts(Set accounts) {this.accounts = accounts;}
}
  • 创建中间表实体类:
package org.example.Entity;public class AccountCourseEntity {private int id;private Integer aid;private Integer cid;public int getId() {return id;}public void setId(int id) {this.id = id;}public Integer getAid() {return aid;}public void setAid(Integer aid) {this.aid = aid;}public Integer getCid() {return cid;}public void setCid(Integer cid) {this.cid = cid;}
}
  • 创建Accounts实体类映射文件:




  • 创建Courses实体类映射文件:




  • 创建中间表实体类映射文件:




  • 测试类:
package org.example;import org.example.Entity.AccountsEntity;
import org.example.Entity.CoursesEntity;
import org.example.Entity.CustomerEntity;
import org.example.Entity.OrdersEntity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;import java.util.HashSet;
import java.util.Set;public class AppTest{@Testpublic void test2(){//创建SessionFactory,从根路径下获取核心配置文件SessionFactory factory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();//创建sessionSession session = factory.openSession();//创建事务Transaction transaction = session.beginTransaction();//创建Courses类对象并给相应属性赋值CoursesEntity course = new CoursesEntity();course.setCname("JAVA");//创建Accounts类对象并给相应属性赋值AccountsEntity account = new AccountsEntity();account.setAname("张三");//建立关联关系Set courses = new HashSet<>();courses.add(course);account.setCourses(courses);//保存,所有对象都要保存session.save(course);session.save(account);//提交事务transaction.commit();//关闭SessionFactoryfactory.close();}
}
  • 运行结果:
    在这里插入图片描述

相关内容

热门资讯

埃菲尔铁塔在哪 中国仿建埃菲尔... 2019年4月26日,广西南宁市,街头惊现一座巨型山寨版埃菲尔铁塔,高约20米,白色塔身,造型逼真,...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...
脚上的穴位图 脚面经络图对应的... 人体穴位作用图解大全更清晰直观的标注了各个人体穴位的作用,包括头部穴位图、胸部穴位图、背部穴位图、胳...
demo什么意思 demo版本... 618快到了,各位的小金库大概也在准备开闸放水了吧。没有小金库的,也该向老婆撒娇卖萌服个软了,一切只...
埃菲尔铁塔在哪 中国仿建埃菲尔... 2019年4月26日,广西南宁市,街头惊现一座巨型山寨版埃菲尔铁塔,高约20米,白色塔身,造型逼真,...
苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
世界上最漂亮的人 世界上最漂亮... 此前在某网上,选出了全球265万颜值姣好的女性。从这些数量庞大的女性群体中,人们投票选出了心目中最美...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...
应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...
脚上的穴位图 脚面经络图对应的... 人体穴位作用图解大全更清晰直观的标注了各个人体穴位的作用,包括头部穴位图、胸部穴位图、背部穴位图、胳...
猫咪吃了塑料袋怎么办 猫咪误食... 你知道吗?塑料袋放久了会长猫哦!要说猫咪对塑料袋的喜爱程度完完全全可以媲美纸箱家里只要一有塑料袋的响...