Hibernate学习笔记----HQL(1)

Department.java

package cn.limbo.hibernate.entities;

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

public class Department {
	
	private Integer id;
	private String name;
	
	private Set<Employee> employees = new HashSet<>();

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

	public String getName() {
		return name;
	}

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

	public Set<Employee> getEmployees() {
		return employees;
	}

	public void setEmployees(Set<Employee> employees) {
		this.employees = employees;
	}

	
}
Employee.java

package cn.limbo.hibernate.entities;

public class Employee {
	
	private Integer id;
	private String name;
	private String email;
	private double salary;
	
	private Department department;

	public Integer getId() {
		return id;
	}

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

	public String getName() {
		return name;
	}

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

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public double getSalary() {
		return salary;
	}

	public void setSalary(double salary) {
		this.salary = salary;
	}

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}
	

}

Department.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2016-7-24 23:26:24 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
    <class name="cn.limbo.hibernate.entities.Department" table="DEPARTMENTS">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="native" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
        <set name="employees" table="EMPLOYEES" inverse="true" lazy="true">
            <key>
                <column name="DEPARTMENT_ID" />
            </key>
            <one-to-many class="cn.limbo.hibernate.entities.Employee" />
        </set>
    </class>
</hibernate-mapping>

Employee.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2016-7-24 23:26:24 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
    <class name="cn.limbo.hibernate.entities.Employee" table="EMPLOYEES">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="native" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
        <property name="email" type="java.lang.String">
            <column name="EMAIL" />
        </property>
        <property name="salary" type="double">
            <column name="SALARY" />
        </property>
        <many-to-one name="department" class="cn.limbo.hibernate.entities.Department" column="DEPARTMENT_ID">
        </many-to-one>
    </class>
</hibernate-mapping>
Junit.java

package cn.limbo.hibernate.entities;

import java.util.List;

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

public class Junit {
	
	private SessionFactory sessionFactory;
	private Session session;
	private Transaction transaction;
	
	@Before
	public void init()
	{
		Configuration configuration = new Configuration().configure();
		ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().
				applySettings(configuration.getProperties()).build();
		sessionFactory = configuration.buildSessionFactory(serviceRegistry);
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
	}
	
	
	@After
	public void destroy()
	{
		transaction.commit();
		session.close();
		sessionFactory.close();
	}

	@Test
	public void testHQL()
	{
		//1.创建Query对象
		//基于位置的的参数
		String hql = "FROM Employee e WHERE e.salary > ? AND e.email LIKE ? AND e.department = ?  ORDER BY e.salary";
		Query query = session.createQuery(hql);
		//2.绑定参数
		//Query对象调用setXxx支持方法链的风格
		Department department = new Department();
		department.setId(13);
		query.setDouble(0, 600).setString(1, "%3%").setEntity(2, department);
		//3.执行查询
		List<Employee>employees = query.list();
		System.out.println(employees);
	}
	
	@Test
	public void testNamedParameter()
	{
		//1.创建Query对象
		//基于命名的参数
		String hql = "FROM Employee e WHERE e.salary > :sal AND e.email LIKE :email";
		Query query = session.createQuery(hql);
		//2.绑定参数
		//Query对象调用setXxx支持方法链的风格
		query.setDouble("sal", 600).setString("email", "%3%");
		//3.执行查询
		List<Employee>employees = query.list();
		System.out.println(employees);
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值