博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hql查询语句 内存中的情况,fetch迫切查询关键字
阅读量:6799 次
发布时间:2019-06-26

本文共 11551 字,大约阅读时间需要 38 分钟。

 Classes.java

1 package cn.itcast.hiberate.sh.domain; 2  3 import java.io.Serializable; 4 import java.util.Set; 5  6 public class Classes implements Serializable{ 7     private Long cid; 8     private String cname; 9     private String description;10     11     public Classes(){}12     13     public Classes(String cname,String description){14         this.cname = cname;15         this.description = description;16     }17     18     public Long getCid() {19         return cid;20     }21 22     public void setCid(Long cid) {23         this.cid = cid;24     }25 26     public String getCname() {27         return cname;28     }29 30     public void setCname(String cname) {31         this.cname = cname;32     }33 34     public String getDescription() {35         return description;36     }37 38     public void setDescription(String description) {39         this.description = description;40     }41 42     public Set
getStudents() {43 return students;44 }45 46 public void setStudents(Set
students) {47 this.students = students;48 }49 50 private Set students;51 }
View Code
1 
2 4
5
6
7
8
9
10 11
12
30
31
34
35
36
37
38
39
40
View Code

Course.java

1 package cn.itcast.hiberate.sh.domain; 2  3 import java.io.Serializable; 4 import java.util.Set; 5  6 public class Course implements Serializable{ 7     private Long cid; 8     private String cname; 9     private String description;10     11     public Long getCid() {12         return cid;13     }14 15     public void setCid(Long cid) {16         this.cid = cid;17     }18 19     public String getCname() {20         return cname;21     }22 23     public void setCname(String cname) {24         this.cname = cname;25     }26 27     public String getDescription() {28         return description;29     }30 31     public void setDescription(String description) {32         this.description = description;33     }34 35     public Set
getStudents() {36 return students;37 }38 39 public void setStudents(Set
students) {40 this.students = students;41 }42 43 private Set
students;44 }
View Code
1 
2 4
7
8
9
10
11
12
13
14
18
19
20
23
24
25
28
29
30
31
View Code

Student.java

1 package cn.itcast.hiberate.sh.domain; 2  3 import java.io.Serializable; 4 import java.util.HashSet; 5 import java.util.Set; 6  7 public class Student implements Serializable{ 8     private Long sid; 9     private String sname;10     11     private Classes classes;12     13     private Set
courses = new HashSet
(0);14 15 public Set
getCourses() {16 return courses;17 }18 public void setCourses(Set
courses) {19 this.courses = courses;20 }21 public Classes getClasses() {22 return classes;23 }24 public void setClasses(Classes classes) {25 this.classes = classes;26 }27 public Long getSid() {28 return sid;29 }30 public void setSid(Long sid) {31 this.sid = sid;32 }33 public String getSname() {34 return sname;35 }36 public void setSname(String sname) {37 this.sname = sname;38 }39 public String getDescription() {40 return description;41 }42 public void setDescription(String description) {43 this.description = description;44 }45 private String description;46 }
View Code
1 
2 4
5
6
7
8
9
10 11
12
30
31
34
35
36
37
38
39
40
View Code

Hibernate.cfg.xml

1 
2 5
6
9
10
13
root
14 15
16 com.mysql.jdbc.Driver17
18 19
22
friends
23
26
27 jdbc:mysql://localhost:3306/mydatabase28
29
36
update
37 38
39 org.hibernate.dialect.MySQLInnoDBDialect40
41
44
true
45
true
46
47
48
49 50
51
View Code

 

 

 

