Hibernate之hql中关联查询与with的用法

Hibernate以面向对象的方式提供我们查询,但是在很多复杂的多表查询中,hql或者sql方式查询就显得尤为重要!

废话不多说,直接上代码

实体类:Student

package com.tz.hql;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "tz_student")
public class Student implements java.io.Serializable{

	private static final long serialVersionUID = 1L;
	private Integer id;
	private String name;
	private String password;
	private ClassRoom classRoom;
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Column(name="name")
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name="password")
	public String getPassword() {
		return password;
	}

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

	@ManyToOne(targetEntity=ClassRoom.class,fetch=FetchType.LAZY)
	public ClassRoom getClassRoom() {
		return classRoom;
	}

	public void setClassRoom(ClassRoom classRoom) {
		this.classRoom = classRoom;
	}


	
}
实体:ClassRoom

package com.tz.hql;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Entity;

@Entity
@Table(name = "tz_class_room")
public class ClassRoom implements java.io.Serializable {

	private static final long serialVersionUID = 1L;
	private Integer id;
	private String name;
	private String departmentName;
	private List students = new ArrayList(0);

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Column(name = "name")
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "department_name")
	public String getDepartmentName() {
		return departmentName;
	}

	public void setDepartmentName(String departmentName) {
		this.departmentName = departmentName;
	}

	@OneToMany(targetEntity=Student.class,mappedBy="classRoom")
	public List getStudents() {
		return students;
	}

	public void setStudents(List students) {
		this.students = students;
	}

}

下面编写测试类:

package com.tz.hql;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.junit.Test;

import com.sun.javafx.collections.MappingChange.Map;

public class HqlTest {

	@Test
	public void handle() {
		Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
		ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
				.applySettings(configuration.getProperties()).build();
		SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
		Session session = sessionFactory.openSession();
//		String hql = "from Student";
//		Query query = session.createQuery(hql);
//		List list = query.list();
//		for(Student student:list){
//			System.out.println(student.getName());
//		}
		/*String sql = "select s.id as id,s.name as name,s.password as password from Student s";
		Query query = session.createQuery(sql);
		List list = query.list();
		System.out.println(list);*/
		
		String hql = "from Student s left join s.classRoom c with (c.id=1)";
		Query query = session.createQuery(hql);
		List list = query.list();
		System.out.println(list);
		
		/*String sql = "select new Map(s.id as id,s.name as name,s.password as password ) from Student s left  where  s.id=1 and s.name='keke'";
		Query query = session.createQuery(sql);
		List> list = query.list();
		System.out.println(list);*/
		
		
	}
}

特别注意的是:关联查询的条件用with,其他查询的条件则用where,如测试类中最下面的返回List方式的查询。




你可能感兴趣的:(Hibernate之hql中关联查询与with的用法)