Retrieving Polymorphic Hibernate Objects Using a Criteria Query
Asked Answered
L

2

15

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?

Lamb answered 7/7, 2010 at 22:52 Comment(0)
D
23

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.

Dumm answered 7/7, 2010 at 23:54 Comment(3)
And you can use the value of HiringManager.class in the query as well as the value of the discriminator; so it can be written as: c.add(Restrictions.eq("class", HiringManager.class)); โ€“ Agonized
In HQL you use it like alias.class = HiringManager without .class suffix. โ€“ Vested
@Pascal Thivent How can I order by class in this case. ๐จ๐ซ๐๐ž๐ซ.๐š๐๐(๐œ๐›.๐š๐ฌ๐œ(๐ซ๐จ๐จ๐ญ.๐ ๐ž๐ญ("๐œ๐ฅ๐š๐ฌ๐ฌ"))); ๐จ๐ซ๐๐ž๐ซ.๐š๐๐(๐œ๐›.๐๐ž๐ฌ๐œ(๐œ๐›.๐ฌ๐ž๐ฅ๐ž๐œ๐ญ๐‚๐š๐ฌ๐ž().๐ฐ๐ก๐ž๐ง(๐œ๐›.๐ข๐ฌ๐๐จ๐ญ๐๐ฎ๐ฅ๐ฅ(๐ซ๐จ๐จ๐ญ.๐ ๐ž๐ญ("๐ฎ๐ฉ๐๐š๐ญ๐ž๐ƒ๐š๐ญ๐ž")), ๐ซ๐จ๐จ๐ญ.๐ ๐ž๐ญ("๐ฎ๐ฉ๐๐š๐ญ๐ž๐ƒ๐š๐ญ๐ž")).๐จ๐ญ๐ก๐ž๐ซ๐ฐ๐ข๐ฌ๐ž(๐ซ๐จ๐จ๐ญ.๐ ๐ž๐ญ("๐œ๐ซ๐ž๐š๐ญ๐ž๐ƒ๐š๐ญ๐ž")))); then i got error Unable to locate Attribute with the the given name [class] on this ManagedType โ€“ Snowdrop
W
3

Note that this also works for Joined Subclass inheritance mappings, with one caveat. The class used in the Restriction must be a concrete class. If you have a model like Animal <- Mammal <- Cat, you cannot do Restrictions.eq("class", Mammal.class); and expect to get back all Mammals. Instead, I had to brute force it using Restrictions.or with all known concrete classes (using Restrictions.in() gave a class cast exception when executing the query).

Wilona answered 10/7, 2012 at 15:36 Comment(0)

© 2022 - 2024 โ€” McMap. All rights reserved.