hql查询语句 内存中的情况,fetch迫切查询关键字
Classes.java
package cn.itcast.hiberate.sh.domain; import java.io.Serializable;
import java.util.Set; public class Classes implements Serializable{
private Long cid;
private String cname;
private String description; public Classes(){} public Classes(String cname,String description){
this.cname = cname;
this.description = description;
} public Long getCid() {
return cid;
} public void setCid(Long cid) {
this.cid = cid;
} public String getCname() {
return cname;
} public void setCname(String cname) {
this.cname = cname;
} public String getDescription() {
return description;
} public void setDescription(String description) {
this.description = description;
} public Set<Student> getStudents() {
return students;
} public void setStudents(Set<Student> students) {
this.students = students;
} private Set students;
}
<?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="cn.itcast.hiberate.sh.domain.Classes">
<id name="cid" length="5" type="java.lang.Long">
<generator class="increment"></generator>
</id>
<property name="cname" length="20" type="java.lang.String"></property> <property name="description" length="100" type="java.lang.String"></property>
<!--
set元素对应类中的set集合
通过set元素使classes表与student表建立关联
key是通过外键的形式让两张表建立关联
one-to-many是通过类的形式让两个类建立关联 cascade 级联
save-update
1、当 保存班级的时候,对学生进行怎么样的操作
如果学生对象在数据库中没有对应的值,这个时候会执行save操作
如果学生对象在数据库中有对应的值,这个时候会执行update操作
delete
all
inverse 维护关系
true 不维护关系
false 维护关系
default false
-->
<set name="students" cascade="save-update" inverse="true" lazy="false" fetch="join">
<!--
key是用来描述外键
-->
<key>
<column name="cid"></column>
</key>
<one-to-many class="cn.itcast.hiberate.sh.domain.Student"/>
</set>
</class>
</hibernate-mapping>
Course.java
package cn.itcast.hiberate.sh.domain; import java.io.Serializable;
import java.util.Set; public class Course implements Serializable{
private Long cid;
private String cname;
private String description; public Long getCid() {
return cid;
} public void setCid(Long cid) {
this.cid = cid;
} public String getCname() {
return cname;
} public void setCname(String cname) {
this.cname = cname;
} public String getDescription() {
return description;
} public void setDescription(String description) {
this.description = description;
} public Set<Student> getStudents() {
return students;
} public void setStudents(Set<Student> students) {
this.students = students;
} private Set<Student> students;
}
<?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="cn.itcast.hiberate.sh.domain.Course">
<id name="cid" type="java.lang.Long" length="5">
<generator class="increment"></generator>
</id>
<property name="cname" type="java.lang.String" length="20"></property>
<property name="description" type="java.lang.String" length="50"></property>
<!--
table
student与course在数据库的层次上通过该表实现关联
-->
<set name="students" table="student_course">
<key>
<!--
在哪个映射文件中,就添加哪个
-->
<column name="cid"></column>
</key>
<!--
column表示外键
-->
<many-to-many class="cn.itcast.hiberate.sh.domain.Student" column="sid"></many-to-many>
</set>
</class>
</hibernate-mapping>
Student.java
package cn.itcast.hiberate.sh.domain; import java.io.Serializable;
import java.util.HashSet;
import java.util.Set; public class Student implements Serializable{
private Long sid;
private String sname; private Classes classes; private Set<Course> courses = new HashSet<Course>(0); public Set<Course> getCourses() {
return courses;
}
public void setCourses(Set<Course> courses) {
this.courses = courses;
}
public Classes getClasses() {
return classes;
}
public void setClasses(Classes classes) {
this.classes = classes;
}
public Long getSid() {
return sid;
}
public void setSid(Long sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
private String description;
}
<?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="cn.itcast.hiberate.sh.domain.Classes">
<id name="cid" length="5" type="java.lang.Long">
<generator class="increment"></generator>
</id>
<property name="cname" length="20" type="java.lang.String"></property> <property name="description" length="100" type="java.lang.String"></property>
<!--
set元素对应类中的set集合
通过set元素使classes表与student表建立关联
key是通过外键的形式让两张表建立关联
one-to-many是通过类的形式让两个类建立关联 cascade 级联
save-update
1、当 保存班级的时候,对学生进行怎么样的操作
如果学生对象在数据库中没有对应的值,这个时候会执行save操作
如果学生对象在数据库中有对应的值,这个时候会执行update操作
delete
all
inverse 维护关系
true 不维护关系
false 维护关系
default false
-->
<set name="students" cascade="save-update" inverse="true" lazy="false" fetch="join">
<!--
key是用来描述外键
-->
<key>
<column name="cid"></column>
</key>
<one-to-many class="cn.itcast.hiberate.sh.domain.Student"/>
</set>
</class>
</hibernate-mapping>
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只能连接一个数据库
-->
<session-factory>
<!--
数据库的用户名
-->
<property name="connection.username">root</property> <property name="connection.driver_class">
com.mysql.jdbc.Driver
</property> <!--
密码
-->
<property name="connection.password">friends</property>
<!--
url
-->
<property name="connection.url">
jdbc:mysql://localhost:3306/mydatabase
</property>
<!--
作用:根据持久化类和映射文件生成表
validate
create-drop
create
update
-->
<property name="hbm2ddl.auto">update</property> <property name="hibernate.dialect">
org.hibernate.dialect.MySQLInnoDBDialect
</property>
<!--
显示hibernate内部生成的sql语句
-->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping resource="cn/itcast/hiberate/sh/domain/Classes.hbm.xml" />
<mapping resource="cn/itcast/hiberate/sh/domain/Student.hbm.xml" />
<mapping resource="cn/itcast/hiberate/sh/domain/Course.hbm.xml" /> </session-factory>
</hibernate-configuration>
package cn.itcast.hibernate.sh.dao; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set; import org.hibernate.Query;
import org.hibernate.Session;
import org.junit.Test; import cn.itcast.hiberate.sh.domain.Classes;
import cn.itcast.hiberate.sh.domain.Course;
import cn.itcast.hiberate.sh.domain.Student;
import cn.itcast.hibernate.sh.utils.HiberanteUtils; /**
* 单表
*
* 一对多和多对多
*
* 多表的结合
* @author Think
*
*/
public class HQLDao extends HiberanteUtils{
public List<Classes> queryAllClasses(){
Session session = sessionFactory.openSession();
List<Classes> cList = session.createQuery("from Classes").list();
session.close();
return cList;
} public List queryClasses_Properties(){
Session session = sessionFactory.openSession();
List cList = session.createQuery("select cid,cname from Classes").list();
session.close();
return cList;
} public List<Classes> queryClasses_Constructor(){
Session session = sessionFactory.openSession();
List<Classes> cList = session.createQuery("select new cn.itcast.hiberate.sh.domain.Classes(cname,description) from Classes").list();
session.close();
return cList;
} public Classes queryClasses_Condition(){
Session session = sessionFactory.openSession();
Query query = session.createQuery("select new cn.itcast.hiberate.sh.domain.Classes(cname,description) from Classes where cid=:cid");
query.setLong("cid", 1L);
Classes classes = (Classes)query.uniqueResult();
System.out.println(classes.getCname());
session.close();
return classes;
} public Classes queryClasses_Condition_2(){
Session session = sessionFactory.openSession();
Query query = session.createQuery("select new cn.itcast.hiberate.sh.domain.Classes(cname,description) from Classes where cid=?");
query.setLong(0, 1L);
Classes classes = (Classes)query.uniqueResult();
System.out.println(classes.getCname());
session.close();
return classes;
} /**
* order by,group by,sun,min,max,avg,having等都适用
* @return
*/ /**
* 子查询
*/
public void queryClasses_SubSelect(){
Session session = sessionFactory.openSession();
List<Classes> cList = session.createQuery("from Classes where cid in(select cid from Classes where cid in(1,2,3))").list();
session.close();
} public static HQLDao getInstance(){
return new HQLDao();
} /*********************************************************************************************/
/**
* 一对多
* 等值连接 查询出来的机构很差
* 内连接
* 左外连接
* 迫切左外连接
*/
public List<Classes> queryClasses_Student_EQ(){
Session session = sessionFactory.openSession();
List<Classes> cList = session.createQuery("from Classes c,Student s where c.cid=s.classes.cid").list();
session.close();
return cList;
} /**
* 内连接
* @return
*/
public List<Classes> queryClasses_Student_INNER(){
Session session = sessionFactory.openSession();
List<Classes> cList = session.createQuery("from Classes c inner join c.students").list();
session.close();
return cList;
} /**
* 迫切内连接
* @return
*/
public List<Classes> queryClasses_Student_INNER_FETCH(){
Session session = sessionFactory.openSession();
List<Classes> cList = session.createQuery("from Classes c inner join fetch c.students").list();
session.close();
return cList;
} /**
* 左外连接
*/
public List<Classes> queryClasses_Student_LeftJoin(){
Session session = sessionFactory.openSession();
List<Classes> cList = session.createQuery("from Classes c left outer join c.students").list();
session.close();
return cList;
} /**
* 迫切左外连接
*/
public List<Classes> queryClasses_Student_LeftJoin_fetch(){
Session session = sessionFactory.openSession();
String hql = "from Classes c left outer join fetch c.students";
hql = "from Student s left outer join fetch s.classes c";
List<Classes> cList = session.createQuery(hql).list();
session.close();
return cList;
} /**
* 带select的查询
*/
public List<Classes> queryClasses_Student_Select(){
Session session = sessionFactory.openSession();
String hql = "select new cn.itcast.hiberate.sh.domain.ClassesView(c.cname,s.sname) " +
"from Student s left outer join s.classes c";
List<Classes> cList = session.createQuery(hql).list();
session.close();
return cList;
} /**
* 多对多
*/
public void testQueryCourse_Student(){
Session session = sessionFactory.openSession();
List<Student> studentList = session.createQuery("from Student s inner join fetch s.courses c").list();
session.close();
} /**
* 一对多结合多对多
*/
public List<Student> queryClasses_Student_Course(){
Session session = sessionFactory.openSession();
String hql = "from Classes cs inner join fetch cs.students s inner join fetch s.courses c";
hql = "from Student s inner join fetch s.classes cs inner join fetch s.courses c";
//hql = "from Classes cs left outer join fetch cs.students s left outer join fetch s.courses c";
List<Student> cList = session.createQuery(hql).list();
// Set<Classes> cset = new HashSet<Classes>(cList);
// cList = new ArrayList<Classes>(cset);
System.out.println(cList.size());
// for(Classes classes:cList){
// System.out.println(classes.getCid());
// Set<Student> students = classes.getStudents();
// for(Student student:students){
// System.out.println(student.getSname());
// Set<Course> courses = student.getCourses();
// for(Course course:courses){
// System.out.println(course.getCname());
// }
// }
// }
session.close();
return cList;
}
}
hql查询语句 内存中的情况,fetch迫切查询关键字的更多相关文章
- 如何在SQL Server查询语句(Select)中检索存储过程(Store Procedure)的结果集?
如何在SQL Server查询语句(Select)中检索存储过程(Store Procedure)的结果集?(2006-12-14 09:25:36) 与这个问题具有相同性质的其他描述还包括:如何 ...
- T_SQL查询语句(一): 单表查询
############################################ 查询语句--SELECT ########################################## ...
- Java的各类型数据在内存中分配情况详解
1. 有这样一种说法,如今争锋于IT战场的两大势力,MS一族偏重于底层实现,Java一族偏重于系统架构.说法根据无从考证,但从两大势力各自的社区力量和图书市场已有佳作不难看出,此说法不虚,但 ...
- PHP实现查询Memcache内存中的所有键与值
使用Memcache时,我们可以用memcache提供的get方法,通过键查询到当前的数据,但是有时候需要查询内存中所有的键和值,这个时候可以使用下面的代码实现: <?php /** * Cre ...
- MYSQL——解题查询语句答题思路,再难的查询都不怕!
select查询语句,作为测试人员,使用此语句是家常便饭,是必须掌握的部分,由开始学习mysql到网上搜索试题做,开始做题一塌糊涂,拿到题目就晕,无从下手,现在慢慢总结了一套自己做题的方式,很开森,嘿 ...
- SpringBoot使用注解的方式构建Elasticsearch查询语句,实现多条件的复杂查询
背景&痛点 通过ES进行查询,如果需要新增查询条件,则每次都需要进行硬编码,然后实现对应的查询功能.这样不仅开发工作量大,而且如果有多个不同的索引对象需要进行同样的查询,则需要开发多次,代码复 ...
- 查询语句为“%string_”的情况
select * from t_user where user_name like '%Joe_%'实际查询出来的语句为: 而不像预计的前两条.
- Oracle中分页查询语句
Oracle分页查询语句使我们最常用的语句之一,下面就为您介绍的Oracle分页查询语句的用法,如果您对此方面感兴趣的话,不妨一看. Oracle分页查询语句基本上可以按照本文给出的格式来进行套用.O ...
- (数据科学学习手册28)SQL server 2012中的查询语句汇总
一.简介 数据库管理系统(DBMS)最重要的功能就是提供数据查询,即用户根据实际需求对数据进行筛选,并以特定形式进行显示.在Microsoft SQL Serve 2012 中,可以使用通用的SELE ...
随机推荐
- Java多线程-线程的调度(守护线程)
本文转自http://www.cnblogs.com/linjiqin/p/3210004.html 感谢作者 守护线程与普通线程写法上基本没啥区别,调用线程对象的方法setDaemon(true), ...
- JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-001Hibernate映射继承的方法
There are four different strategies for representing an inheritance hierarchy: Use one table per co ...
- BS与CS的比较
http://www.oschina.net/news/57811/open-web-not-died?p=2#comments 浏览器是为浏览文档设计的,它的界面布局其实只有从上至下的流布局一种,浏 ...
- C#枚举硬件设备(升级版)
原文:C#枚举硬件设备(升级版) 先取设备类型: ; } }
- SSIS ->> Null & Null Functions
SSIS不支持值为NULL的变量.每种类型的变量都有自己的默认值. 做了一个测试,用一个Execute SQL Task输出一个NULL值给A变量,然后把A变量传到到另外一个Execute SQL T ...
- USACO Section 3.3: Home on the Range
到最后发现是DP题 /* ID: yingzho1 LANG: C++ TASK: range */ #include <iostream> #include <fstream> ...
- php多维数组化一维数组
一.使用foreach <?php function arr_foreach ($arr) { static $tmp=array(); if (!is_array ($arr)) { retu ...
- ACdream 1735 输油管道 (排序)
http://acdream.info/problem?pid=1735 官方题解:http://acdream.info/topic?tid=4246 因为主干线是平行于x轴的直线,那么跟x坐标其实 ...
- java获取系统指定时间年月日
java获取系统指定时间年月日 private String setDateTime(String falg) { Calendar c = Calendar.getInstance(); c.set ...
- 聚集索引、非聚集索引、聚集索引组织表、堆组织表、Mysql/PostgreSQL对比、联合主键/自增长、InnoDB/MyISAM(引擎方面另开一篇)
参考了多篇文章,分别记录,如下. 下面是第一篇的总结 http://www.jb51.net/article/76007.htm: 在MySQL中,InnoDB引擎表是(聚集)索引组织表(cluste ...