Hibernate查询 load与get的区别及其它查询测试
一、测试load与get
默认配置下,load将实现懒加载,即只有当对象被使用的时候才发出sql,而get将立刻发出sql
如下:
POJO:
package com.lwf.hibernate;import java.util.Date;public class Doc {private String id;private String name;private Date createDate;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Date getCreateDate() {return createDate;}public void setCreateDate(Date createDate) {this.createDate = createDate;}}
?
配置文件:
<?xml version="1.0"?><!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.lwf.hibernate.Doc"><id name="id"><generator name="code">package com.lwf.hibernate;import java.util.Date;import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class DocSessionTest {public static void main(String[] args) {//testSave();testGet();//testLoad();}public static void commit(Session session){try {session.getTransaction().commit();} catch (HibernateException e) {e.printStackTrace();session.getTransaction().rollback();}}public static void closeSession(Session session){if(session.isOpen()){session.close();}}public static Session getSession(){Session session = null;Configuration config = new Configuration().configure();SessionFactory factory = config.buildSessionFactory();session = factory.openSession();return session;}public static void testSave(){Session session = null;session = getSession();session.beginTransaction();Doc doc = new Doc();session.save(doc);doc.setName("gdgdgd");doc.setCreateDate(new Date());session.flush();commit(session);closeSession(session);}public static void testGet(){Session session = null;session = getSession();session.beginTransaction();Doc doc = (Doc)session.get(Doc.class, "402880f62861314e0128613157060001");//get会立刻发出查询语句,而load则不会立刻发出查询,直到后面有使用doc如doc.setName才进行查询.//System.out.println(doc.getName());//doc.setName("t");session.flush();commit(session);closeSession(session);}public static void testLoad(){Session session = null;session = getSession();session.beginTransaction();//get会立刻发出查询语句,而load则不会立刻发出查询,直到后面有使用doc如doc.setName才进行查询.//load方法实现了lazy即懒模式Doc doc =(Doc)session.load(Doc.class, "40288e81280b1db101280b1db61c0001");//System.out.println(doc.getName());//doc.setName("t");session.flush();commit(session);closeSession(session);}}
?
?
运行代码会发现load与get的不同。
另外,如果没有查询到数据,get会返回null,而load回返回异常
?
二、下例中对HQL的各种情况的查询,删除进行演示
package com.lwf.hibernate;import java.util.List;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class DocSqlTest {public static void main(String[] args) {Configuration cfg = new Configuration().configure();SessionFactory factory = cfg.buildSessionFactory();Session session = factory.openSession();session.beginTransaction();//where子句中使用值传递String sqlStr = "from Doc where name ='we'";getDocList(session,sqlStr);//where子句中使用参数传递,参数通过query.setString(paramName,paramVal);设置sqlStr = "from Doc where name =:name";Object[] object = new Object[2];object[1] = "yeeey";getDocList(session,sqlStr,object);//where子句中使用参数传递,参数通过query.setProperties(object);设置sqlStr = "from Doc where name =:name";Doc doc = new Doc();doc.setName("Lisi1");getDocList(session,sqlStr,doc);//通过sqlStr字符串设定的查询条件进行查询sqlStr = "select id,name from Doc";getDocObject(session,sqlStr);//通过从配置文件中获得查询语句进行查询getDocBynameQuery(session);//get getObjectBynameQuerygetObjectBynameQuery(session);//get getDocBynameQueryParamgetDocBynameQueryParam(session);//只获得一笔记录sqlStr = "from Doc";getSingleData(session,sqlStr);//通过改变查询条件进行查询。sqlStr = "from Doc where lower(name)='df'";getDocList(session,sqlStr);sqlStr = "from Doc where name not in ('we','df')";getDocList(session,sqlStr);sqlStr = "from Doc where name like '_e%'";getDocList(session,sqlStr);//获得总记录数sqlStr = "select count(*) from Doc";Long l = (Long)session.createQuery(sqlStr).uniqueResult();System.out.println(l);//测试group by子名sqlStr = "select name from Doc group by name having name is not null";getDocName(session,sqlStr);session.getTransaction().commit();session.flush();if(session.isOpen()){session.close();}//测试删除表记录session = factory.openSession();session.beginTransaction();Query query = session.createQuery("delete from Doc");query.executeUpdate();session.getTransaction().commit();if(session.isOpen()){session.close();}}public static void getSingleData(Session session,String sqlStr){Query query = session.createQuery(sqlStr);query.setFirstResult(0);query.setMaxResults(1);Object object = query.uniqueResult();System.out.println(((Doc)object).getId() + " : " + ((Doc)object).getName());}public static void getDocBynameQueryParam(Session session){Query query = session.getNamedQuery("HqlNameAllParam");query.setString("name", "we");List list = query.list();for (int i = 0; i < list.size(); i++) {Doc doc = (Doc)list.get(i);String id = doc.getId();String name = doc.getName();System.out.println(id +" : "+ name);}}public static void getDocBynameQuery(Session session){Query query = session.getNamedQuery("HqlNameAll");List list = query.list();for (int i = 0; i < list.size(); i++) {Doc doc = (Doc)list.get(i);String id = doc.getId();String name = doc.getName();System.out.println(id +" : "+ name);}}public static void getObjectBynameQuery(Session session){Query query = session.getNamedQuery("HqlName");List list = query.list();for (int i = 0; i < list.size(); i++) {Object[] object = (Object[])list.get(i);System.out.println(object[0] + " <:> " + object[1]);}}public static void getDocList(Session session,String sqlStr,Object object){Query query = session.createQuery(sqlStr);query.setProperties(object);List list = query.list();for (int i = 0; i < list.size(); i++) {Doc doc = (Doc)list.get(i);String id = doc.getId();String name = doc.getName();System.out.println(id +" : "+ name);}}public static void getDocList(Session session,String sqlStr, Object[] parameter){Query query = session.createQuery(sqlStr);query.setString("name", (String)parameter[1]);List list = query.list();for (int i = 0; i < list.size(); i++) {Doc doc = (Doc)list.get(i);String id = doc.getId();String name = doc.getName();System.out.println(id +" : "+ name);}}public static void getDocList(Session session,String sqlStr){Query query = session.createQuery(sqlStr);List list = query.list();for (int i = 0; i < list.size(); i++) {Doc doc = (Doc)list.get(i);String id = doc.getId();String name = doc.getName();System.out.println(id +" : "+ name);}}public static void getDocObject(Session session,String sqlStr){Query query = session.createQuery(sqlStr);List list = query.list();for (int i = 0; i < list.size(); i++) {Object[] object = (Object[])list.get(i);System.out.println(object[0] + " <:> " + object[1]);}}public static void getDocName(Session session,String sqlStr){Query query = session.createQuery(sqlStr);List list = query.list();for (int i = 0; i < list.size(); i++) {String name = (String)list.get(i);System.out.println(name);}}}
?