日期:2014-05-16  浏览次数:20571 次

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 class="uuid"></generator>
		</id>
		<property name="name"></property>
		<property name="createDate"></property>
	</class>
	<query name="HqlName">
		<![CDATA[select id,name from Doc]]>
	</query>
	
	<query name="HqlNameAll">
		<![CDATA[from Doc order by name desc]]>
	</query>
	
	<query name="HqlNameAllParam">
		<![CDATA[from Doc where name=:name]]>
	</query>
	
	 
</hibernate-mapping>	


测试代码:

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 fa