11.hibernate的连接查询

时间:2022-01-22 17:58:38

1.创建如下javaweb项目结构

11.hibernate的连接查询

11.hibernate的连接查询

2.在项目的src下创建hibernate.cfg.xml主配置文件

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property>
<property name="connection.username">scott</property>
<property name="connection.password">tiger</property>
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping resource="com/entity/Dept.hbm.xml"/>
<mapping resource="com/entity/Emp.hbm.xml"/>
</session-factory>
</hibernate-configuration>

hibernate.cfg.xml

3.在项目的src下的com.util包下创建HibernateUtil.java

 package com.util;

 import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; public class HibernateUtil {
private static ThreadLocal<Session> thread=new ThreadLocal<Session>();
private static Configuration config=null;
private static SessionFactory factory=null;
/**
* 读取配置文件
*/
static{
try {
config=new Configuration().configure("/hibernate.cfg.xml");
factory=config.buildSessionFactory();
} catch (HibernateException e) {
System.out.println("读取配置文件失败或创建factory失败");
e.printStackTrace();
}
}
/**
* 打开session
* @return
*/
public static Session getSession(){
Session session =thread.get();
if(session==null){
session=factory.openSession();
thread.set(session);
}
return session;
}
/**
* 关闭session
*/
public static void closeSession(){
Session session =thread.get();
thread.set(null);
session.close(); } }

HibernateUtil.java

4.在项目的src下的com.entity包下创建Dept.java

 package com.entity;

 import java.util.HashSet;
import java.util.Set; public class Dept {
private Integer deptno;
private String dname;
private String loc;
private Set<Emp> emps =new HashSet<Emp>(); public Dept() {
} public Dept(Integer deptno, String dname, String loc) {
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
} public Dept(Integer deptno, String dname, String loc, Set<Emp> emps) {
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
this.emps = emps;
}
public Integer getDeptno() {
return deptno;
}
public void setDeptno(Integer deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
public Set<Emp> getEmps() {
return emps;
}
public void setEmps(Set<Emp> emps) {
this.emps = emps;
} @Override
public String toString() {
return "Dept [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + "]";
} }

Dept.java

5.在项目的src下的com.entity包下创建Emp.java

 package com.entity;

 public class Emp {
private Integer empno;
private String ename;
private String job;
private Integer sal;
private Dept dept; public Emp() {
} public Emp(Integer empno, String ename, String job, Integer sal) {
super();
this.empno = empno;
this.ename = ename;
this.job = job;
this.sal = sal;
} public Emp(Integer empno, String ename, String job, Integer sal, Dept dept) {
super();
this.empno = empno;
this.ename = ename;
this.job = job;
this.sal = sal;
this.dept = dept;
} public Integer getEmpno() {
return empno;
} public void setEmpno(Integer empno) {
this.empno = empno;
} public String getEname() {
return ename;
} public void setEname(String ename) {
this.ename = ename;
} public String getJob() {
return job;
} public void setJob(String job) {
this.job = job;
} public Integer getSal() {
return sal;
} public void setSal(Integer sal) {
this.sal = sal;
} public Dept getDept() {
return dept;
} public void setDept(Dept dept) {
this.dept = dept;
} @Override
public String toString() {
return "Emp [ empno=" + empno + ", ename=" + ename
+ ", job=" + job + ", sal=" + sal + "]";
} }

Emp.java

6.在项目的src下的com.entity包下创建Dept.hbm.xml

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="com.entity.Dept" table="DEPT">
<id name="deptno" type="java.lang.Integer" column="DEPTNO">
<generator class="assigned"/>
</id>
<property name="dname" type="java.lang.String" column="DNAME"/>
<property name="loc" type="java.lang.String" column="LOC"/>
<!-- 一对多 -->
<set name="emps" inverse="true" cascade="save-update">
<key column="DEPTNO"></key>
<one-to-many class="com.entity.Emp"/>
</set>
</class>
</hibernate-mapping>

Dept.hbm.xml

