原文出处:http://penghao122.iteye.com/blog/211581
Hibernate junit SQL C C++
Hibernate hql
* 注意hql的大小写敏感性
1、实体对象的查询,查询的是实体对象的数据【重要】
* n+1问题,在默认配置的情况下,使用query.iterate()操作,有可能有n+1问题,所谓
n+1,指在查询对象数据的时候,发出了n+1条查询语句。
1:首先发出了一条查询语句,查询对象的id列表
n:在迭代访问每个对象的时候,如果缓存中没有对象数据,Hibernate会在此发出一条查询语句,
查询相应的对象
*List操作与Iterate操作的区别
list,每次都会发出一条查询语句,查询所有的对象
iterate,首先发出一条查询语句,查询对象的id列表,然后根据缓存情况,决定
是否发出更多的查询语句,来查询对象数据
参见:SimpleObjectQueryTest2.java
2、简单属性查询
* 单个属性查询,返回的结果集属性的列表,其元素和属性的类型一致
* 多个属性查询,返回的结果集是数组类型,数组的长度和查询的属性数量相关,数组中元素的类型与相应的属性类型相同
参见:SimplePropertyQueryTest.java
3、条件查询【重要】
* 可以拼字符串的形式传递参数
* 可以用过?来传递参数(注意索引值是从0开始的,跟jdbc不同,jdbc是从1开始的)
* 可以通过 :参数名 来传递参数(即命名参数方式)
* 如果对应的参数值是多个,可以调用setParamterList()方法开传递
* 在HQL中可以使用数据库的函数,如date_format()
参见:SimpleConditionQueryTest.java
4、hibernate也支持直接使用原生sql进行查询
参见:SqlQueryTest.java
5、外置的命名查询
* 在映射文件中通过
标签定义hql
* 在程序中使用session.getNameQuery()方法获得这个查询
参见:Student.hbm.xml,NameQueryTest.java
6、查询过滤器
* 定义过滤器参数
* 在类映射文件中使用这些参数
* 在session中启用过滤器
参见:Student.hbm.xml,FilterQueryTest.java
7、对象导航查询,在HQL语句中,可以使用.的方式进行对象导航【重要】
参见:ObjectNavQueryTest.java
8、连接查询(在对象里可以直接导航)【重要】
* 内连接
* 外连接(左连接/右连接)
参见:JionQueryTest
9、统计查询
参见:StatQueryTest.java
10、分页查询【重要】
* 通过query接口中的setFirstResult()和setMaxResults() 进行分页
参见:PageQueryTest
11、DML风格的操作(尽量)
- package com.bjsxt.hibernate;
-
- import java.util.Iterator;
- import java.util.List;
-
- import org.hibernate.Session;
-
- import junit.framework.TestCase;
-
- public class JionQueryTest extends TestCase {
-
-
-
-
-
- public void testQuery1() {
- Session session = null;
- try {
- session = HibernateUtils.getSession();
-
- List students = session.createQuery("select c.name, s.name from Student s join s.classes c ")
- .list();
- for (Iterator iter = students.iterator();iter.hasNext();) {
- Object[] obj = (Object[])iter.next();
- System.out.println(obj[0] + ", " + obj[1]);
- }
- }catch(Exception e) {
- e.printStackTrace();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
-
-
-
-
-
- public void testQuery2() {
- Session session = null;
- try {
- session = HibernateUtils.getSession();
-
- List students = session.createQuery("select c.name, s.name from Classes c left join c.students s ")
- .list();
- for (Iterator iter = students.iterator();iter.hasNext();) {
- Object[] obj = (Object[])iter.next();
- System.out.println(obj[0] + ", " + obj[1]);
- }
- }catch(Exception e) {
- e.printStackTrace();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
-
-
-
-
-
- public void testQuery3() {
- Session session = null;
- try {
- session = HibernateUtils.getSession();
-
- List students = session.createQuery("select c.name, s.name from Classes c right join c.students s ")
- .list();
- for (Iterator iter = students.iterator();iter.hasNext();) {
- Object[] obj = (Object[])iter.next();
- System.out.println(obj[0] + ", " + obj[1]);
- }
- }catch(Exception e) {
- e.printStackTrace();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
-
-
-
-
-
- private void testQuery4() {
-
- }
- }
-
-
- package com.bjsxt.hibernate;
-
- import java.util.Iterator;
- import java.util.List;
-
- import org.hibernate.Session;
-
- import junit.framework.TestCase;
-
-
-
-
-
-
- public class NameQueryTest extends TestCase {
-
- public void testQuery1() {
- Session session = null;
- try {
- session = HibernateUtils.getSession();
-
- List students = session.getNamedQuery("searchStudents")
- .setParameter(0, 10)
- .list();
-
- for (Iterator iter = students.iterator();iter.hasNext();) {
- Student student = (Student)iter.next();
- System.out.println(student.getName());
- }
- }catch(Exception e) {
- e.printStackTrace();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
- }
-
-
-
- package com.bjsxt.hibernate;
-
- import java.util.Iterator;
- import java.util.List;
-
- import org.hibernate.Session;
-
- import junit.framework.TestCase;
-
-
-
-
-
-
- public class ObjectNavQueryTest extends TestCase {
-
- public void testQuery1() {
- Session session = null;
- try {
- session = HibernateUtils.getSession();
-
- List students = session.createQuery("select s.name from Student s where s.classes.id < 2").list();
- for (Iterator iter = students.iterator();iter.hasNext();) {
- String name = (String)iter.next();
- System.out.println(name);
- }
- }catch(Exception e) {
- e.printStackTrace();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
- }
-
-
-
-
- package com.bjsxt.hibernate;
-
- import java.util.Iterator;
- import java.util.List;
-
- import org.hibernate.Query;
- import org.hibernate.Session;
-
- import junit.framework.TestCase;
-
-
-
-
-
-
- public class PageQueryTest extends TestCase {
-
- public void testQuery1() {
- Session session = null;
- try {
- session = HibernateUtils.getSession();
- Query query = session.createQuery("from Student");
- query.setFirstResult(27);
- query.setMaxResults(5);
- List students = query.list();
- for (Iterator iter = students.iterator();iter.hasNext();) {
- Student student = (Student)iter.next();
- System.out.println(student.getId() + ", " + student.getName());
- }
- }catch(Exception e) {
- e.printStackTrace();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
- }
-
-
-
- package com.bjsxt.hibernate;
-
- import java.text.SimpleDateFormat;
- import java.util.Iterator;
- import java.util.List;
-
- import org.hibernate.Query;
- import org.hibernate.Session;
-
- import junit.framework.TestCase;
-
- public class SimpleConditionQueryTest extends TestCase {
-
- public void testQuery1() {
- Session session = null;
- try {
- session = HibernateUtils.getSession();
-
-
- List students = session.createQuery("SELECT s.id, s.name FROM Student s WHERE s.name LIKE '%1%'")
- .list();
- for (Iterator iter = students.iterator();iter.hasNext();) {
- Object[] obj = (Object[])iter.next();
- System.out.println(obj[0] + ", " + obj[1]);
- }
- }catch(Exception e) {
- e.printStackTrace();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
-
- public void testQuery2() {
- Session session = null;
- try {
- session = HibernateUtils.getSession();
-
-
- Query query = session.createQuery("SELECT s.id, s.name FROM Student s WHERE s.name LIKE ?");
-
-
-
-
- query.setParameter(0, "%1%");
-
- List students = query.list();
-
- for (Iterator iter = students.iterator();iter.hasNext();) {
- Object[] obj = (Object[])iter.next();
- System.out.println(obj[0] + ", " + obj[1]);
- }
- }catch(Exception e) {
- e.printStackTrace();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
-
- public void testQuery3() {
- Session session = null;
- try {
- session = HibernateUtils.getSession();
-
-
- Query query = session.createQuery("SELECT s.id, s.name FROM Student s WHERE s.name LIKE :myname");
-
-
- query.setParameter("myname", "%1%");
-
- List students = query.list();
-
- for (Iterator iter = students.iterator();iter.hasNext();) {
- Object[] obj = (Object[])iter.next();
- System.out.println(obj[0] + ", " + obj[1]);
- }
- }catch(Exception e) {
- e.printStackTrace();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
-
- public void testQuery4() {
- Session session = null;
- try {
- session = HibernateUtils.getSession();
-
-
- List students = session.createQuery("SELECT s.id, s.name FROM Student s WHERE s.name LIKE :myname and s.id = :myid")
- .setParameter("myname", "%1%")
- .setParameter("myid", 15)
- .list();
-
- for (Iterator iter = students.iterator();iter.hasNext();) {
- Object[] obj = (Object[])iter.next();
- System.out.println(obj[0] + ", " + obj[1]);
- }
- }catch(Exception e) {
- e.printStackTrace();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
-
- public void testQuery5() {
- Session session = null;
- try {
- session = HibernateUtils.getSession();
-
-
- List students = session.createQuery("SELECT s.id, s.name FROM Student s WHERE s.id in(:myids)")
- .setParameterList("myids", new Object[]{1, 3, 5})
- .list();
-
- for (Iterator iter = students.iterator();iter.hasNext();) {
- Object[] obj = (Object[])iter.next();
- System.out.println(obj[0] + ", " + obj[1]);
- }
- }catch(Exception e) {
- e.printStackTrace();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
-
- public void testQuery6() {
- Session session = null;
- try {
- session = HibernateUtils.getSession();
-
-
-
-
-
-
-
- List students = session.createQuery("SELECT s.id, s.name FROM Student s WHERE " +
- "substring(s.createTime, 1, 7) =?")
- .setParameter(0, "2007-01")
- .list();
-
- for (Iterator iter = students.iterator();iter.hasNext();) {
- Object[] obj = (Object[])iter.next();
- System.out.println(obj[0] + ", " + obj[1]);
- }
- }catch(Exception e) {
- e.printStackTrace();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
-
- public void testQuery7() {
- Session session = null;
- try {
- session = HibernateUtils.getSession();
-
-
-
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
- List students = session.createQuery("SELECT s.id, s.name FROM Student s WHERE " +
- "s.createTime between ? and ? ")
- .setParameter(0, format.parseObject("2007-01-01"))
- .setParameter(1, format.parseObject("2007-03-01"))
- .list();
-
- for (Iterator iter = students.iterator();iter.hasNext();) {
- Object[] obj = (Object[])iter.next();
- System.out.println(obj[0] + ", " + obj[1]);
- }
- }catch(Exception e) {
- e.printStackTrace();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
-
- }
-
-
-
- package com.bjsxt.hibernate;
-
- import java.util.Iterator;
- import java.util.List;
-
- import org.hibernate.Query;
- import org.hibernate.Session;
-
- import junit.framework.TestCase;
-
-
-
-
-
-
- public class SimpleObjectQueryTest2 extends TestCase {
-
- public void testQueryWithListMethod() {
- Session session = null;
- try {
- session = HibernateUtils.getSession();
-
-
-
-
-
-
-
- List students = session.createQuery("from Student").list();
- for (Iterator iter = students.iterator();iter.hasNext();) {
- Student student = (Student)iter.next();
- System.out.println(student.getName());
- }
- }catch(Exception e) {
- e.printStackTrace();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
-
- public void testQueryWithIterateMethod() {
- Session session = null;
- try {
- session = HibernateUtils.getSession();
-
-
-
-
-
-
-
- Query query = session.createQuery("from Student");
- Iterator students = query.iterate();
- while (students.hasNext()) {
- Student student = (Student)students.next();
- System.out.println(student.getName());
- }
- }catch(Exception e) {
- e.printStackTrace();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
-
- public void testQueryWithListAndIterate() {
- Session session = null;
- try {
- session = HibernateUtils.getSession();
- Query query = session.createQuery("from Student");
- List students = query.list();
- for (Iterator iter = students.iterator();iter.hasNext();) {
- Student student = (Student)iter.next();
- System.out.println(student.getName());
- }
-
-
-
-
-
-
- Iterator studentsIter = query.iterate();
- while (studentsIter.hasNext()) {
- Student student = (Student)studentsIter.next();
- System.out.println(student.getName());
- }
- }catch(Exception e) {
- e.printStackTrace();
- }finally {
- HibernateUtils.closeSession(session);
- }
-
- }
-
- public void testQueryWithListAndList() {
- Session session = null;
- try {
- session = HibernateUtils.getSession();
- Query query = session.createQuery("from Student");
- List students = query.list();
- for (Iterator iter = students.iterator();iter.hasNext();) {
- Student student = (Student)iter.next();
- System.out.println(student.getName());
- }
-
-
-
-
-
- students = query.list();
- for (Iterator iter = students.iterator();iter.hasNext();) {
- Student student = (Student)iter.next();
- System.out.println(student.getName());
- }
- }catch(Exception e) {
- e.printStackTrace();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
-
-
- }
-
-
- package com.bjsxt.hibernate;
-
- import java.util.Iterator;
- import java.util.List;
-
- import org.hibernate.Query;
- import org.hibernate.Session;
-
- import junit.framework.TestCase;
-
-
-
-
-
-
- public class SimpleObjectQueryTest extends TestCase {
-
- public void testQuery1() {
- Session session = null;
- try {
- session = HibernateUtils.getSession();
-
-
- List students = session.createQuery("from Student").list();
- for (Iterator iter = students.iterator();iter.hasNext();) {
-
-
- Student student = (Student)iter.next();
- System.out.println(student.getName());
- }
- }catch(Exception e) {
- e.printStackTrace();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
-
- public void testQuery2() {
- Session session = null;
- try {
- session = HibernateUtils.getSession();
-
-
- List students = session.createQuery("from Student s").list();
- for (Iterator iter = students.iterator();iter.hasNext();) {
-
-
- Student student = (Student)iter.next();
- System.out.println(student.getName());
- }
- }catch(Exception e) {
- e.printStackTrace();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
-
- public void testQuery3() {
- Session session = null;
- try {
- session = HibernateUtils.getSession();
-
-
- List students = session.createQuery("from Student as s").list();
- for (Iterator iter = students.iterator();iter.hasNext();) {
-
-
- Student student = (Student)iter.next();
- System.out.println(student.getName());
- }
- }catch(Exception e) {
- e.printStackTrace();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
-
- public void testQuery4() {
- Session session = null;
- try {
- session = HibernateUtils.getSession();
-
-
- List students = session.createQuery("select s from Student s").list();
- for (Iterator iter = students.iterator();iter.hasNext();) {
-
-
- Student student = (Student)iter.next();
- System.out.println(student.getName());
- }
- }catch(Exception e) {
- e.printStackTrace();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
-
- public void testQuery5() {
- Session session = null;
- try {
- session = HibernateUtils.getSession();
-
-
- List students = session.createQuery("select * from Student").list();
- for (Iterator iter = students.iterator();iter.hasNext();) {
-
-
- Student student = (Student)iter.next();
- System.out.println(student.getName());
- }
- }catch(Exception e) {
- e.printStackTrace();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
- }
-
-
- package com.bjsxt.hibernate;
-
- import java.util.Iterator;
- import java.util.List;
-
- import org.hibernate.Session;
-
- import junit.framework.TestCase;
-
-
-
-
-
-
- public class SimplePropertyQueryTest extends TestCase {
-
-
-
-
-
- public void testQuery1() {
- Session session = null;
- try {
- session = HibernateUtils.getSession();
-
- List students = session.createQuery("select name from Student").list();
- for (Iterator iter = students.iterator();iter.hasNext();) {
- String name = (String)iter.next();
- System.out.println(name);
- }
- }catch(Exception e) {
- e.printStackTrace();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
-
-
-
-
-
- public void testQuery2() {
- Session session = null;
- try {
- session = HibernateUtils.getSession();
-
-
-
- List students = session.createQuery("select id, name from Student").list();
- for (Iterator iter = students.iterator();iter.hasNext();) {
- Object[] obj = (Object[])iter.next();
- System.out.println(obj[0] + ", " + obj[1]);
- }
- }catch(Exception e) {
- e.printStackTrace();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
-
- public void testQuery3() {
- Session session = null;
- try {
- session = HibernateUtils.getSession();
-
-
-
-
- List students = session.createQuery("select s.id, s.name from Student s").list();
- for (Iterator iter = students.iterator();iter.hasNext();) {
- Object[] obj = (Object[])iter.next();
- System.out.println(obj[0] + ", " + obj[1]);
- }
- }catch(Exception e) {
- e.printStackTrace();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
-
- }
-
-
-
- package com.bjsxt.hibernate;
-
- import java.util.Iterator;
- import java.util.List;
-
- import org.hibernate.SQLQuery;
- import org.hibernate.Session;
-
- import junit.framework.TestCase;
-
-
-
-
-
-
- public class SqlQueryTest extends TestCase {
-
- public void testQuery1() {
- Session session = null;
- try {
- session = HibernateUtils.getSession();
-
- SQLQuery sqlQuery = session.createSQLQuery("select * from t_student");
- List students = sqlQuery.list();
- for (Iterator iter = students.iterator();iter.hasNext();) {
- Object[] obj = (Object[])iter.next();
- System.out.println(obj[0] + ", " + obj[1]);
- }
- }catch(Exception e) {
- e.printStackTrace();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
-
- }
-
-
-
- package com.bjsxt.hibernate;
-
- import java.util.Iterator;
- import java.util.List;
-
- import org.hibernate.Session;
-
- import junit.framework.TestCase;
-
- public class StatQueryTest extends TestCase {
-
- public void testQuery1() {
- Session session = null;
- try {
- session = HibernateUtils.getSession();
-
- List students = session.createQuery("select count(*) from Student").list();
- Long count = (Long)students.get(0);
- System.out.println(count);
-
-
-
-
- }catch(Exception e) {
- e.printStackTrace();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
-
- public void testQuery2() {
- Session session = null;
- try {
- session = HibernateUtils.getSession();
-
- List students = session.createQuery("select c.id, c.name, count(s) from Student s join " +
- "s.classes c group by c.id, c.name").list();
- for (Iterator iter = students.iterator();iter.hasNext();) {
- Object[] obj = (Object[])iter.next();
- System.out.println(obj[0] + ", " + obj[1] + ", " + obj[2]);
- }
- }catch(Exception e) {
- e.printStackTrace();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
-
- }
-
-
- package com.bjsxt.hibernate;
-
- import java.util.Iterator;
- import java.util.List;
-
- import org.hibernate.Session;
-
- import junit.framework.TestCase;
-
-
-
-
-
-
- public class FilterQueryTest extends TestCase {
-
- public void testQuery1() {
- Session session = null;
- try {
- session = HibernateUtils.getSession();
-
- session.enableFilter("idfilter")
- .setParameter("myid", 10);
-
- List students = session.createQuery("from Student")
- .list();
-
- for (Iterator iter = students.iterator();iter.hasNext();) {
-
- Student student = (Student)iter.next();
- System.out.println(student.getName());
- }
- }catch(Exception e) {
- e.printStackTrace();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
-
- }
-
-
- package com.bjsxt.hibernate;
-
- import java.util.Iterator;
- import java.util.List;
-
- import org.hibernate.Query;
- import org.hibernate.Session;
-
- import junit.framework.TestCase;
-
- public class DMLQueryTest extends TestCase {
-
- public void testQuery1() {
- Session session = null;
- try {
- session = HibernateUtils.getSession();
- session.beginTransaction();
-
-
-
-
- Query updateQuery = session.createQuery("update Student a set a.name=? where a.id in(:ids)");
- updateQuery.setParameter(0, "张三");
- updateQuery.setParameterList("ids", new Object[]{1, 2});
- updateQuery.executeUpdate();
- session.getTransaction().commit();
- }catch(Exception e) {
- e.printStackTrace();
- session.getTransaction().rollback();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
- }