hibernate09--连接查询
2021-06-20 00:06
阅读:425
标签:nbsp 返回 XML cad 姓名 apach 测试数据 key 数组
创建实体类
package cn.bdqn.bean; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * @author 小豆腐 * *员工的实体类 */ public class Emp { private Integer empNo; private String empName; private String job; private Double salary; private Date hireDate; //多个员工属于一个部门 private Dept dept; public Integer getEmpNo() { return empNo; } public void setEmpNo(Integer empNo) { this.empNo = empNo; } public String getEmpName() { return empName; } public void setEmpName(String empName) { this.empName = empName; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public Double getSalary() { return salary; } public void setSalary(Double salary) { this.salary = salary; } public Date getHireDate() { return hireDate; } public void setHireDate(Date hireDate) { this.hireDate = hireDate; } public Dept getDept() { return dept; } public void setDept(Dept dept) { this.dept = dept; } public Emp(Integer empNo, String empName, String job, Double salary, Date hireDate, Dept dept) { super(); this.empNo = empNo; this.empName = empName; this.job = job; this.salary = salary; this.hireDate = hireDate; this.dept = dept; } public Emp() { super(); } @Override public String toString() { return "Emp [empNo=" + empNo + ", empName=" + empName + ", job=" + job + ", salary=" + salary + ", hireDate=" + hireDate + ", dept=" + dept + "]"; } }
package cn.bdqn.bean; import java.util.HashSet; import java.util.Set; import com.sun.org.apache.bcel.internal.generic.NEW; /** * * 部门的实体类 */ public class Dept { private Integer deptNo; private String deptName; private String location; //一个部门对应多个员工 private Setemps=new HashSet(); public Integer getDeptNo() { return deptNo; } public void setDeptNo(Integer deptNo) { this.deptNo = deptNo; } public String getDeptName() { return deptName; } public void setDeptName(String deptName) { this.deptName = deptName; } public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } public Set getEmps() { return emps; } public void setEmps(Set emps) { this.emps = emps; } public Dept(Integer deptNo, String deptName, String location, Set emps) { super(); this.deptNo = deptNo; this.deptName = deptName; this.location = location; this.emps = emps; } public Dept() { super(); } @Override public String toString() { return "Dept [deptNo=" + deptNo + ", deptName=" + deptName + ", location=" + location + ", emps=" + emps.size() + "]"; } }
创建对应的数据库表
创建对应的映射文件
在hibernate.cfg.xml文件中 管理两个映射文件之后,创建测试类代码
public class EmpTest { Session session =null; Transaction transaction=null; @Before public void before(){ session = HibernateSessionUtil.getCurrentSession(); transaction= session.beginTransaction(); } /** * 先给两个表中 增加测试数据 */ @Test public void testAdd(){ /** * 因为我们设置了级联操作 cascade=all * 那么我们就可以在保存部门信息的时候保存员工 */ Dept dept=new Dept(); dept.setDeptNo(2); dept.setDeptName("市场部"); dept.setLocation("2楼"); //创建员工 Emp emp1=new Emp(5, "员工5", "程序猿5", 5000.0,dept); Emp emp2=new Emp(6, "员工6", "程序猿6", 545000.0,dept); Emp emp3=new Emp(7, "员工7", "程序猿7", 2000.0,dept); Emp emp4=new Emp(8, "员工8", "程序猿8", 98000.0,dept); Setemps=new HashSet(); emps.add(emp1); emps.add(emp2); emps.add(emp3); emps.add(emp4); dept.setEmps(emps); //把所有的员工放入了集合中 session.save(dept); transaction.commit(); } /** * 所有的迫切连接返回的都是一个对象! 有fetch===》迫切连接 * 所有的非迫切连接返回的都是一个数组! * * * 01.使用内连接查询 两个表的所有数据 * sql语句 * select * from emp inner join dept on dept.deptno=emp.deptno hql语句 from Emp e inner join e.dept(Emp类中的关联属性名) */ @Test public void test01(){ String hql="from Emp e inner join e.dept"; List
hibernate09--连接查询
标签:nbsp 返回 XML cad 姓名 apach 测试数据 key 数组
原文地址:http://www.cnblogs.com/HHR-SUN/p/7190961.html
下一篇:Flask入门-上传的问题
评论
亲,登录后才可以留言!