ssedano
ssedano

Reputation: 8432

Hibernate Criteria filter by parent

I want to filter objects based in some conditions or if their parent is of a specific type.

class A {}

class B extends A {}

class O {
    A a;
    long n;
}

I want all O objects where n > 100 or a is of type B.

In Criteria what would be the or condition

Upvotes: 1

Views: 353

Answers (2)

Vishwas Shashidhar
Vishwas Shashidhar

Reputation: 67

You can use "Restrictions" type to create a criteria...

From my experience,

it should be something like below

Restrictions.ge("n", )

Sorry....don't remember the exact function name under Restrictions...You can check from the javadoc.

And for the object comparison, it's the same way except that you can use "eq" and give the object as a restriction...

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691625

Restrictions.eq("a.class", B.class)

I've had bugs though when a single-table inheritance type was chosen (using a discriminator), and where I had to use

Restrictions.eq("a.class", B.DISCRIMINATOR_VALUE)

Note that class is an implicit property. You don't need to define anything special in the entity to make it work.

Upvotes: 1

Related Questions