Hibernate条件结果映射到类/ Bean

时间:2017-01-15 08:43:41

标签: java hibernate

我想找到第三高薪

结果应为:“Mark”,“Hastings”,Gender.male,60000,“USA”

这是数据

Employee employee1 = new Employee("Mark", "Hastings", Gender.male, 60000,"USA");
Employee employee2 = new Employee("Steve", "Pound", Gender.male, 45000, "USA");
Employee employee3 = new Employee("Ben", "Hoskins", Gender.male, 70000, "USA");
Employee employee4 = new Employee("Philip", "Hastings",Gender.male, 45000, "USA");
Employee employee5 = new Employee("Mary", "Lambeth",Gender.female, 30000, "UK");
Employee employee6 = new Employee("Valarie", "Vikings",Gender.female, 35000,"UK");
Employee employee7 = new Employee("John", "Stanmore",Gender.male, 80000, "UK");
Employee employee8 = new Employee("Ben", "Hoskins", Gender.male,70000, "USA");
Employee employee9 = new Employee("Geir", "Ruine", Gender.male, 70000, "USA");

这是我的hibernate代码:

final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();

        try {

            SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();

            Session session = sessionFactory.openSession();
            session.beginTransaction();

            ProjectionList projectionList = Projections.projectionList();
            projectionList.add(Projections.property("salary"));
            projectionList.add(Projections.property("firstName"));
            projectionList.add(Projections.property("lastName"));
            projectionList.add(Projections.property("Country"));
            projectionList.add(Projections.property("gender"));

            Criteria criteria = session.createCriteria(Employee.class);
            criteria.setFirstResult(3);
            criteria.setMaxResults(1);
            criteria.setProjection(Projections.distinct(projectionList));
            criteria.addOrder(org.hibernate.criterion.Order.desc("salary"));
        //  criteria.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
            criteria.setResultTransformer(Transformers.aliasToBean(Employee.class));
            Employee emp = (Employee) criteria.list().get(0);
            System.out.println(emp.getCountry());

            session.getTransaction().commit();
            session.flush();
            session.clear();

            session.close();

        } catch (Exception e) {
            e.printStackTrace();
            StandardServiceRegistryBuilder.destroy(registry);
        }
    }

这给了我带有空值的员工对象。

但是 当我执行Transformers.ALIAS_TO_ENTITY_MAP or Transformers.To_List时,我在表单hashmap和List of String中获取结果。

建议我如何转换为bean即Employee对象。

1 个答案:

答案 0 :(得分:0)

        Criteria criteria = session.createCriteria(Employee.class);
        criteria.addOrder(Order.desc("salary"));
        criteria.setFirstResult(3);// 0 based index; set 0 for 1st hishest salary,1 for second highest salary
        criteria.setMaxResults(1);
        criteria.setProjection(Projections.distinct(Projections.property("salary")));
        int hishest = (Integer) criteria.uniqueResult();