hibernate HQL 分页 关联查询(一对多单向,多对一 双向,多对多)

HQL的 分页

1.首先我们准备好实体类:

package cn.happy.hibernate04pagelist;

import cn.happy.hibernate03hql.conEmp;

import java.util.Date;

/**
 * Created by linlin on 2017/9/25.
 */
public class Emp {
    private Long empno;
    private String empname;
    private String job;
    private Long mgr;
    private Date hiredate;
    private Long sal;
    private Long comm;
    private Byte deptno;



    public Long getEmpno() {
        return empno;
    }

    public void setEmpno(Long empno) {
        this.empno = empno;
    }

    public String getEmpname() {
        return empname;
    }

    public void setEmpname(String empname) {
        this.empname = empname;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public Long getMgr() {
        return mgr;
    }

    public void setMgr(Long mgr) {
        this.mgr = mgr;
    }

    public Date getHiredate() {
        return hiredate;
    }

    public void setHiredate(Date hiredate) {
        this.hiredate = hiredate;
    }

    public Long getSal() {
        return sal;
    }

    public void setSal(Long sal) {
        this.sal = sal;
    }

    public Long getComm() {
        return comm;
    }

    public void setComm(Long comm) {
        this.comm = comm;
    }

    public Byte getDeptno() {
        return deptno;
    }

    public void setDeptno(Byte deptno) {
        this.deptno = deptno;
    }
}


然后是小配置:




    
        
            
        
        
        
        
        
        
        
        
    
        


大配置:





   

    
        oracle.jdbc.OracleDriver
        jdbc:oracle:thin:@localhost:1521:orcl
        sll
        sll
        
        org.hibernate.dialect.Oracle10gDialect

thread
        true


        true
     
     
        
        

      
        
    


测试类:

    Configuration cfg;
    Transaction tx;
    Session session;
    SessionFactory factory;
    @Before
    public void myBefore(){
        //创建配置对象
        cfg = new Configuration().configure("Hibernate.cfg.xml");
        //根据配置对象创建SessionFactory
        factory = cfg.buildSessionFactory();
        //根据SessionFactory创建Session
        session= factory.openSession();
    /*    Session session1= HibernateUtil.getSession();
        System.out.println(session);
        System.out.println(session1);*/
        //在Session创建后开启事务
        tx = session.beginTransaction();
    }

//分页
    @Test
    public void testl8() {

        String hql="from Emp";
        Query query = session.createQuery(hql);
            int pageindex=2;
            int pagesize=3;
            query.setFirstResult((pageindex-1)*pagesize);
            query.setMaxResults(pagesize);
        List list=query.list();
        for (Emp item:list){
            System.out.println(item.getEmpname());
        }
    }


一对一单向查询:

实体 的关联:封装*****

hibernate HQL 分页 关联查询(一对多单向,多对一 双向,多对多)_第1张图片hibernate HQL 分页 关联查询(一对多单向,多对一 双向,多对多)_第2张图片


并进行封装(get  set)

然后是关联小配置




    
        
            
        
        
    


        




    
        
            
        
        

        
    


        


大配置 如上述 一样 添加一个mapping

测试类:

//多对一
    @Test
    public void testmanyToOne(){
        cn.happy.hibernate06mapping.Emp emp=session.get(cn.happy.hibernate06mapping.Emp.class,1);
        System.out.println(emp.getDept().getDeptname());
    }



多对多 查询 带有中间表:变像的 2个多对一

hibernate HQL 分页 关联查询(一对多单向,多对一 双向,多对多)_第3张图片

小配置:




    
        
           
        
        
        
        
            
            
        
    






    
        
           
        
        
    



测试类;

package cn.happy.test;


import cn.happy.hibernate08save.Dept;
import cn.happy.hibernate08save.Emp;
import cn.happy.hibernate09manytomany.Employee;
import cn.happy.hibernate09manytomany.Project;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

/**
 * Created by linlin on 2017/9/22.
 */
public class test1006_02 {
    Configuration cfg;
    Transaction tx;
    Session session;
    SessionFactory factory;

    @Before
    public void myBefore() {
        //创建配置对象
        cfg = new Configuration().configure("Hibernate.cfg.xml");
        //根据配置对象创建SessionFactory
        factory = cfg.buildSessionFactory();
        //根据SessionFactory创建Session
        session = factory.openSession();
        //在Session创建后开启事务
        tx = session.beginTransaction();
    }


    @Test
    public void test03() {
        Employee emp=session.get(Employee.class,1);
        System.out.println(emp.getEmpname());
        System.out.println("============");
        for(Project pro:emp.getProjects()){
            System.out.println(pro.getProname());
        }
    }


}



你可能感兴趣的:(hibernate HQL 分页 关联查询(一对多单向,多对一 双向,多对多))