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);
}
}