如何将简单的 Hibernate 连接结果映射到具有子对象的对象?

发布于 2024-12-09 17:30:28 字数 1107 浏览 0 评论 0原文

我对如何查询其中包含子对象的对象列表感到困惑。

我有两个课程“执行”和“订单”。在执行中,我将订单对象作为一对一关系:

public class Execution {
    private long id;
    private Order order;
    ...

在 Execution.hbm.xml 中,我将订单配置为具有 unique="true" 的多对一关系。

<class name="Execution" table="executions">
    <id name="executionId" type="long">
        <generator class="native" />
    </id>
    <many-to-one name="Order" unique="true" fetch="join" class="Order" />
</class>

通常,我们使用 HibernateDAOSupport 查询 Execution 对象,方法是:

List<Execution> executions = getHibernateTemplate().find("from Execution");

通过上述关系,我们如何查询它以及在哪里可以找到有关复杂连接的持久对象的更多信息?我已经尝试过了:

List<Execution> list = getHibernateTemplate().find("from Execution e left outer join e.order");

但它给出了 ClassCastException,因为查询似乎没有返回 Execution 对象,并且在 Execution 中的 Order 对象中包含额外的订单详细信息。

映射有问题吗?还是查询?

非常感谢您的指点!

--更新

抱歉,我是个菜鸟。显然第一个查询有效。在阅读了大量有关 HQL 的文档后,他们对此还不够清楚。任何更好的文档都会有帮助。谢谢...

I am confused about how to query for a list of objects with sub-objects in it.

I have two classes Execution and Order. And In Execution, I have the order object in it as a one-to-one relationship:

public class Execution {
    private long id;
    private Order order;
    ...

In the Execution.hbm.xml, I have configured Order as a many-to-one with unique="true".

<class name="Execution" table="executions">
    <id name="executionId" type="long">
        <generator class="native" />
    </id>
    <many-to-one name="Order" unique="true" fetch="join" class="Order" />
</class>

Normally, we query for Execution objects with HibernateDAOSupport by using:

List<Execution> executions = getHibernateTemplate().find("from Execution");

With the above relationship, how do we query it and where can I find more information on persisting objects for complicated joins? I've tried:

List<Execution> list = getHibernateTemplate().find("from Execution e left outer join e.order");

But it's giving a ClassCastException as the query does not seem to return Execution objects with the extra order details in the Order object in Execution.

Is something wrong with the mapping? Or the query?

Thanks so much for any pointers!

-- update

Sorry for being a noob. Apparently the first query works. After reading tons of documentation on HQL, they weren't clear enough about this. Any better documentation would be helpful. Thanks...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

別甾虛僞 2024-12-16 17:30:28

这是行不通的,因为您没有明确告诉您结果中只需要执行实体。它无法弄清楚,因为加入 order 实体也会导致 Order 属性成为结果的一部分。

List<Execution> list = getHibernateTemplate().find(
    "from Execution e left outer join e.order");

只需添加选择,它就会正常工作:

List<Execution> list = getHibernateTemplate().find(
    "select e from Execution e left outer join e.order");

但是拥有这样的查询是否有意义是另一个问题,一般来说,您不必在查询中进行联接来填充关系属性。它们是否已填充是通过将获取策略设置为 LAZY/EAGER 或根据默认值来控制的。

在您的情况下,仅当您想通过某些属性按顺序限制查询结果时,顺序才应该存在于查询中

  1. (在这种情况下,您也可以避免在 HQL 查询中加入 JOIN,而只需浏览路径:e.order.id=2 ) 或
  2. 与订单的关系是惰性的,您只想确保订单
    已获取:

    列表列表 = getHibernateTemplate().find(
    "from Execution e left external join fetch e.order");

This will not work, because you do not explicitly tell that you want only Execution entities in your result. It cannot figure it out, because of join to the order entity causes also Order attributes to be part of result.

List<Execution> list = getHibernateTemplate().find(
    "from Execution e left outer join e.order");

Just add select and it will work fine:

List<Execution> list = getHibernateTemplate().find(
    "select e from Execution e left outer join e.order");

But does it make sense to have a such a query is other question, in general you do not have to make joins in queries to have relationship attributes populated. Are they populated is controlled by setting fetching strategy to LAZY/EAGER or by depending about defaults.

In your case order should exist in query only if

  1. you want to limit results of query by some attribute in order (in such a case you can also avoid JOIN in your HQL query and just navigate through the path: e.order.id=2) or
  2. relationship to order is lazy and you just want to ensure that order
    is fetched:

    List list = getHibernateTemplate().find(
    "from Execution e left outer join fetch e.order");

霞映澄塘 2024-12-16 17:30:28

@Mikko 和@JB 的回答/评论是正确的。要查询子对象,只需将行更改

List<Execution> list = getHibernateTemplate().find(
"select e from Execution e left outer join e.order");

List<Execution> list = getHibernateTemplate().find(
"select e from Execution e left outer join e.order as o where o.id = ?");

@Mikko's and @JB's answer/comments are correct. To query the on the sub object just change the line

List<Execution> list = getHibernateTemplate().find(
"select e from Execution e left outer join e.order");

to

List<Execution> list = getHibernateTemplate().find(
"select e from Execution e left outer join e.order as o where o.id = ?");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文