使用关键字字段上的Max()投影和按外键分组的Hibernate条件查询
在Hibernate(版本3.2.5)中将此查询(直接在数据库上工作)表示为条件查询时遇到困难: 到目前为止,这是我想出的不起作用的方法,并抛出一条使用关键字字段上的Max()投影和按外键分组的Hibernate条件查询,hibernate,group-by,projection,foreign-keys,many-to-one,Hibernate,Group By,Projection,Foreign Keys,Many To One,在Hibernate(版本3.2.5)中将此查询(直接在数据库上工作)表示为条件查询时遇到困难: 到目前为止,这是我想出的不起作用的方法,并抛出一条无法解析属性:datetime of:common.entity.FtpStatus错误消息: Criteria crit = s.createCriteria(FtpStatus.class); crit = crit.createAlias("connections", "c"); crit = crit.createAlias("id", "f
无法解析属性:datetime of:common.entity.FtpStatus
错误消息:
Criteria crit = s.createCriteria(FtpStatus.class);
crit = crit.createAlias("connections", "c");
crit = crit.createAlias("id", "f");
ProjectionList proj = Projections.projectionList();
proj = proj.add(Projections.max("f.datetime"));
proj = proj.add(Projections.groupProperty("c.connectionid"));
crit = crit.setProjection(proj);
List<FtpStatus> dtlList = crit.list();
crit=s.createCriteria(FtpStatus.class);
crit=crit.createAlias(“连接”,“c”);
crit=crit.createAlias(“id”,“f”);
ProjectionList proj=Projections.ProjectionList();
项目=项目添加(projects.max(“f.datetime”));
项目=项目添加(Projects.groupProperty(“c.connectionid”);
临界值=临界集投影(项目);
List dtlList=crit.List();
以下是Netbeans 6.8直接从数据库生成的相关参考配置:
FtpStatus.hbm.xml-
<hibernate-mapping>
<class name="common.entity.FtpStatus" table="ftp_status" catalog="common">
<composite-id name="id" class="common.entity.FtpStatusId">
<key-property name="siteid" type="int">
<column name="siteid" />
</key-property>
<key-property name="connectionid" type="int">
<column name="connectionid" />
</key-property>
<key-property name="datetime" type="timestamp">
<column name="datetime" length="19" />
</key-property>
</composite-id>
<many-to-one name="connections" class="common.entity.Connections" update="false" insert="false" fetch="select">
<column name="connectionid" not-null="true" />
</many-to-one>
<many-to-one name="sites" class="common.entity.Sites" update="false" insert="false" fetch="select">
<column name="siteid" not-null="true" />
</many-to-one>
<property name="upInd" type="boolean">
<column name="up_ind" not-null="true" />
</property>
<property name="lastMessage" type="string">
<column name="last_message" length="65535" not-null="true" />
</property>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="common.entity.Connections" table="connections" catalog="common">
<id name="connectionid" type="java.lang.Integer">
<column name="connectionid" />
<generator class="identity" />
</id>
<property name="ip" type="string">
<column name="ip" length="15" not-null="true" />
</property>
<property name="port" type="int">
<column name="port" not-null="true" />
</property>
<property name="user" type="string">
<column name="user" length="8" not-null="true" />
</property>
<property name="password" type="string">
<column name="password" length="50" not-null="true" />
</property>
<set name="ftpStatuses" inverse="true">
<key>
<column name="connectionid" not-null="true" />
</key>
<one-to-many class="common.entity.FtpStatus" />
</set>
</class>
</hibernate-mapping>
Connections.hbm.xml-
<hibernate-mapping>
<class name="common.entity.FtpStatus" table="ftp_status" catalog="common">
<composite-id name="id" class="common.entity.FtpStatusId">
<key-property name="siteid" type="int">
<column name="siteid" />
</key-property>
<key-property name="connectionid" type="int">
<column name="connectionid" />
</key-property>
<key-property name="datetime" type="timestamp">
<column name="datetime" length="19" />
</key-property>
</composite-id>
<many-to-one name="connections" class="common.entity.Connections" update="false" insert="false" fetch="select">
<column name="connectionid" not-null="true" />
</many-to-one>
<many-to-one name="sites" class="common.entity.Sites" update="false" insert="false" fetch="select">
<column name="siteid" not-null="true" />
</many-to-one>
<property name="upInd" type="boolean">
<column name="up_ind" not-null="true" />
</property>
<property name="lastMessage" type="string">
<column name="last_message" length="65535" not-null="true" />
</property>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="common.entity.Connections" table="connections" catalog="common">
<id name="connectionid" type="java.lang.Integer">
<column name="connectionid" />
<generator class="identity" />
</id>
<property name="ip" type="string">
<column name="ip" length="15" not-null="true" />
</property>
<property name="port" type="int">
<column name="port" not-null="true" />
</property>
<property name="user" type="string">
<column name="user" length="8" not-null="true" />
</property>
<property name="password" type="string">
<column name="password" length="50" not-null="true" />
</property>
<set name="ftpStatuses" inverse="true">
<key>
<column name="connectionid" not-null="true" />
</key>
<one-to-many class="common.entity.FtpStatus" />
</set>
</class>
</hibernate-mapping>
我知道我遗漏了什么,但我在hibernate上的谷歌搜索还没有发现。或者,直接使用
s.createSQLQuery()
或s.createQuery()
进行SQL查询也是可以接受的,但我编写该查询的成功率更低 您提供的条件
似乎只生成内部查询部分。您可以组合内部查询,例如使用DetachedCriteria
:
DetachedCriteria maxDateQuery = DetachedCriteria.forClass(FtpStatus.class);
ProjectionList proj = Projections.projectionList();
proj.add(Projections.max("datetime"));
proj.add(Projections.groupProperty("connectionid"));
maxDateQuery.setProjection(proj);
Criteria crit = s.createCriteria(FtpStatus.class);
crit.add(Subqueries.propertiesEq(new String[] {"datetime", "connectionid"}, maxDateQuery));
List<FtpStatus> dtlList = crit.list();
只需将propertiesEq更改为propertiesIn,即可正常工作。