Hibernate 基于元素列表的标准和预测
我想创建一个与以下SQL查询具有相同行为的条件查询:Hibernate 基于元素列表的标准和预测,hibernate,criteria,Hibernate,Criteria,我想创建一个与以下SQL查询具有相同行为的条件查询: SELECT Count(*) FROM Datagathering_respuestas, Datagathering WHERE Datagathering.document = 4 AND Datagathering.id_datagathering = datagathering_respuestas.id_datagathering GROUP BY respuesta; 它围绕着以下类别: public class DataGa
SELECT Count(*)
FROM Datagathering_respuestas, Datagathering
WHERE Datagathering.document = 4 AND Datagathering.id_datagathering = datagathering_respuestas.id_datagathering
GROUP BY respuesta;
它围绕着以下类别:
public class DataGathering {
private int id;
private Usuario user;
private Date fecha;
private int individual;
private DataDocument document;
private List<String> respuestas;
它的行为与上面的SQL查询相同,但是没有GROUPBY元素,所以我认为我已经接近了。问题是我不知道如何引用respuesta中的值
你知道我怎样才能完成我的查询吗?我们将非常感谢您的帮助。我通过以下方式做到了这一点:
da = s.createCriteria(DataGathering.class)
.add(Restrictions.eq("document.id", id))
.createCriteria("respuestas","resp")
.setProjection(Projections.projectionList()
.add(Projections.groupProperty("resp.elements").as("resp"))
.add(Projections.count("resp.elements").as("amount")))
.setResultTransformer(Transformers.aliasToBean(EncuestaHelper.class))
.list();
诀窍在于,Criteria使用关键元素来引用普通元素集合。然后,为projections提供与特殊类的属性匹配的别名,您可以轻松地创建该类的列表,以便在报告中使用它
感谢所有试图帮助你的人
Criteria c = s.createCriteria(DataGathering.class);
c.add(Restrictions.eq("document.id", id));
c.createCriteria("respuestas","resp");
c.setProjection(Projections.projectionList().add(Projections.rowCount()));
da = s.createCriteria(DataGathering.class)
.add(Restrictions.eq("document.id", id))
.createCriteria("respuestas","resp")
.setProjection(Projections.projectionList()
.add(Projections.groupProperty("resp.elements").as("resp"))
.add(Projections.count("resp.elements").as("amount")))
.setResultTransformer(Transformers.aliasToBean(EncuestaHelper.class))
.list();