-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: Inheritance and subclasses fetching
PostPosted: Fri Mar 16, 2007 8:10 am 
Newbie

Joined: Fri Mar 16, 2007 7:40 am
Posts: 4
Hi,

I have a problem with Inheritance and subclasses fetching. Basically I have very simple inheritance structure like the following:

Code:
@Entity
public class Base {
}

@Entity
public class Child extends Base {
}


The problem is that when I'm querying for a Base class, it returns all subclasses as well. But I need only a Base class instances. I tried to use different inheritance strategies, and discriminators, but it didnt' help.

Finally I thought I found a sollution, joined inheritance and fetch='select', but it was for xml mapping.

And I don't know, and can't find anywhere how to set fetching mode (fetch=select) for an Entity using annotations. Is it possible at all?

thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 16, 2007 9:41 am 
Expert
Expert

Joined: Thu Sep 04, 2003 8:23 am
Posts: 368
In hbm files you can also set polymorphism="explicit" but I don't know if this is available through annotations

_________________
Seb
(Please don't forget to give credits if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 16, 2007 10:20 am 
Newbie

Joined: Fri Mar 16, 2007 7:40 am
Posts: 4
scesbron wrote:
In hbm files you can also set polymorphism="explicit" but I don't know if this is available through annotations


Yes, it is available. I tried to set it to explicit, but again got the same behaviour :(

Here is the exact code I'm using:

Code:
@Entity()
@org.hibernate.annotations.Entity(polymorphism = PolymorphismType.EXPLICIT)
@Inheritance(strategy=InheritanceType.JOINED)
public class Base {
}

@Entity
@PrimaryKeyJoinColumn(name="id", referencedColumnName = "id")
public class Child extends Base {
}


As I understand, but correct me if I'm wrong, for some reason, when there is a query for a superclass, it doesn't check whereas there is a record in the joined table for a subclass.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 16, 2007 12:23 pm 
Beginner
Beginner

Joined: Tue Jun 28, 2005 2:43 pm
Posts: 29
Location: Silicon Valley
Would you consider changing your own class hierarchy? Perhaps

Code:
@Entity
public abstract class AbstractBase {
}

@Entity
public class Base extends AbstractBase {
}

@Entity
public class Child extends AbstractBase {
}


If you query for Base, you get only Base, but if you query for AbstractBase you get both. I would suggest that if you want queries for Base not to include instances of Child, then Child isn't really a true subclass of Base in the first place.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 17, 2007 4:47 am 
Newbie

Joined: Fri Mar 16, 2007 7:40 am
Posts: 4
Actually, I can't change the class hierarchy. The thing is that the base class is a part of the existing application. And in that application there are a lot of queries for a base.

Now I need to add a child of the base, but all that queries should fetch the base only.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 17, 2007 9:29 am 
Expert
Expert

Joined: Thu Sep 04, 2003 8:23 am
Posts: 368
So why do you want to set inheritance in hbm files ?
You can have inheritance in java but not specify it in hibernate. Thus this classes are not correlated

_________________
Seb
(Please don't forget to give credits if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 17, 2007 10:06 am 
Newbie

Joined: Fri Mar 16, 2007 7:40 am
Posts: 4
scesbron wrote:
So why do you want to set inheritance in hbm files ?
You can have inheritance in java but not specify it in hibernate. Thus this classes are not correlated


But these classes are correlated. Child class should have something from the Base class, also it has to introduce something own. And Child class should behave like a base in some cases.
Furthermore they have to share at least the id column to save the existing relationships of the Base class with other classes.

I figured out that when hibernate querying for Base class, it usesleft outer join to the Child class. Thats why it fetches all records from the Base table.
And I need to specify somehow that when querying for a Base class, it needs to check the Child table. Or it should be specified for a Child class, not to fetch it, until the explicit query will ask for it.

But I don't know how to specify such kind of limitation.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.