投影 - 分组 - 分离标准

时间:2017-10-04 10:20:47

标签: hibernate criteria projection hibernate-criteria

public List<Staffing> upcoming(){

         List<Staffing> staffing = new ArrayList<Staffing>();

        Criteria criteria = getCriteria();
        criteria.add(Restrictions.isNotNull("startDate")).add(Restrictions.le("startDate", new Date()));
        criteria.add(Restrictions.isNotNull("endDate")).add(Restrictions.ge("endDate", new Date()));
        criteria.add(Restrictions.eq("softDelete", false));
        criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("user")));
        DetachedCriteria maxDateQuery = DetachedCriteria.forClass(Employee.class);
        maxDateQuery.add(Restrictions.in("user",criteria.list() ));
        maxDateQuery.setProjection(Projections.max("endDate"));

        staffing = criteria.list();
        return staffing;
    }

这里我试图获取人员配置列表,但我得到的对象数组列表不确定有什么问题,但我觉得投影有问题。我尝试的是根据最大endDate获取用户及其他描述。

我有一个类Staffing.java有一个varibale Employee用户(Employee是另一个类)... Date endDate .... Date startDate,Boolean softDelete ..... String projectName ...我想要获取列表对于具有最大endDate的用户的人员配置....像用户可能有许多具有endDate的项目..我想获取每个用户具有最大endDate的列表

这是我的人员配备课程

public class Staffing extends BaseObject {

    /** The Constant serialVersionUID. */
    private static final long serialVersionUID = -3254731507746702368L;

    /** The id. */
    private Long id;

    /** The start date. */
    private Date startDate;

    /** The end date. */
    private Date endDate;

    /** The user. */
    private Employee user;

    /** The project. */
    private Project project;

    /**
     * isDelete for soft delete of staffing.
     */
    private boolean softDelete;


}

这是员工类

public class Employee extends BaseObject implements Serializable,
        Comparable<Employee>, IAuditLog {


    /**
     * id
     */
    private Long id;

    /**
     * username is required field
     */
    private String username;
    /**
     * password is required field
     */
    private String password;

    /**
     * firstname is required field
     */
    private String firstName;

    /**
     * lastname is required field
     */
    private String lastName;

    /**
     * personal Email is required field
     */
    private String email;

    /**
     * primary phone number
     */
    private String phoneNumber;

    /**
     * permanent Address
     */
    private Address permanentAddress;


    /**
     * gender is required field
     */
    private char gender;

    /**
     * Date of birth is required field
     */
    private Date DOB;



}

2 个答案:

答案 0 :(得分:0)

因此,您可以使用此方法获得一名员工的最大endDate

然后您可以使用循环对Employees列表执行相同的操作。

所以现在有我的方法:

public List<Staffing> upcoming(Employee User){

         List<Staffing> staffing = new ArrayList<Staffing>();

        Criteria criteria = getCriteria();
        criteria.add(Restrictions.isNotNull("startDate"))
        .add(Restrictions.le("startDate", new Date()));
        criteria.add(Restrictions.isNotNull("endDate"))
        .add(Restrictions.ge("endDate", new Date()));
        criteria.add(Restrictions.eq("softDelete", false));
        criteria.add(Restrictions.eq("user", User));
        criteria.setProjection(Projections.max("endDate"));
        staffing = criteria.list();
        return staffing;
    } 

然后我们得到所有员工的名单:

public List<Employee> allEmployees(){

             List<Employee> employees= new ArrayList<Employee>();

            Criteria criteria = getCriteria();
              employees= criteria.list();
            Set<Domaine> employeesLLC = new HashSet<>();
        employeesLLC .addAll(employees);
        employees.clear();
        employees.addAll(employeesLLC );
        return employees;
        } 

并将第一种方法应用于每位员工。

答案 1 :(得分:0)

我找到了此查询的解决方案

List<Staffing> staffing = null;
        SimpleDateFormat df = new SimpleDateFormat("yyyy MM dd");
        String dateString = df.format(duration);

        Date date = df.parse(dateString);
        DetachedCriteria maxDate=DetachedCriteria.forClass(Staffing.class);
        maxDate.add(Restrictions.eq("softDelete", false));
        maxDate.setProjection(Projections.projectionList().add(Projections.groupProperty("user").as("user")).add(Projections.max("endDate"),"maxDate"));
        Criteria criteria = getCriteria();

        criteria.add(Restrictions.isNotNull("startDate")).add(Restrictions.le("startDate", new Date()));
        criteria.add(Restrictions.isNotNull("endDate")).add(Restrictions.ge("endDate", new Date())).add(Restrictions.le("endDate", date));
        criteria.add(Restrictions.eq("softDelete", false));
        criteria.add(Subqueries.propertiesIn(new String[]{"user","endDate"}, maxDate));
        staffing = criteria.list();
        return staffing;