Last Record of one to many relation Hibernate Criteria

家住魔仙堡 提交于 2019-12-11 10:47:28

问题


I need to get last record and the main record of one-to-many relation with Hibernate criteria. The Pseudo-Sql show the query I want to execute

Table1(Master)
Table2(Details)

Select *  
      from Table1 tab1, Table2  tab2 
              where tab2.tab1id == tab1.id 
              and tab2.date == (  select Max(date) 
                                    from table2 where table2.tab1id == tab1.id)

回答1:


I did it!!!

DetachedCriteria maxFecha = DetachedCriteria
.forClass(CambiosEstado.class, "cambio")
.setProjection(Projections.max("fecha"))
.add(Property.forName("cambio.practicasEst").eqProperty("cambio2.practicasEst"));  
Criteria criteria = this.getSession().createCriteria(
PracticasEst.class);
Criteria estadosCriteria = criteria.createCriteria(
"cambiosEstados", "cambio2");
estadosCriteria.add(Restrictions.eq("estados", estados));
estadosCriteria.add(Property.forName("fecha").eq(maxFecha));
return criteria.list();

the Master Class is PracticasEst that has a collection of CambiosEstado. the query take the PracticaEst and the last CambiosEstado. (Fecha is Date in spanish).



来源:https://stackoverflow.com/questions/7890062/last-record-of-one-to-many-relation-hibernate-criteria

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