Department:对应一的一方
package zhang.hibernate.entities;
import java.util.HashSet;
import java.util.Set;
public class Department {
private Integer departmentId;
private String departmentName;
private Set<Employee> employees=new HashSet<>();
public Integer getDepartmentId() {
return departmentId;
}
public void setDepartmentId(Integer departmentId) {
this.departmentId = departmentId;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
public Set<Employee> getEmployees() {
return employees;
}
public void setEmployees(Set<Employee> employees) {
this.employees = employees;
}
public Department(String departmentName, Set<Employee> employees) {
super();
this.departmentName = departmentName;
this.employees = employees;
}
@Override
public String toString() {
return "Department [departmentId=" + departmentId + ", departmentName=" + departmentName + "]\n";
}
public Department() {
// TODO Auto-generated constructor stub
}
}
Employee对应多的一方
package zhang.hibernate.entities;
public class Employee {
private Integer id;
private String name;
private String email;
private String phoneNumber;
private float 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 String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public Employee(Integer id, String name, String email) {
super();
this.id = id;
this.name = name;
this.email = email;
}
public Employee(Integer id, String name, String email, String phoneNumber) {
super();
this.id = id;
this.name = name;
this.email = email;
this.phoneNumber = phoneNumber;
}
public Employee(String name, String email, String phoneNumber, float salary, Department department) {
super();
this.name = name;
this.email = email;
this.phoneNumber = phoneNumber;
this.salary = salary;
this.department = department;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", email=" + email + ", phoneNumber=" + phoneNumber
+ ", salary=" + salary + "]\n";
}
public Employee() {
// TODO Auto-generated constructor stub
}
}
employee和department对应的映射文件
department.hbm.xm文件:
<?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 2019-5-6 16:51:34 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="zhang.hibernate.entities.Department" table="ZYK_DEPARTMENTS">
<id name="departmentId" type="java.lang.Integer">
<column name="DEPARTMENTID" />
<generator class="native" />
</id>
<property name="departmentName" type="java.lang.String">
<column name="DEPARTMENTNAME" />
</property>
<set name="employees" table="ZYK_EMPLOYEES" inverse="true" lazy="true">
<key>
<column name="DEPARTMENTID" />
</key>
<one-to-many class="zhang.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 2019-5-6 16:51:34 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="zhang.hibernate.entities.Employee" table="ZYK_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="phoneNumber" type="java.lang.String">
<column name="PHONENUMBER" />
</property>
<property name="salary" type="float">
<column name="SALARY" />
</property>
<many-to-one name="department" class="zhang.hibernate.entities.Department" column="DEPARTMENTID" >
</many-to-one>
</class>
<query name="zhangyukangquery"><![CDATA[select id,name,email from Employee e where e.department=:dept]]></query>
<!--配置命名查询 -->
<query name="QueryWithSalary"><![CDATA[ FROM Employee e where e.salary>:minSal and e.salary<:maxSal order by e.salary]]></query>
</hibernate-mapping>**
QBC和HQL查询的测试类:
package zhang.hibernate.test;
import java.util.Arrays;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Conjunction;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import zhang.hibernate.entities.Department;
import zhang.hibernate.entities.Employee;
public class Test2 {
private SessionFactory SessionFactory = null;
private Session session = null;
private Transaction Transaction = null;
@Before
public void init() {
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
.buildServiceRegistry();
this.SessionFactory = configuration.buildSessionFactory(serviceRegistry);
this.session = SessionFactory.openSession();
Transaction = session.beginTransaction();
}
@After
public void destory() {
Transaction.commit();
session.close();
SessionFactory.close();
}
@Test
public void testhqlmaxandminquery(){
String hql="from Employee e where e.salary>? and e.department=:dept";
Department department=new Department();
department.setDepartmentId(80);
List<Employee> list = session.createQuery(hql)
.setFloat(0, 1000).setEntity("dept", department).list();
System.out.println(list);
}
// hql的分页
@Test
public void testhqlpage() {
int pageNo = 3;
int pageSize = 5;
String hql = "from Employee e where e.salary>? ";
List<Employee> list = session.createQuery(hql).setFloat(0, 5000)
.setFirstResult((pageNo - 1) * pageSize)
.setMaxResults(pageSize).list();
System.out.println(list);
}
@Test
public void buildbeansbyfields() {
String hql = "select new Employee(id,name,email,phoneNumber) "
+ " from Employee e "
+ "where e.department=:dept "
+ " order by salary";
Department department = new Department();
department.setDepartmentId(80);
List<Employee> list = session.createQuery(hql).setEntity("dept", department).list();
System.out.println(list);
}
@Test
public void testandQBC(){
Criteria criteria=session.createCriteria(Employee.class);
Conjunction conjunction=Restrictions.conjunction();
Department department=new Department();
department.setDepartmentId(80);
conjunction.add(Restrictions.eq("department", department));
criteria.add(conjunction);
List<Employee> list = criteria.list();
System.out.println(list);
}
//qbc查询,查询出数据库中的email是SKING的员工的信息
@Test
public void testSKING(){
Criteria criteria=session.createCriteria(Employee.class);
Employee employee= (Employee) criteria.add(Restrictions.eq("email", "SKING")).uniqueResult();
System.out.println(employee);
}
//查询数据库中的工资大于10000元的所有的员工的信息
@Test
public void testqbc2(){
Criteria criteria=session.createCriteria(Employee.class);
criteria.add(Restrictions.gt("salary", 10000f));
List<Employee> list = criteria.list();
System.out.println(list);
}
/**
* QBC查询:查询email中的含有字符串的a的所有员工的详细的信息
*/
@Test
public void testqbc(){
Criteria criteria=session.createCriteria(Employee.class);
criteria.add(Restrictions.like("email", "A",MatchMode.ANYWHERE));
List<Employee> list = criteria.list();
System.out.println(list);
}
//命名查询
@Test
public void testgetnamequery(){
Department department=new Department();
department.setDepartmentId(80);
Query query = session.getNamedQuery("zhangyukangquery").setEntity("dept", department);
List<Object[]> list = query.list();
for(Object[] objs:list){
System.out.println(Arrays.asList(objs));
}
System.out.println(list);
}
/**
* hql分页
*/
@Test
public void testpage2(){
String sql="select id,name,email,salary from Employee e "
+ "where e.salary>? and email like:emailstr";
int pageNo=3;
int pageSize=5;
List<Employee> list = session.createQuery(sql)
.setFloat(0, 5000F).setString("emailstr", "%A%").setFirstResult((pageNo-1)*pageSize)
.setMaxResults(pageSize).list();
for(Employee emp:list){
System.out.println(emp.getEmail()+", "+emp.getName()+", "+emp.getSalary());
}
System.out.println(list);
}
@Test
public void testpage(){
String hql="from Employee e where e.salary>:sal and department=:dept";
Department department=new Department();
department.setDepartmentId(80);
List<Employee> list = session.createQuery(hql).setFloat("sal", 10000f).setEntity("dept", department).list();
System.out.println(list);
}
@Test
public void testleftjoin(){
String hql="FROM Employee";
List<Employee> list = session.createQuery(hql).list();
System.out.println(list);
}
//迫切左外连接
@Test
public void tesleftjoinfetch(){
String hql="from Employee e left join fetch e.department where e.department=:dept";
Department department =new Department();
department.setDepartmentId(80);
List<Employee> list = session.createQuery(hql).setEntity("dept", department).list();
System.out.println(list);
}
/**
* 查询各个部门的最低的工资和最高的工资
*/
@Test
public void testGroupBy() {
String hql = "select department, min(e.salary),max(e.salary) "
+ "from Employee e " + "group by e.department "
+ "having min(e.salary)>5000";
List<Object[]> list = session.createQuery(hql).list();
for (Object[] objs : list) {
System.out.println(Arrays.asList(objs));
}
}
/**
* 获取一个部分的字段
*/
@Test
public void testgetfield() {
String hql = "select id,name,email,phoneNumber from Employee e "
+ "where e.department=:dept "
+ " order by salary";
Department department = new Department();
department.setDepartmentId(80);
Query setFloat = session.createQuery(hql).setEntity("dept", department);
List<Object[]> list = setFloat.list();
for (Object[] objs : list) {
System.out.println(Arrays.asList(objs));
}
}
//获取80号部门的所有的员工的信息
@Test
public void testgetdepartmentequal80() {
String hql = "from Employee e "
+ "where e.department=:department";
Department department = new Department();
department.setDepartmentId(80);
List<Employee> list = session.createQuery(hql).setEntity("department", department).list();
for (Employee employee : list) {
System.out.println(employee.getName() + ", " + employee.getPhoneNumber() + ", " + employee.getEmail());
}
}
// 带条件的hql查询
@Test
public void testhql2() {
String hql = "from Employee e where e.salary>:sal";
Department department = new Department();
department.setDepartmentId(80);
List<Employee> list = session.createQuery(hql).setFloat("sal", 10000).list();
System.out.println(list);
}
// 获取搜有的员工的信息
@Test
public void testhql() {
List<Employee> list = session.createQuery("From Employee").list();
System.out.println(list);
}
@Test
public void testgetcustomer() {
Employee employee = (Employee) session.get(Employee.class, 107);
System.out.println(employee);
}
@Test
public void testhibernateapi() {
SessionFactory sessionFactory = null;
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry =
new ServiceRegistryBuilder()
.applySettings(configuration.getProperties())
.buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Department department = new Department();
department.setDepartmentName("zhangyukangdepartment");
session.save(department);
transaction.wasCommitted();
transaction.commit();
session.close();
sessionFactory.close();
}
}