如何在Hibernate中基于自定义查询的集合映射? [英] How to do custom-query based collection mappings in Hibernate?

查看:69
本文介绍了如何在Hibernate中基于自定义查询的集合映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个被映射为具有一组对象的对象,非常简单.但是,我真正想做的是在该映射上设置一些条件.这是当前的映射:

I have an object that is mapped to have a set of objects, very simple. However, what I really want to do is put some criteria on that mapping. Here's the current mapping:

    <set name="ops" inverse="true" cascade="all, delete-orphan">
        <key column="cityblock_id" on-delete="cascade"/>
        <one-to-many class="com.tamedtornado.data.Operation"/>
    </set>

现在,我不仅要获取所有相关联的操作,还想获取尚未完成或尚待完成的操作.尽管一些旧的论坛帖子向我展示了一些内容,但是Hibernate文档对此非常安静.您显然可以使用sql-query标记,但这也不是我想要的.

Now, instead of just getting all associated ops, I want to just get ops that haven't completed, or have, or whatever. The Hibernate docs are very quiet on this, although some old forum posts have shown me a little. You can apparently use a sql-query tag, but that's also not what I want to do.

反正有HQL查询吗?我只想通过一个简单的查询来缩小结果范围,并提供一些集合图,例如active_ops和completed_ops等.

Is there anyway to do this with an HQL query? I just want to narrow the results with a simple query and have a few collection maps, like active_ops and completed_ops, etc.

干杯, 杰森

推荐答案

6.2.在Hibernate文档中的集合映射,您会发现可以在任何集合映射上使用WHERE子句:

Under 6.2. Collection mappings in the Hibernate docs you will find that you can use a WHERE clause on any collection mapping:

其中(可选)指定在检索或删除集合时要使用的任意SQL WHERE条件(如果集合仅应包含可用数据的子集,则很有用)

您说过要使用HQL查询,但我认为这是不可能的.但是,由于只需在映射中指定一个简单的WHERE子句,HQL和真实" SQL之间并没有太大区别.

You said that you want to use a HQL Query but I don't think that is possible. But as you only have to specify a simple WHERE clause in your mapping there isn't really much difference, between HQL and "real" SQL.

这篇关于如何在Hibernate中基于自定义查询的集合映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