Projection-Grouping-Detached Criteria

无人久伴 提交于 2019-12-25 09:37:06

问题


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;
    }

Here I am trying to fetch List of staffing but I am getting List of object array not sure what is wrong but I feel something wrong with projection. What I was trying is to fetch user and its other description based on its maximum endDate.

I have a class Staffing.java having a varibale Employee user (Employee is another class)...Date endDate.... Date startDate, Boolean softDelete .....String projectName... I want to fetch list of staffing for user having maximum endDate.... like user may have many projects which have endDate.. I want to fetch List having maximum endDate per user

This is my staffing class

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;


}

And this is employee class

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;



}

回答1:


So you can use this methode to get the max of endDate for one Employee .

And then you can use a loop to do the same for a list of Employees.

So there is my now approach :

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;
    } 

Then we get the list of all Employees:

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;
        } 

And apply the first methode to each of your employees.




回答2:


I found solution of this query

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;


来源:https://stackoverflow.com/questions/46562466/projection-grouping-detached-criteria

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!