Java hibernate HQL查询(全部查询,参数查询,模糊查询,投影查询)

Student.java类

package cn.com.edu;


import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

@Entity
public class Student {
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private int id;
	private String name;
	private String sex;
	@ManyToOne(cascade={CascadeType.MERGE},fetch=FetchType.EAGER)
	private Project pro;
	public Student() {
		super();
	}
	public Student(String name, String sex) {
		super();
		this.name = name;
		this.sex = sex;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Project getPro() {
		return pro;
	}
	public void setPro(Project pro) {
		this.pro = pro;
	}
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", sex=" + sex + "]";
	}

}

Project.java类

package cn.com.edu;

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

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;



@Entity
public class Project {
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private int id;
	private String name;
	@OneToMany(cascade={CascadeType.ALL},mappedBy="pro",fetch=FetchType.LAZY)
	private Set students=new HashSet();
	public Project() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Project(String name) {
		super();
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Set getStudents() {
		return students;
	}
	public void setStudents(Set students) {
		this.students = students;
	}
}

hibernate.cfg.xml







    
        org.hibernate.dialect.MySQLDialect
        jdbc:mysql:///test?characterEncoding=utf8
        root
        root
        com.mysql.jdbc.Driver
        firstTest
    	true
    	true

    	
    	
    	
    

hiberTest.java  测试类

package cn.com.edu;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;


public class hiberTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Configuration con=new Configuration().configure();//读取解析配置文件
		SessionFactory sf=con.buildSessionFactory(); //创建工厂,读取映射信息
		
		Session session=sf.openSession();//打开Session

		Transaction tr=session.beginTransaction(); //创建并开始一个事务
//		function1(session);//全部查询
//		function2(session);//参数绑定
//		function3(session);//模糊查询
		function4(session);//查询某些属性 投影查询
		tr.commit();
		session.close();
		sf.close();
	}

	private static void function4(Session session) {
		String hql="select s.name,s.sex from Student s";
		Query query =session.createQuery(hql);
		List list=query.list();
		for(Object[] student:list)
		System.out.println(student[0]+"-----"+student[1]);
	}

	private static void function3(Session session) {
		String hql="from Student s where s.name like ? and sex=?";
		Query query=session.createQuery(hql);
		query.setString(0, "%s%").setString(1, "男");
		List list =query.list();
		for(Student student:list){
			System.out.println(student);
		}
		
	}

	private static void function2(Session session) {
		String hql="from Student s where s.name=?";
		Query query = session.createQuery(hql);
		query.setString(0, "w");//0->第一个问号,"w"->问号的值
		List list=query.list();
		for(Student student:list){
			System.out.println(student);
		}
		
	}

	private static void function1(Session session) {
		String hql="from Student";
		Query query =session.createQuery(hql);
		List student=query.list();
		for(Student student2:student)
		System.out.println(student2);
	}
	
}

 

你可能感兴趣的:(Java学习)