1.创建如下javaweb项目结构
2.在项目的src下创建hibernate.cfg.xml主配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" > 3 <hibernate-configuration> 4 <session-factory> 5 <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> 6 <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property> 7 <property name="connection.username">scott</property> 8 <property name="connection.password">tiger</property> 9 <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> 10 <property name="show_sql">true</property> 11 <property name="format_sql">true</property> 12 <mapping resource="com/entity/Dept.hbm.xml"/> 13 <mapping resource="com/entity/Emp.hbm.xml"/> 14 </session-factory> 15 </hibernate-configuration>
hibernate.cfg.xml
3.在项目的src下的com.util包下创建HibernateUtil.java
1 package com.util; 2 3 import org.hibernate.HibernateException; 4 import org.hibernate.Session; 5 import org.hibernate.SessionFactory; 6 import org.hibernate.cfg.Configuration; 7 8 public class HibernateUtil { 9 private static ThreadLocal<Session> thread=new ThreadLocal<Session>(); 10 private static Configuration config=null; 11 private static SessionFactory factory=null; 12 /** 13 * 读取配置文件 14 */ 15 static{ 16 try { 17 config=new Configuration().configure("/hibernate.cfg.xml"); 18 factory=config.buildSessionFactory(); 19 } catch (HibernateException e) { 20 System.out.println("读取配置文件失败或创建factory失败"); 21 e.printStackTrace(); 22 } 23 } 24 /** 25 * 打开session 26 * @return 27 */ 28 public static Session getSession(){ 29 Session session =thread.get(); 30 if(session==null){ 31 session=factory.openSession(); 32 thread.set(session); 33 } 34 return session; 35 } 36 /** 37 * 关闭session 38 */ 39 public static void closeSession(){ 40 Session session =thread.get(); 41 thread.set(null); 42 session.close(); 43 44 } 45 46 }
HibernateUtil.java
4.在项目的src下的com.entity包下创建Dept.java
1 package com.entity; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 public class Dept { 7 private Integer deptno; 8 private String dname; 9 private String loc; 10 private Set<Emp> emps =new HashSet<Emp>(); 11 12 public Dept() { 13 } 14 15 public Dept(Integer deptno, String dname, String loc) { 16 this.deptno = deptno; 17 this.dname = dname; 18 this.loc = loc; 19 } 20 21 public Dept(Integer deptno, String dname, String loc, Set<Emp> emps) { 22 this.deptno = deptno; 23 this.dname = dname; 24 this.loc = loc; 25 this.emps = emps; 26 } 27 public Integer getDeptno() { 28 return deptno; 29 } 30 public void setDeptno(Integer deptno) { 31 this.deptno = deptno; 32 } 33 public String getDname() { 34 return dname; 35 } 36 public void setDname(String dname) { 37 this.dname = dname; 38 } 39 public String getLoc() { 40 return loc; 41 } 42 public void setLoc(String loc) { 43 this.loc = loc; 44 } 45 public Set<Emp> getEmps() { 46 return emps; 47 } 48 public void setEmps(Set<Emp> emps) { 49 this.emps = emps; 50 } 51 52 @Override 53 public String toString() { 54 return "Dept [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + "]"; 55 } 56 57 58 59 }
Dept.java
5.在项目的src下的com.entity包下创建Emp.java
1 package com.entity; 2 3 4 5 public class Emp { 6 private Integer empno; 7 private String ename; 8 private String job; 9 private Integer sal; 10 private Dept dept; 11 12 public Emp() { 13 } 14 15 public Emp(Integer empno, String ename, String job, Integer sal) { 16 super(); 17 this.empno = empno; 18 this.ename = ename; 19 this.job = job; 20 this.sal = sal; 21 } 22 23 public Emp(Integer empno, String ename, String job, Integer sal, Dept dept) { 24 super(); 25 this.empno = empno; 26 this.ename = ename; 27 this.job = job; 28 this.sal = sal; 29 this.dept = dept; 30 } 31 32 public Integer getEmpno() { 33 return empno; 34 } 35 36 public void setEmpno(Integer empno) { 37 this.empno = empno; 38 } 39 40 public String getEname() { 41 return ename; 42 } 43 44 public void setEname(String ename) { 45 this.ename = ename; 46 } 47 48 public String getJob() { 49 return job; 50 } 51 52 public void setJob(String job) { 53 this.job = job; 54 } 55 56 public Integer getSal() { 57 return sal; 58 } 59 60 public void setSal(Integer sal) { 61 this.sal = sal; 62 } 63 64 public Dept getDept() { 65 return dept; 66 } 67 68 public void setDept(Dept dept) { 69 this.dept = dept; 70 } 71 72 @Override 73 public String toString() { 74 return "Emp [ empno=" + empno + ", ename=" + ename 75 + ", job=" + job + ", sal=" + sal + "]"; 76 } 77 78 79 80 }
Emp.java
6.在项目的src下的com.entity包下创建Dept.hbm.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > 3 <hibernate-mapping> 4 <class name="com.entity.Dept" table="DEPT"> 5 <id name="deptno" type="java.lang.Integer" column="DEPTNO"> 6 <generator class="assigned"/> 7 </id> 8 <property name="dname" type="java.lang.String" column="DNAME"/> 9 <property name="loc" type="java.lang.String" column="LOC"/> 10 <!-- 一对多 --> 11 <set name="emps" inverse="true" cascade="save-update"> 12 <key column="DEPTNO"></key> 13 <one-to-many class="com.entity.Emp"/> 14 </set> 15 </class> 16 </hibernate-mapping>
Dept.hbm.xml
7.在项目的src下的com.entity包下创建Emp.hbm.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > 3 <hibernate-mapping> 4 <class name="com.entity.Emp" table="EMP"> 5 <id name="empno" type="java.lang.Integer" column="EMPNO"> 6 <generator class="assigned"/> 7 </id> 8 <property name="ename" type="java.lang.String" column="ENAME"/> 9 <property name="job" type="java.lang.String" column="JOB"/> 10 <property name="sal" type="java.lang.Integer" column="SAL"/> 11 <!-- 多对一 --> 12 <many-to-one name="dept" class="com.entity.Dept" column="DEPTNO"/> 13 </class> 14 </hibernate-mapping>
Emp.hbm.xml
8.在项目的src下的com.dao包下创建DpetDao.java
1 package com.dao; 2 3 import java.util.List; 4 5 import org.hibernate.Query; 6 import org.hibernate.Session; 7 8 import com.entity.Dept; 9 import com.util.HibernateUtil; 10 11 public class DpetDao { 12 13 14 public static void main(String[] args) { 15 System.out.println("-----------1.关联查询-----------"); 16 //getDept(); 17 System.out.println("-------2.内连接-------"); 18 innerJoin(); 19 System.out.println("-------3.迫切内连接-------"); 20 innerJoinFetch(); 21 System.out.println("-------4.左连接-------"); 22 leftJoin(); 23 System.out.println("-------5.迫切左连接-------"); 24 leftJoinFetch(); 25 System.out.println("-------6.右连接-------"); 26 rightJoin(); 27 28 } 29 /** 30 * 6.右连接 31 */ 32 private static void rightJoin() { 33 Session session=HibernateUtil.getSession(); 34 String hql="from Dept d right join d.emps "; 35 Query query=session.createQuery(hql); 36 /*list集合中的每个元素都是一个Object数组,数组的第一个元素时Dept对象, 37 * 第二个元素时Emp对象,Dept对象的emps集合元素没有被初始化, 38 * 即emps集合没有存放关联的Emp对象*/ 39 List<Object[]> list=query.list(); 40 System.out.println("dept对象"+"~~"+"emp对象"); 41 for (Object[] obj : list) { 42 System.out.println(obj[0]); 43 System.out.println(obj[1]); 44 } 45 HibernateUtil.closeSession(); 46 47 } 48 /** 49 * 5.迫切左连接 50 */ 51 private static void leftJoinFetch() { 52 Session session=HibernateUtil.getSession(); 53 /*Hibernate使用fetch关键字实现了将Emp对象读取出来后立即填充到对应的Dept对象 54 * 的集合属性中,*/ 55 String hql="select distinct d from Dept d left join fetch d.emps "; 56 Query query=session.createQuery(hql); 57 List<Dept> list=query.list(); 58 for (Dept dept : list) { 59 System.out.println(dept); 60 } 61 HibernateUtil.closeSession(); 62 } 63 /** 64 * 4.左连接 65 */ 66 private static void leftJoin() { 67 Session session=HibernateUtil.getSession(); 68 String hql="from Dept d left join d.emps "; 69 Query query=session.createQuery(hql); 70 /*list集合中的每个元素都是一个Object数组,数组的第一个元素时Dept对象, 71 * 第二个元素时Emp对象,Dept对象的emps集合元素没有被初始化, 72 * 即emps集合没有存放关联的Emp对象*/ 73 List<Object[]> list=query.list(); 74 System.out.println("dept对象"+"~~"+"emp对象"); 75 for (Object[] obj : list) { 76 System.out.println(obj[0]); 77 System.out.println(obj[1]); 78 } 79 HibernateUtil.closeSession(); 80 } 81 /** 82 * 3.迫切内连接 83 */ 84 private static void innerJoinFetch() { 85 Session session=HibernateUtil.getSession(); 86 /*Hibernate使用fetch关键字实现了将Emp对象读取出来后立即填充到对应的Dept对象 87 * 的集合属性中,*/ 88 String hql="select distinct d from Dept d inner join fetch d.emps "; 89 Query query=session.createQuery(hql); 90 List<Dept> list=query.list(); 91 for (Dept dept : list) { 92 System.out.println(dept); 93 } 94 HibernateUtil.closeSession(); 95 } 96 /** 97 * 2.内连接 98 */ 99 private static void innerJoin() { 100 Session session=HibernateUtil.getSession(); 101 String hql="from Dept d inner join d.emps "; 102 Query query=session.createQuery(hql); 103 /*list集合中的每个元素都是一个Object数组,数组的第一个元素时Dept对象, 104 * 第二个元素时Emp对象,Dept对象的emps集合元素没有被初始化, 105 * 即emps集合没有存放关联的Emp对象*/ 106 List<Object[]> list=query.list(); 107 System.out.println("dept对象"+"~~"+"emp对象"); 108 for (Object[] obj : list) { 109 System.out.println(obj[0]); 110 System.out.println(obj[1]); 111 } 112 HibernateUtil.closeSession(); 113 } 114 /** 115 * Exception in thread "main" java.lang.StackOverflowError 116 * 内存溢出:死循环 117 * hiberante双向关联时:打印内容时不能你中有我,我中有你,查询默认是懒加载 118 * 119 */ 120 private static void getDept() { 121 Session session=HibernateUtil.getSession(); 122 String hql="from Dept"; 123 Query query=session.createQuery(hql); 124 List<Dept> list=query.list(); 125 for (Dept dept : list) { 126 System.out.println(dept); 127 System.out.println(dept.getEmps()); 128 } 129 HibernateUtil.closeSession(); 130 131 } 132 133 }
DpetDao.java
9.运行结果如下:remdeme.txt
1 1.内连接 2 Hibernate: 3 select 4 dept0_.DEPTNO as DEPTNO0_0_, 5 emps1_.EMPNO as EMPNO1_1_, 6 dept0_.DNAME as DNAME0_0_, 7 dept0_.LOC as LOC0_0_, 8 emps1_.ENAME as ENAME1_1_, 9 emps1_.JOB as JOB1_1_, 10 emps1_.SAL as SAL1_1_, 11 emps1_.DEPTNO as DEPTNO1_1_ 12 from 13 DEPT dept0_ 14 inner join 15 EMP emps1_ 16 on dept0_.DEPTNO=emps1_.DEPTNO 17 2.迫切内连接 18 select 19 dept0_.DEPTNO as DEPTNO0_0_, 20 emps1_.EMPNO as EMPNO1_1_, 21 dept0_.DNAME as DNAME0_0_, 22 dept0_.LOC as LOC0_0_, 23 emps1_.ENAME as ENAME1_1_, 24 emps1_.JOB as JOB1_1_, 25 emps1_.SAL as SAL1_1_, 26 emps1_.DEPTNO as DEPTNO1_1_, 27 emps1_.DEPTNO as DEPTNO0__, 28 emps1_.EMPNO as EMPNO0__ 29 from 30 DEPT dept0_ 31 inner join 32 EMP emps1_ 33 on dept0_.DEPTNO=emps1_.DEPTNO 34 -------4.左连接------- 35 Hibernate: 36 select 37 dept0_.DEPTNO as DEPTNO0_0_, 38 emps1_.EMPNO as EMPNO1_1_, 39 dept0_.DNAME as DNAME0_0_, 40 dept0_.LOC as LOC0_0_, 41 emps1_.ENAME as ENAME1_1_, 42 emps1_.JOB as JOB1_1_, 43 emps1_.SAL as SAL1_1_, 44 emps1_.DEPTNO as DEPTNO1_1_ 45 from 46 DEPT dept0_ 47 left outer join 48 EMP emps1_ 49 on dept0_.DEPTNO=emps1_.DEPTNO 50 51 -------5.迫切左连接------- 52 Hibernate: 53 select 54 distinct dept0_.DEPTNO as DEPTNO0_0_, 55 emps1_.EMPNO as EMPNO1_1_, 56 dept0_.DNAME as DNAME0_0_, 57 dept0_.LOC as LOC0_0_, 58 emps1_.ENAME as ENAME1_1_, 59 emps1_.JOB as JOB1_1_, 60 emps1_.SAL as SAL1_1_, 61 emps1_.DEPTNO as DEPTNO1_1_, 62 emps1_.DEPTNO as DEPTNO0__, 63 emps1_.EMPNO as EMPNO0__ 64 from 65 DEPT dept0_ 66 left outer join 67 EMP emps1_ 68 on dept0_.DEPTNO=emps1_.DEPTNO 69 -------6.右连接------- 70 Hibernate: 71 select 72 dept0_.DEPTNO as DEPTNO0_0_, 73 emps1_.EMPNO as EMPNO1_1_, 74 dept0_.DNAME as DNAME0_0_, 75 dept0_.LOC as LOC0_0_, 76 emps1_.ENAME as ENAME1_1_, 77 emps1_.JOB as JOB1_1_, 78 emps1_.SAL as SAL1_1_, 79 emps1_.DEPTNO as DEPTNO1_1_ 80 from 81 DEPT dept0_ 82 right outer join 83 EMP emps1_ 84 on dept0_.DEPTNO=emps1_.DEPTNO
时间: 2024-12-29 23:23:30