开发者

Hibernate Inheritance problem

开发者 https://www.devze.com 2023-01-07 19:56 出处:网络
I am using Hibernate 3.0 . I am facing an issuein 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 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.


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.)


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消