7.在项目的src下的com.entity包下创建Emp.hbm.xml

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="com.entity.Emp" table="EMP">
<id name="empno" type="java.lang.Integer" column="EMPNO">
<generator class="assigned"/>
</id>
<property name="ename" type="java.lang.String" column="ENAME"/>
<property name="job" type="java.lang.String" column="JOB"/>
<property name="sal" type="java.lang.Integer" column="SAL"/>
<!-- 多对一 -->
<many-to-one name="dept" class="com.entity.Dept" column="DEPTNO"/>
</class>
</hibernate-mapping>

Emp.hbm.xml

8.在项目的src下的com.dao包下创建DpetDao.java

 package com.dao;

 import java.util.List;

 import org.hibernate.Query;
import org.hibernate.Session; import com.entity.Dept;
import com.util.HibernateUtil; public class DpetDao { public static void main(String[] args) {
System.out.println("-----------1.关联查询-----------");
//getDept();
System.out.println("-------2.内连接-------");
innerJoin();
System.out.println("-------3.迫切内连接-------");
innerJoinFetch();
System.out.println("-------4.左连接-------");
leftJoin();
System.out.println("-------5.迫切左连接-------");
leftJoinFetch();
System.out.println("-------6.右连接-------");
rightJoin(); }
/**
* 6.右连接
*/
private static void rightJoin() {
Session session=HibernateUtil.getSession();
String hql="from Dept d right join d.emps ";
Query query=session.createQuery(hql);
/*list集合中的每个元素都是一个Object数组,数组的第一个元素时Dept对象,
* 第二个元素时Emp对象,Dept对象的emps集合元素没有被初始化,
* 即emps集合没有存放关联的Emp对象*/
List<Object[]> list=query.list();
System.out.println("dept对象"+"~~"+"emp对象");
for (Object[] obj : list) {
System.out.println(obj[0]);
System.out.println(obj[1]);
}
HibernateUtil.closeSession(); }
/**
* 5.迫切左连接
*/
private static void leftJoinFetch() {
Session session=HibernateUtil.getSession();
/*Hibernate使用fetch关键字实现了将Emp对象读取出来后立即填充到对应的Dept对象
* 的集合属性中,*/
String hql="select distinct d from Dept d left join fetch d.emps ";
Query query=session.createQuery(hql);
List<Dept> list=query.list();
for (Dept dept : list) {
System.out.println(dept);
}
HibernateUtil.closeSession();
}
/**
* 4.左连接
*/
private static void leftJoin() {
Session session=HibernateUtil.getSession();
String hql="from Dept d left join d.emps ";
Query query=session.createQuery(hql);
/*list集合中的每个元素都是一个Object数组,数组的第一个元素时Dept对象,
* 第二个元素时Emp对象,Dept对象的emps集合元素没有被初始化,
* 即emps集合没有存放关联的Emp对象*/
List<Object[]> list=query.list();
System.out.println("dept对象"+"~~"+"emp对象");
for (Object[] obj : list) {
System.out.println(obj[0]);
System.out.println(obj[1]);
}
HibernateUtil.closeSession();
}
/**
* 3.迫切内连接
*/
private static void innerJoinFetch() {
Session session=HibernateUtil.getSession();
/*Hibernate使用fetch关键字实现了将Emp对象读取出来后立即填充到对应的Dept对象
* 的集合属性中,*/
String hql="select distinct d from Dept d inner join fetch d.emps ";
Query query=session.createQuery(hql);
List<Dept> list=query.list();
for (Dept dept : list) {
System.out.println(dept);
}
HibernateUtil.closeSession();
}
/**
* 2.内连接
*/
private static void innerJoin() {
Session session=HibernateUtil.getSession();
String hql="from Dept d inner join d.emps ";
Query query=session.createQuery(hql);
/*list集合中的每个元素都是一个Object数组,数组的第一个元素时Dept对象,
* 第二个元素时Emp对象,Dept对象的emps集合元素没有被初始化,
* 即emps集合没有存放关联的Emp对象*/
List<Object[]> list=query.list();
System.out.println("dept对象"+"~~"+"emp对象");
for (Object[] obj : list) {
System.out.println(obj[0]);
System.out.println(obj[1]);
}
HibernateUtil.closeSession();
}
/**
* Exception in thread "main" java.lang.*Error
* 内存溢出:死循环
* hiberante双向关联时:打印内容时不能你中有我,我中有你,查询默认是懒加载
*
*/
private static void getDept() {
Session session=HibernateUtil.getSession();
String hql="from Dept";
Query query=session.createQuery(hql);
List<Dept> list=query.list();
for (Dept dept : list) {
System.out.println(dept);
System.out.println(dept.getEmps());
}
HibernateUtil.closeSession(); } }

