Hibernate 标准查询使用键字段上的 Max() 投影并按外键进行分组
我很难将此查询(直接在数据库上工作)表示为 Hibernate(版本 3.2.5)中的标准查询:
SELECT s.*
FROM ftp_status s
WHERE (s.datetime,s.connectionid) IN (SELECT MAX(f.datetime),
f.connectionid
FROM ftp_status f
GROUP BY f.connectionid);
到目前为止,这是我想出的方法,但不起作用,并抛出 < code>无法解析属性: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();
这是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>
Connections.hbm.xml -
<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 查询也是可以接受的,但我编写该查询的成功率更低...... 。
I'm having difficulty representing this query (which works on the database directly) as a criteria query in Hibernate (version 3.2.5):
SELECT s.*
FROM ftp_status s
WHERE (s.datetime,s.connectionid) IN (SELECT MAX(f.datetime),
f.connectionid
FROM ftp_status f
GROUP BY f.connectionid);
so far this is what I've come up with that doesn't work, and throws a could not resolve property: datetime of: common.entity.FtpStatus
error message:
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();
Here's the relevant reference configuration that Netbeans 6.8 generated directly from the database:
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>
Connections.hbm.xml -
<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>
I know I'm missing something, but my googling on hibernate hasn't revealed it yet. Alternatively a SQL query directly using s.createSQLQuery()
or s.createQuery()
is also acceptable, but I've had even less success writing that one.....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您提供的
Criteria
似乎仅生成内部查询部分。您可以使用DetachedCriteria
来组合内部查询:请注意,直到 休眠 4.0.0.CR5 (HHH-6766)。
与本机 SQL 查询一样,如果希望在结果查询中具有显式列名称,Hibernate 的 createSQLString 应该立即使用您指定的查询字符串或添加的查询中涉及的实体。
The
Criteria
you supplied seems to generate only the inner query part. You can combine the inner query e.g. by usingDetachedCriteria
:Note that support for multicolumn subqueries is not implemented until in Hibernate 4.0.0.CR5 (HHH-6766).
As what comes to native SQL queries, Hibernate's
createSQLString
should work straight away with the query string you specified, or with the entities involved in the query added, if one want's to have explicit column names in the resulting query.