使用条件查询检索多态Hibernate对象 [英] Retrieving Polymorphic Hibernate Objects Using a Criteria Query

查看:113
本文介绍了使用条件查询检索多态Hibernate对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的模型中,我有一个抽象的用户类,以及多个子类,例如Applicant,HiringManager和Interviewer。他们在一张桌子上,我有一个单一的DAO来管理他们。

用户:

  @Entity 
@Table名称=User)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
name =role,
discriminatorType = DiscriminatorType.STRING

公共抽象类用户扩展BaseObject实现可识别< Long> ...

HiringManager(例如):

  @Entity 
@DiscriminatorValue(HIRING_MANAGER)
公共类HiringManager扩展用户...

现在,如果我想要让所有与部门无关的招聘经理,我该怎么做?我想它会看起来像这样:

  DetachedCriteria c = DetachedCriteria.forClass(User.class); 
c.add(Restrictions.eq(role,HIRING_MANAGER));
c.add(Restrictions.isNull(department));
列表<用户> results = getHibernateTemplate()。findByCriteria(c);

但是当我运行这个时,Hibernate抱怨无法解析property:role因为User类实际上没有明确的角色属性)

那么我想要做什么的正确方法是什么?

解决方案

我可能会错过某些明显的东西,但是既然您想找到招聘经理,为什么不将 HiringManager 子类作为该类到 Criteria ?为了防万一,有一个特殊的 class 属性,您可以使用它来将查询限制为子类型。从Hibernate参考文档:


14.9。 where子句



...



特殊属性在多态
持久性的情况下访问
实例
的鉴别符值。嵌入在where子句中的Java类名称
将被
转换为其鉴别值。

  from Cat cat where cat.class = DomesticCat 


您可以使用此特殊功能class属性与Criteria API相同,但只需稍作更改:您必须使用鉴别器值作为值。

  c.add(Restrictions.eq(class,HIRING_MANAGER));与HQL不同,看起来Hibernate并没有使用Criteria API进行翻译。我真的不喜欢在代码中使用鉴别器值的想法,也许有另一种更清晰的方式,但我不知道它。但它有效。


In my model I have an abstract "User" class, and multiple subclasses such as Applicant, HiringManager, and Interviewer. They are in a single table, and I have a single DAO to manage them all.

User:

@Entity
@Table(name="User")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
    name="role",
    discriminatorType=DiscriminatorType.STRING
)
public abstract class User extends BaseObject implements Identifiable<Long> ...

HiringManager (for example):

@Entity
@DiscriminatorValue("HIRING_MANAGER")
public class HiringManager extends User ...

Now if I wanted to, say, get all the hiring managers that are not associated with a department, how would I do that? I imagine it would look something like:

DetachedCriteria c = DetachedCriteria.forClass(User.class);
c.add(Restrictions.eq("role", "HIRING_MANAGER"));
c.add(Restrictions.isNull("department"));
List<User> results = getHibernateTemplate().findByCriteria(c);

But when I run this, Hibernate complains "could not resolve property: role" (Which actually makes sense because the User class really doesn't have an explicit role property)
So what's the right way to do what I'm trying to do?

解决方案

I may be missing something obvious but since you want to find hiring managers, why don't you just pass the HiringManager subclass as the class to the Criteria?

Just in case, there is a special class property that you can use to restrict a query to a subtype. From the Hibernate reference documentation:

14.9. The where clause

...

The special property class accesses the discriminator value of an instance in the case of polymorphic persistence. A Java class name embedded in the where clause will be translated to its discriminator value.

from Cat cat where cat.class = DomesticCat

You can use this special class property with the Criteria API too, but with a minor change: you have to use the discriminator value as value.

c.add(Restrictions.eq("class", "HIRING_MANAGER"));

Unlike with HQL, it looks like Hibernate is not doing the translation with the Criteria API. I don't really like the idea of using the discriminator value in the code and there is maybe another cleaner way but I'm not aware of it. But it works.

这篇关于使用条件查询检索多态Hibernate对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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