DpetDao.java

9.运行结果如下:remdeme.txt

 1.内连接
Hibernate:
select
dept0_.DEPTNO as DEPTNO0_0_,
emps1_.EMPNO as EMPNO1_1_,
dept0_.DNAME as DNAME0_0_,
dept0_.LOC as LOC0_0_,
emps1_.ENAME as ENAME1_1_,
emps1_.JOB as JOB1_1_,
emps1_.SAL as SAL1_1_,
emps1_.DEPTNO as DEPTNO1_1_
from
DEPT dept0_
inner join
EMP emps1_
on dept0_.DEPTNO=emps1_.DEPTNO
2.迫切内连接
select
dept0_.DEPTNO as DEPTNO0_0_,
emps1_.EMPNO as EMPNO1_1_,
dept0_.DNAME as DNAME0_0_,
dept0_.LOC as LOC0_0_,
emps1_.ENAME as ENAME1_1_,
emps1_.JOB as JOB1_1_,
emps1_.SAL as SAL1_1_,
emps1_.DEPTNO as DEPTNO1_1_,
emps1_.DEPTNO as DEPTNO0__,
emps1_.EMPNO as EMPNO0__
from
DEPT dept0_
inner join
EMP emps1_
on dept0_.DEPTNO=emps1_.DEPTNO
-------4.左连接-------
Hibernate:
select
dept0_.DEPTNO as DEPTNO0_0_,
emps1_.EMPNO as EMPNO1_1_,
dept0_.DNAME as DNAME0_0_,
dept0_.LOC as LOC0_0_,
emps1_.ENAME as ENAME1_1_,
emps1_.JOB as JOB1_1_,
emps1_.SAL as SAL1_1_,
emps1_.DEPTNO as DEPTNO1_1_
from
DEPT dept0_
left outer join
EMP emps1_
on dept0_.DEPTNO=emps1_.DEPTNO -------5.迫切左连接-------
Hibernate:
select
distinct dept0_.DEPTNO as DEPTNO0_0_,
emps1_.EMPNO as EMPNO1_1_,
dept0_.DNAME as DNAME0_0_,
dept0_.LOC as LOC0_0_,
emps1_.ENAME as ENAME1_1_,
emps1_.JOB as JOB1_1_,
emps1_.SAL as SAL1_1_,
emps1_.DEPTNO as DEPTNO1_1_,
emps1_.DEPTNO as DEPTNO0__,
emps1_.EMPNO as EMPNO0__
from
DEPT dept0_
left outer join
EMP emps1_
on dept0_.DEPTNO=emps1_.DEPTNO
-------6.右连接-------
Hibernate:
select
dept0_.DEPTNO as DEPTNO0_0_,
emps1_.EMPNO as EMPNO1_1_,
dept0_.DNAME as DNAME0_0_,
dept0_.LOC as LOC0_0_,
emps1_.ENAME as ENAME1_1_,
emps1_.JOB as JOB1_1_,
emps1_.SAL as SAL1_1_,
emps1_.DEPTNO as DEPTNO1_1_
from
DEPT dept0_
right outer join
EMP emps1_
on dept0_.DEPTNO=emps1_.DEPTNO