tags:

views:

36

answers:

2

I am using Hibernate 3.0 . I am facing an issue in using joined sub class.

Here is a small example

I have an Animal super class, and i have 2 sub classses Dog and Cat. I am jusing joined sub class for defining this relationship.

when i do

Query query=session.createQuery("from Animal");

it is fetching the objects of animal,dog and cat.

This is creating a problems as the fetching time is more.

Any solution for the same.

A: 

I assume you only want the Animal instances, not their subclasses?

That's a little tricky to do in JPA, as cats are also animals, hence they are returned.

You have to exclude them manually from the query, like this:

from Animal a 
     where a.id not in (select c.id from Cat c)
       and a.id not in (select d.id from Dog d)

(As you have this problem, maybe polymorphism isn't perfect solution in this case.)

Henning
A: 

You can switch-off the implicit polymorphism for this class by setting polymorphism="explicit" in the xml mapping, or use the hibernate annotation @Entity(polymorphism=PolymorphismType.EXPLICIT)

Bozho