HQL(Hibernate Query Language)
Posted 流年如水~烟雨随风
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HQL(Hibernate Query Language)相关的知识,希望对你有一定的参考价值。
1. NativeSQL > HQL > EJB QL(JP QL 1.0) > QBC(Query By Criteria) > QBE(Query By Example)
2. 总结:QL应该和导航关系结合,共同为查询提供服务。
以下图为例:
Category:
1 package com.bjsxt.hibernate; 2 3 import javax.persistence.Entity; 4 import javax.persistence.GeneratedValue; 5 import javax.persistence.Id; 6 7 @Entity 8 public class Category { 9 10 private Integer id; 11 12 private String name; 13 14 @Id 15 @GeneratedValue 16 public Integer getId() { 17 return id; 18 } 19 20 public void setId(Integer id) { 21 this.id = id; 22 } 23 24 public String getName() { 25 return name; 26 } 27 28 public void setName(String name) { 29 this.name = name; 30 } 31 }
Topic:
1 package com.bjsxt.hibernate; 2 3 import java.util.Date; 4 5 import javax.persistence.Entity; 6 import javax.persistence.FetchType; 7 import javax.persistence.GeneratedValue; 8 import javax.persistence.Id; 9 import javax.persistence.ManyToOne; 10 11 @Entity 12 public class Topic { 13 14 private Integer id; 15 16 private String title; 17 18 private Category category; 19 20 private Date createDate; 21 22 @Id 23 @GeneratedValue 24 public Integer getId() { 25 return id; 26 } 27 28 public void setId(Integer id) { 29 this.id = id; 30 } 31 32 public String getTitle() { 33 return title; 34 } 35 36 public void setTitle(String title) { 37 this.title = title; 38 } 39 40 @ManyToOne(fetch=FetchType.LAZY) 41 public Category getCategory() { 42 return category; 43 } 44 45 public void setCategory(Category category) { 46 this.category = category; 47 } 48 49 public Date getCreateDate() { 50 return createDate; 51 } 52 53 public void setCreateDate(Date createDate) { 54 this.createDate = createDate; 55 } 56 }
Msg:
1 package com.bjsxt.hibernate; 2 3 import javax.persistence.Entity; 4 import javax.persistence.GeneratedValue; 5 import javax.persistence.Id; 6 import javax.persistence.ManyToOne; 7 8 @Entity 9 public class Msg { 10 11 private Integer id; 12 13 private String cont; 14 15 private Topic topic; 16 17 @Id 18 @GeneratedValue 19 public Integer getId() { 20 return id; 21 } 22 23 public void setId(Integer id) { 24 this.id = id; 25 } 26 27 public String getCont() { 28 return cont; 29 } 30 31 public void setCont(String cont) { 32 this.cont = cont; 33 } 34 35 @ManyToOne 36 public Topic getTopic() { 37 return topic; 38 } 39 40 public void setTopic(Topic topic) { 41 this.topic = topic; 42 } 43 }
MsgInfo:
1 package com.bjsxt.hibernate; 2 3 //VO(Value Object) DTO(Data Transfer Object) 4 public class MsgInfo { 5 6 private Integer id; 7 8 private String cont; 9 10 private String topicName; 11 12 private String categoryName; 13 14 public MsgInfo(Integer id,String cont,String topicName,String categoryName){ 15 this.id = id; 16 this.cont = cont; 17 this.topicName = topicName; 18 this.categoryName = categoryName; 19 } 20 21 public Integer getId() { 22 return id; 23 } 24 25 public void setId(Integer id) { 26 this.id = id; 27 } 28 29 public String getCont() { 30 return cont; 31 } 32 33 public void setCont(String cont) { 34 this.cont = cont; 35 } 36 37 public String getTopicName() { 38 return topicName; 39 } 40 41 public void setTopicName(String topicName) { 42 this.topicName = topicName; 43 } 44 45 public String getCategoryName() { 46 return categoryName; 47 } 48 49 public void setCategoryName(String categoryName) { 50 this.categoryName = categoryName; 51 } 52 }
HibernateQLTest1:
1 package com.bjsxt.hibernate; 2 3 import java.util.Date; 4 import java.util.List; 5 6 import org.hibernate.Query; 7 import org.hibernate.Session; 8 import org.hibernate.SessionFactory; 9 import org.hibernate.cfg.AnnotationConfiguration; 10 import org.hibernate.tool.hbm2ddl.SchemaExport; 11 import org.junit.After; 12 import org.junit.Before; 13 import org.junit.Test; 14 15 public class HibernateQLTest { 16 private static SessionFactory sf = null; 17 18 @Before 19 public void beforeClass(){ 20 // new SchemaExport(new AnnotationConfiguration().configure()).create(false, true); 21 sf = new AnnotationConfiguration().configure().buildSessionFactory(); 22 } 23 24 @After 25 public void afterClass(){ 26 if(sf != null){ 27 sf.close(); 28 } 29 } 30 31 @Test 32 public void testSave(){ 33 34 Session session = sf.getCurrentSession(); 35 session.beginTransaction(); 36 37 for(int i=0; i<10; i++){ 38 Category c = new Category(); 39 c.setName("c" + i); 40 session.save(c); 41 } 42 43 for(int i=0; i<10; i++){ 44 Category c = new Category(); 45 c.setId(1); 46 Topic t = new Topic(); 47 t.setCategory(c); 48 t.setCreateDate(new Date()); 49 t.setTitle("t"+i); 50 session.save(t); 51 } 52 53 for(int i=0; i<10; i++){ 54 Topic t = new Topic(); 55 t.setId(1); 56 Msg m = new Msg(); 57 m.setTopic(t); 58 m.setCont("m" + i); 59 session.save(m); 60 } 61 62 session.getTransaction().commit(); 63 } 64 65 @Test 66 public void testHQL_01(){ 67 Session session = sf.getCurrentSession(); 68 session.beginTransaction(); 69 Query q = (Query) session.createQuery("from Category"); 70 List<Category> categories = (List<Category>)q.list(); 71 for(Category c : categories){ 72 System.out.println(c.getName()); 73 } 74 session.getTransaction().commit(); 75 } 76 77 @Test 78 public void testHQL_02(){ 79 Session session = sf.getCurrentSession(); 80 session.beginTransaction(); 81 Query q = (Query) session.createQuery("from Category c where c.name > \'c5\'"); 82 List<Category> categories = (List<Category>)q.list(); 83 for(Category c : categories){ 84 System.out.println(c.getName()); 85 } 86 session.getTransaction().commit(); 87 } 88 89 @Test 90 public void testHQL_03(){ 91 Session session = sf.getCurrentSession(); 92 session.beginTransaction(); 93 Query q = (Query) session.createQuery("from Category c order by name desc"); 94 List<Category> categories = (List<Category>)q.list(); 95 for(Category c : categories){ 96 System.out.println(c.getName()); 97 } 98 session.getTransaction().commit(); 99 } 100 101 @Test 102 public void testHQL_04(){ 103 Session session = sf.getCurrentSession(); 104 session.beginTransaction(); 105 Query q = (Query) session.createQuery( 106 "select distinct c from Category c order by name desc");//主键不同 107 List<Category> categories = (List<Category>)q.list(); 108 for(Category c : categories){ 109 System.out.println(c.getName()); 110 } 111 session.getTransaction().commit(); 112 } 113 114 @Test 115 public void testHQL_05(){ 116 Session session = sf.getCurrentSession(); 117 session.beginTransaction(); 118 /*Query q = (Query) session.createQuery( 119 "from Category c where c.id > :min and c.id < :max");//主键不同 120 // q.setParameter("min", 2); 121 // q.setParameter("max", 8); 122 q.setInteger("min", 2); 123 q.setInteger("max", 8);*/ 124 125 Query q = (Query) session.createQuery("from Category c where c.id > :min and c.id < :max") 126 .setInteger("min", 2) 127 .setInteger("max", 8); 128 List<Category> categories = (List<Category>)q.list(); 129 for(Category c : categories){ 130 System.out.println(c.getId() + "-" +c.getName()); 131 } 132 session.getTransaction().commit(); 133 } 134 135 /** 136 * 链式编程 137 */ 138 @Test 139 public void testHQL_06(){ 140 Session session = sf.getCurrentSession(); 141 session.beginTransaction(); 142 Query q = (Query) session.createQuery("from Category c where c.id > ? and c.id < ?") 143 .setInteger(0, 2)//索引从0开始 144 .setInteger(1, 8); 145 List<Category> categories = (List<Category>)q.list(); 146 for(Category c : categories){ 147 System.out.println(c.getId() + "-" +c.getName()); 148 } 149 session.getTransaction().commit(); 150 } 151 152 /** 153 * 分页 154 */ 155 @Test 156 public void testHQL_07(){ 157 Session session = sf.getCurrentSession(); 158 session.beginTransaction(); 159 Query q = (Query) session.createQuery("from Category c order by name desc"); 160 q.setMaxResults(4);//每页4条 161 q.setFirstResult(2);//从第二行开始 162 List<Category> categories = (List<Category>)q.list(); 163 for(Category c : categories){ 164 System.out.println(c.getId() + "-" +c.getName()); 165 } 166 session.getTransaction().commit(); 167 } 168 169 /** 170 * 分页(换一种获取方式) 171 */ 172 @Test 173 public void testHQL_08(){ 174 Session session = sf.getCurrentSession(); 175 session.beginTransaction(); 176 Query q = (Query) session.createQuery("select c.id,c.name from Category c order by name desc"); 177 q.setMaxResults(4);//每页4条 178 q.setFirstResult(2);//从第二行开始 179 List<Object[]> categories = (List<Object[]>)q.list(); 180 for(Object[] c : categories){ 181 System.out.println(c[0] + "-" +c[1]); 182 } 183 session.getTransaction().commit(); 184 } 185 186 /** 187 * 设定 fetch type 为 lazy 后将不会有第二条sql语句 188 */ 189 @Test 190 public void testHQL_09(){ 191 Session session = sf.getCurrentSession(); 192 session.beginTransaction(); 193 Query q = session.createQuery("from Topic t where t.category.id = 1"); 194 List<Topic> topics = (List<Topic>)q.list(); 195 for(Topic t : topics){ 196 System.out.println(t.getId() + "-" + t.getTitle()); 197 // System.out.println(t.getCategory().getName()); 198 } 199 session.getTransaction().commit(); 200 } 201 202 /** 203 * 设定 fetch type 为 lazy 后将不会有第二条sql语句 204 */ 205 @Test 206 public void testHQL_10(){ 207 Session session = sf.getCurrentSession(); 208 session.beginTransaction(); 209 Query q = session.createQuery("from Topic t where t.category.id = 1"); 210 List<Topic> topics = (List<Topic>)q.list(); 211 for(Topic t : topics){ 212 System.out.println(t.getId() + "-" + t.getTitle()); 213 // System.out.println(t.getCategory().getName()); 214 } 215 session.getTransaction().commit(); 216 } 217 218 @Test 219 public void testHQL_11(){ 220 Session session = sf.getCurrentSession(); 221 session.beginTransaction(); 222 Query q = session.createQuery("from Msg m where m.topic.category.id = 1"); 223 for(Object o : q.list()){ 224 Msg m = (Msg)o; 225 System.out.println(m.getCont()); 226 } 227 session.getTransaction().commit(); 228 } 229 230 /** 231 * VO(Value Object) 232 * DTO(Data Transfer Object) 233 */ 234 @Test 235 public void testHQL_12(){ 236 Session session = sf.getCurrentSession(); 237 session.beginTransaction(); 238 Query q = session.createQuery("select new com.bjsxt.hibernate.MsgInfo(m.id, m.cont, m.topic.title, m.topic.category.name) from Msg m"); 239 for(Object o : q.list()){ 240 MsgInfo m = (MsgInfo)o; 241 System.out.println(m.getCont()); 242 } 243 session.getTransaction().commit(); 244 } 245 246 //动手测试left right join 247 //为什么不能直接写 Category 名,而必须写 t.category 248 //因为有可能存在多个成员变量(同一个类),需要指明哪一个成员变量的连接条件来做连接 249 @Test 250 public void testHQL_13(){ 251 Session session = sf.getCurrentSession(); 252 session.beginTransaction(); 253 Query q = session.createQuery("select t.title,c.name from Topic t join t.category c"); 254 List<Object[]> list = q.list(); 255 for(Object[] o : list){ 256 System.out.println(o[0] + "-" + o[1]); 257 } 258 session.getTransaction().commit(); 259 } 260 261 //学习使用 uniqueResult 262 @Test 263 public void testHQL_14(){ 264 Session session = sf.getCurrentSession(); 265 session.beginTransaction(); 266 Query q = session.createQuery("from Msg m where m = :MsgToSearch");//不重要 267 Msg m = new Msg(); 268 m.setId(1); 269以上是关于HQL(Hibernate Query Language)的主要内容,如果未能解决你的问题,请参考以下文章HQL: The Hibernate Query Language
常用HQL(Hibernate Query Language)查询