1 package cn.itcast.hibernate.sh.dao;  2   3 import java.util.ArrayList;  4 import java.util.HashSet;  5 import java.util.List;  6 import java.util.Set;  7   8 import org.hibernate.Query;  9 import org.hibernate.Session; 10 import org.junit.Test; 11  12 import cn.itcast.hiberate.sh.domain.Classes; 13 import cn.itcast.hiberate.sh.domain.Course; 14 import cn.itcast.hiberate.sh.domain.Student; 15 import cn.itcast.hibernate.sh.utils.HiberanteUtils; 16  17 /** 18  * 单表 19  *  20  * 一对多和多对多 21  *  22  * 多表的结合 23  * @author Think 24  * 25  */ 26 public class HQLDao extends HiberanteUtils{ 27     public List
queryAllClasses(){ 28 Session session = sessionFactory.openSession(); 29 List
cList = session.createQuery("from Classes").list(); 30 session.close(); 31 return cList; 32 } 33 34 public List queryClasses_Properties(){ 35 Session session = sessionFactory.openSession(); 36 List cList = session.createQuery("select cid,cname from Classes").list(); 37 session.close(); 38 return cList; 39 } 40 41 public List
queryClasses_Constructor(){ 42 Session session = sessionFactory.openSession(); 43 List
cList = session.createQuery("select new cn.itcast.hiberate.sh.domain.Classes(cname,description) from Classes").list(); 44 session.close(); 45 return cList; 46 } 47 48 public Classes queryClasses_Condition(){ 49 Session session = sessionFactory.openSession(); 50 Query query = session.createQuery("select new cn.itcast.hiberate.sh.domain.Classes(cname,description) from Classes where cid=:cid"); 51 query.setLong("cid", 1L); 52 Classes classes = (Classes)query.uniqueResult(); 53 System.out.println(classes.getCname()); 54 session.close(); 55 return classes; 56 } 57 58 public Classes queryClasses_Condition_2(){ 59 Session session = sessionFactory.openSession(); 60 Query query = session.createQuery("select new cn.itcast.hiberate.sh.domain.Classes(cname,description) from Classes where cid=?"); 61 query.setLong(0, 1L); 62 Classes classes = (Classes)query.uniqueResult(); 63 System.out.println(classes.getCname()); 64 session.close(); 65 return classes; 66 } 67 68 /** 69 * order by,group by,sun,min,max,avg,having等都适用 70 * @return 71 */ 72 73 /** 74 * 子查询 75 */ 76 public void queryClasses_SubSelect(){ 77 Session session = sessionFactory.openSession(); 78 List
cList = session.createQuery("from Classes where cid in(select cid from Classes where cid in(1,2,3))").list(); 79 session.close(); 80 } 81 82 public static HQLDao getInstance(){ 83 return new HQLDao(); 84 } 85 86 /*********************************************************************************************/ 87 /** 88 * 一对多 89 * 等值连接 查询出来的机构很差 90 * 内连接 91 * 左外连接 92 * 迫切左外连接 93 */ 94 public List
queryClasses_Student_EQ(){ 95 Session session = sessionFactory.openSession(); 96 List
cList = session.createQuery("from Classes c,Student s where c.cid=s.classes.cid").list(); 97 session.close(); 98 return cList; 99 }100 101 /**102 * 内连接103 * @return104 */105 public List
queryClasses_Student_INNER(){106 Session session = sessionFactory.openSession();107 List
cList = session.createQuery("from Classes c inner join c.students").list();108 session.close();109 return cList;110 }111 112 /**113 * 迫切内连接114 * @return115 */116 public List
queryClasses_Student_INNER_FETCH(){117 Session session = sessionFactory.openSession();118 List
cList = session.createQuery("from Classes c inner join fetch c.students").list();119 session.close();120 return cList;121 }122 123 /**124 * 左外连接125 */126 public List
queryClasses_Student_LeftJoin(){127 Session session = sessionFactory.openSession();128 List
cList = session.createQuery("from Classes c left outer join c.students").list();129 session.close();130 return cList;131 }132 133 /**134 * 迫切左外连接135 */136 public List
queryClasses_Student_LeftJoin_fetch(){137 Session session = sessionFactory.openSession();138 String hql = "from Classes c left outer join fetch c.students";139 hql = "from Student s left outer join fetch s.classes c";140 List
cList = session.createQuery(hql).list();141 session.close();142 return cList;143 }144 145 /**146 * 带select的查询147 */148 public List
queryClasses_Student_Select(){149 Session session = sessionFactory.openSession();150 String hql = "select new cn.itcast.hiberate.sh.domain.ClassesView(c.cname,s.sname) " +151 "from Student s left outer join s.classes c";152 List
cList = session.createQuery(hql).list();153 session.close();154 return cList;155 }156 157 /**158 * 多对多159 */160 public void testQueryCourse_Student(){161 Session session = sessionFactory.openSession();162 List
studentList = session.createQuery("from Student s inner join fetch s.courses c").list();163 session.close();164 }165 166 167 /**168 * 一对多结合多对多169 */170 public List
queryClasses_Student_Course(){171 Session session = sessionFactory.openSession();172 String hql = "from Classes cs inner join fetch cs.students s inner join fetch s.courses c";173 hql = "from Student s inner join fetch s.classes cs inner join fetch s.courses c";174 //hql = "from Classes cs left outer join fetch cs.students s left outer join fetch s.courses c";175 List
cList = session.createQuery(hql).list();176 // Set
cset = new HashSet
(cList);177 // cList = new ArrayList
(cset);178 System.out.println(cList.size());179 // for(Classes classes:cList){180 // System.out.println(classes.getCid());181 // Set
students = classes.getStudents();182 // for(Student student:students){183 // System.out.println(student.getSname());184 // Set
courses = student.getCourses();185 // for(Course course:courses){186 // System.out.println(course.getCname());187 // }188 // }189 // }190 session.close();191 return cList;192 }193 }

 

 

转载于:https://www.cnblogs.com/friends-wf/p/3779747.html

你可能感兴趣的文章
head first python 6 class 扩展
查看>>
大数据,多大算“大
查看>>
「镁客·请讲」前知智能唐宝:中国金属3D打印最大的挑战是软硬件的自研与国产化 ...
查看>>
EMNLP2018 - 语言理解+对话系统的最新进展
查看>>
如何解决移动电商平台中的“伪曝光”?
查看>>
迁云工具版本更新(1.3.2.5)
查看>>
使用golang编写prometheus metrics exporter
查看>>
基于python开发的股市行情看板
查看>>
linux进程管理总结
查看>>
Linux学习笔记(1)--基本命令
查看>>
Longhorn:实现Kubernetes集群的持久化存储
查看>>
阿里云 Aliplayer高级功能介绍(三):多字幕
查看>>
Data Lake Analytics: 以SQL方式查询Redis数据
查看>>
一条查询sql的执行流程和底层原理
查看>>
ActiveMQ多个消费者消费不均匀问题
查看>>
ovirt自承载引擎安装配置 安装过程中的FQDN问题
查看>>
小米进军欧洲智能手机市场:一面是狂欢,一面是考验
查看>>
提高IO性能(只需要设置 noatime)
查看>>
批处理 启动和关闭 Oracle 11g 服务
查看>>
二手车服务商完成A轮融资,投资方为标志雪铁龙集团
查看>>