简体   繁体   English

休眠标准集合的最新日期字段

[英]Hibernate Criteria most recent date field of Collection

I have Laboratories with a collection of states, here are my simplified Classes: 我的实验室有一组州,这是我的简化班级:

    LabState {
        private Integer id;
        private State state;
        private Date date;
        private String comments;
    }

    State{
        private Integer id;
        private String name;

    }

    Lab{
        private Integer id;
        private List<LabState> states = new ArrayList<LabState>();
        private Date date;
        private String name
    }

I want to get via Hibernate Criteria the Last State from a specific laboratory. 我想通过休眠标准从特定实验室获得“最后状态”。 in mySql this work fine: 在mySql中这项工作很好:

SELECT ls.* FROM labs_states les INNER JOIN
    ( SELECT idLab, idLabState, MAX(date) AS lastDate FROM labs_states GROUP BY idLab)
        groupedLabs ON les.idLab = groupedLabs.idLab  AND les.date = groupedLabs.lastDate

Edit: SearchMethod: 编辑:SearchMethod:

   public List<Labs> search(LabSearch labSearch) {

            SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
            Session session = sessionFactory.openSession();

            Criteria criteria = session.createCriteria(Lab.class);

            criteria.add(Restrictions.like("name", "%" + labSearch.getName() + "%"));

            if(labSearch.getState() != null){
                criteria.createCriteria("states").add(Restrictions.eq("state", labSearch.getState())).addOrder(Order.asc("date")).list().get(0);


            return (List<Lab>) criteria.list();
}

I don't nkow how to do this with Criteria, or if there is any other way to do 我不知道如何使用Criteria来执行此操作,或者是否还有其他方法可以执行此操作

thanks 谢谢

to get the last state of the given lab 得到给定实验室的最后状态

Criteria criteria = session.createCriteria(Lab.class)
    .add(Restrictions.like("name", "%" + labSearch.getName() + "%"));
    .createAlias("states", "labstate")
    .createAlias("labstate", "state")
    .addOrder(Order.desc("date"))
    .setProjection(Projections.list()
        .add(Projections.property("labstate.id"), "id")
        .add(Projections.property("labstate.date"), "date")
        .add(Projections.property("labstate.comments"), "comments")
        .add(Projections.property("state.name"), "state"))
    .setResultTransformer(Transformers.aliasToBean<LabstateDto>())
    .setMaxResults(1);

return (List<LabstateDto>)criteria.list();

if you want more than one lab with its last state returned you'll need another query which is not that easy because you have the backreference from Labstate to Lab ony in db and not in class. 如果您希望多个实验室返回最后一个状态,则需要另一个查询,因为它在db中而不是在类中具有从Labstate到Lab ony的向后引用,所以这并不是那么容易。

Update: in response to your comment an alternative query 更新:根据您的评论,替代查询

Criteria criteria = session.createCriteria(Lab.class, "lab")
    .add(Restrictions.like("name", "%" + labSearch.getName() + "%"));
    .createAlias("states", "labstate")
    .add(Subqueries.proptertyEq("labstate.Id", DetachedCriteria.for(Lab.class)
        .add(Restrictions.propertyEq("id", "lab.id"));
        .createAlias("states", "lstate")
        .createAlias("labstate.state", "state")
        .add(Restrictions.eq("name", state));
        .addOrder(Order.desc("labstate.date"))
        .setProjection(Projection.property("labstate.Id"))
        .setMaxResults(1)
     );

return (List<Lab>)criteria.list();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM