1. 程式人生 > >Hibernate中的查詢策略

Hibernate中的查詢策略

 Hibernate的基本策略查詢。

建立pojo.User 類

package com.pojo;

import java.util.HashSet;
import java.util.Set;

/**
 * User entity. @author MyEclipse Persistence Tools
 */

public class User implements java.io.Serializable {

	// Fields

	private String username;
	private String password;
	private Set goodses = new HashSet(0);

	// Constructors

	/** default constructor */
	public User() {
	}
	
	
	/** minimal constructor */
	public User(String username, String password) {
		this.username = username;
		this.password = password;
	}

	/** full constructor */
	public User(String username, String password, Set goodses) {
		this.username = username;
		this.password = password;
		this.goodses = goodses;
	}

	// Property accessors

	public String getUsername() {
		return this.username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Set getGoodses() {
		return this.goodses;
	}

	public void setGoodses(Set goodses) {
		this.goodses = goodses;
	}

}

一、投影查詢

@org.junit.Test
	public void testTouying(){
		Session session = HibernateSessionFactory.getSession();
		String hql = "select username ,password from User" ;
		Query query = session.createQuery(hql);
		List<Object[]> list = query.list();
			for (Object[] objects : list) {
				System.out.println(objects[0]+"\t"+objects[1]);
			}
	}
	@org.junit.Test
	public void testTouying2(){
		Session session = HibernateSessionFactory.getSession();
		String hql = "select new User(username,password) from User" ;
		Query query = session.createQuery(hql);
		List<User> list = query.list();
		for (User user : list) {
			System.out.println(user.getUsername()+"\t"+user.getPassword());
		}
	}

上面有兩種投影查詢。一般使用第二種。

二、條件查詢

@org.junit.Test
	public void testFZ(){
		Session session = HibernateSessionFactory.getSession() ;
		String hql = "from User where username = ?" ;
		Query query = session.createQuery(hql) ;
		query.setParameter(0, "劉強") ;
		List<User> list = query.list() ;
		for (User user : list) {
			System.out.println(user.getUsername()+"\t"+user.getPassword()) ;
		}
	}

條件查詢和sql 類似,但是hql面向的是物件。

三、分頁查詢

@org.junit.Test
	public void testFenye(){
		Session session = HibernateSessionFactory.getSession();
		String hql = "from User" ;
		Query createQuery = session.createQuery(hql);
		createQuery.setFirstResult(0).setMaxResults(3);
		List<User> list = createQuery.list();
		for (User user : list) {
			System.out.println(user.getUsername());
		}
	}

由於Hibernate不能通用得到頁碼和數量,所以Hibernate封裝了mysql中的limit,setFirstResult()從那個開始,setMaxResults()得到多少個結果。

四、迫切查詢(多表查詢)

@org.junit.Test
	public void testPoQie(){
		Session session = HibernateSessionFactory.getSession();
		String hql = "from User u join u.goodses" ;
		Query query = session.createQuery(hql);
		List<Object[]> list = query.list() ;
		for (Object[] objects : list) {
			System.out.println(objects[0]+"\t"+objects[1]);
		}
	}
	@org.junit.Test
	public void testPoQie2(){
		Session session = HibernateSessionFactory.getSession();
		String hql  = "from User u join fetch  u.goodses" ;
		Query query = session.createQuery(hql);
		List<User> list = query.list() ;
		for (User user : list) {
			System.out.println(user.getUsername()+"\t"+user.getPassword()+"\t"+user.getGoodses());
		}
 	}

 通常用第二種。