如何在 Hibernate 中执行非多态 HQL 查询? [英] How to perform a non-polymorphic HQL query in Hibernate?

查看:35
本文介绍了如何在 Hibernate 中执行非多态 HQL 查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Hibernate 3.1.1,特别是我使用的是 HQL 查询.

I'm using Hibernate 3.1.1, and in particular, I'm using HQL queries.

根据文档、Hibernate的查询是多态的:

According to the documentation, Hibernate's queries are polymorphic:

这样的查询:from Cat as cat 不仅返回Cat 的实例,还返回DomesticCat 等子类的实例.

A query like: from Cat as cat returns instances not only of Cat, but also of subclasses like DomesticCat.

如何查询 Cat 的实例,而不查询它的任何子类?

我希望能够做到这一点,而不必明确提及每个子类.

I'd like to be able to do it without having to explicitly mention each subclass.

我知道以下选项,但并不满意:

I'm aware of the following options, and don't find them satisfactory:

  1. 在查询后手动过滤实例,或者:
  2. 在鉴别器列上手动添加 WHERE 子句.

Hibernate 允许用户决定查询是否应该是多态的是有意义的,但我找不到这样的选项.

It would make sense for Hibernate to allow the user to decide whether a query should be polymorphic or not, but I can't find such an option.

提前致谢!

推荐答案

/core/reference/en/html_single/#mapping-declaration-class" rel="noreferrer">类映射.这将导致查询仅返回命名类的实例,而不返回其子类.

Use polymorphism="explicit" in the class mapping. This will cause queries to return only instances of the named class and not its subclasses.

隐式多态性意味着类的实例将是由命名任何的查询返回超类或实现的接口或类,以及任何的实例将返回该类的子类通过命名类的查询本身.显式多态性意味着该类实例将被返回仅通过明确命名的查询那个班.

Implicit polymorphism means that instances of the class will be returned by a query that names any superclass or implemented interface or class, and that instances of any subclass of the class will be returned by a query that names the class itself. Explicit polymorphism means that class instances will be returned only by queries that explicitly name that class.

这篇关于如何在 Hibernate 中执行非多态 HQL 查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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