Hibernate必知必会——映射查询结果集

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010871004/article/details/81436577

映射结果集

有时候我们需要将查询出来的数据,进行映射,映射到当前我们需要的实体类(当前的实体类可能没有进行和数据建立ORM映射)。例如有些时候,我们会把查询出来的结果集映射到Map结果的数据上。在Hibernate中,将结果集映射到实体类上需要使用Transformers类。

转换成List

public class App5 {

    public static void main(String[] args) {
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        session.getTransaction().begin();
        Criteria criteria = session.createCriteria(Employee.class);
        criteria.setResultTransformer(Transformers.TO_LIST);
        List employees = criteria.list();
        for (int i = 0; i < employees.size(); i++) {
            System.out.println(employees.get(i));
        }
        session.getTransaction().commit();
        session.close();
        HibernateUtil.closeSessionFactory();
    }
}

执行结果

[Employee [id=1, firstName=Yogesh, salary=50000.0, department=Department [id=1, deptName=development]]]
[Employee [id=2, firstName=Aarush, salary=35000.0, department=Department [id=1, deptName=development]]]
[Employee [id=4, firstName=Vishal, salary=75000.0, department=Department [id=2, deptName=R&D]]]
[Employee [id=3, firstName=Varsha, salary=30000.0, department=Department [id=3, deptName=UI/UX]]]

转换成Map

public class App5 {

    public static void main(String[] args) {
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        session.getTransaction().begin();
        Criteria criteria = session.createCriteria(Employee.class);
        criteria.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        List employees = criteria.list();
        for (int i = 0; i < employees.size(); i++) {
            Map employeeMap = (Map)employees.get(i);
            Employee employee = (Employee)employeeMap.get(Criteria.ROOT_ALIAS);
            System.out.println(employee);
        }
        session.getTransaction().commit();
        session.close();
        HibernateUtil.closeSessionFactory();
    }
}

执行结果

[{this=Employee [id=1, firstName=Yogesh, salary=50000.0, department=Department [id=1, deptName=development]]}, {this=Employee [id=2, firstName=Aarush, salary=35000.0, department=Department [id=1, deptName=development]]}, {this=Employee [id=4, firstName=Vishal, salary=75000.0, department=Department [id=2, deptName=R&D]]}, {this=Employee [id=3, firstName=Varsha, salary=30000.0, department=Department [id=3, deptName=UI/UX]]}]
Employee [id=1, firstName=Yogesh, salary=50000.0, department=Department [id=1, deptName=development]]
Employee [id=2, firstName=Aarush, salary=35000.0, department=Department [id=1, deptName=development]]
Employee [id=4, firstName=Vishal, salary=75000.0, department=Department [id=2, deptName=R&D]]
Employee [id=3, firstName=Varsha, salary=30000.0, department=Department [id=3, deptName=UI/UX]]

转化成数据传输对象(DTO)

public class App6 {

    public static void main(String[] args) {
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        session.getTransaction().begin();
        Criteria criteria = session.createCriteria(Employee.class);
        criteria.createAlias("department", "_department");
        ProjectionList projectionList = Projections.projectionList();
        projectionList.add(Projections.alias(Projections.property("id"), "empId"));
        projectionList.add(Projections.alias(Projections.property("firstName"), "empFirstName"));
        projectionList.add(Projections.alias(Projections.property("salary"), "empSalary"));
        projectionList.add(Projections.alias(Projections.property("_department.deptName"), "empDeptName"));
        criteria.setProjection(projectionList);
        criteria.setResultTransformer(Transformers.aliasToBean(EmployeeDetail.class));
        List<EmployeeDetail> employeeDetails = criteria.list();
        EmployeeDetail employeeDetail = employeeDetails.get(0);
        System.out.println(employeeDetail);
        session.getTransaction().commit();
        session.close();
        HibernateUtil.closeSessionFactory();
    }
}

执行结果

EmployeeDetail [empId=1, empFirstName=Yogesh, empSalary=50000.0, empDeptName=development]

猜你喜欢

转载自blog.csdn.net/u010871004/article/details/81436577
今日推荐