我是Hibernate的新手,我正在使用Hibernate开发一个应用程序。具体来说,我正在尝试使用Hibernate实现以下SQL Query:
select * from ifcitsv2013 where timestamp between startDate and finishDate and id = '50'
在Hibernate中,我有以下代码用于查询:
DetachedCriteria dc = DetachedCriteria.forClass(IfcitsSensorphistory.class).add(Property.forName("id").eq(sDevice) );
List<Ifcitsv2013> readings = session.createCriteria(Ifcitsv2013.class).add(Property.forName("ifcitsSensorphistory").gt(dc)).list();
映射类Ifcitsv2013
如下:
public class Ifcitsv2013 implements Serializable {
private static final long serialVersionUID = 1L;
private long readingid;
private IfcitsSensorphistory ifcitsSensorphistory;
private Timestamp timestamp;
private String value;
private String direction;
private String quality;
private String status;
,班级IfcitsSensorphistory
是:
public class IfcitsSensorphistory {
private String id;
private String description;
private Timestamp starttime;
private Timestamp endtime;
private String timeseriesdatatype;
private String dataorigin;
private String usagedefineddataorigin;
private String unit;
private String value;
当我执行查询时,我得到了Exception
:
Exception in thread "main" java.lang.NullPointerException
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getProjectedTypes(CriteriaQueryTranslator.java:362)
at org.hibernate.criterion.SubqueryExpression.createAndSetInnerQuery(SubqueryExpression.java:153)
at org.hibernate.criterion.SubqueryExpression.toSqlString(SubqueryExpression.java:69)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:380)
at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:114)
at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:83)
at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:92)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1687)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)
at com.cartif.dwhconnector.manager.DatabaseCommunicator.retrieveHistoricalData(DatabaseCommunicator.java:242)
at com.cartif.dwhconnector.manager.DWHManager.retrieveData(DWHManager.java:62)
at unittest.DWHConnectorTester.main(DWHConnectorTester.java:31)
有人可以帮我开发正确的查询吗?
答案 0 :(得分:0)
确保您有一个开放的session
。
Query q = session.CreateQuery("select * from Ifcitsv2013 left join IfcitsSensorphistory as h where timestamp between :startDate and :finishDate and h.id = 50");
q.setDate("startDate", startDate);
q.setDate("finishDate", finishDate);
List result = q.list();