-->
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.  [ 5 posts ] 
Author Message
 Post subject: More than 30 joins in one query due to polymorphism
PostPosted: Mon Feb 18, 2008 2:10 pm 
Newbie

Joined: Sun Apr 29, 2007 4:27 am
Posts: 10
Location: Israel
I have the following class hierarchy:
Class AAA contains reference to BBB (named - bbb)
Class AAA2 inherits class AAA (consequently - has this reference too)
Classes BBB2,BBB3,BBB4,BBB5,BBB6 inherit BBB.

However, in our application - class AAA2 can only have reference to BBB6.

When calling ‘find by id’ on AAA2 instance - the query generated by hibernate has more than 5 left outer join because AAA2 has reference (due to the inheritance from AAA ) to BBB and BBB has 5 extenders.
When loading AAA2 - all left outer join on BBB descendents are useless because it can have only reference to BBB6.

Can you advice on good practice?
I have queries with more than 30 joins…

Thanks :)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 18, 2008 2:33 pm 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
two ideas

1. you could use the single_table inheritance mapping which would put all your BBBXs in one table

2. you could use type parameter approximatly like this:

@MappedSuperClass // note in this case AAA will not get mapped
public class AAA<B extends BBB>{

@<what ever mapping annotation you need>
B b;
}

@Entity
public class AAA2 extends AAA<BBB6>{
}


I only use Annotations, so I can't help with translation to Hibernate Core.

_________________
Please rate useful posts.


Schauderhaft: Softwaredevelopment, Projectmanagement, Qualitymanagement and all things "schauderhaft"


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 20, 2008 9:37 am 
Newbie

Joined: Sun Apr 29, 2007 4:27 am
Posts: 10
Location: Israel
Thanks for your answer.

1. Its solve the join amount , but you will still have a lot of columns in the select because hibernate select all subtype. My problem that I know that only one relevant , but I don’t know hot to tell this to hibermate.

2,. Great!!!!! Thank you for this. If you define AAA as an entity which I need it as an entity you will get an exception when building the factory. Is there any clever idea that can have entity with genric?


Top
 Profile  
 
 Post subject: Re: More than 30 joins in one query due to polymorphism
PostPosted: Mon Feb 25, 2008 3:01 am 
Newbie

Joined: Wed Feb 20, 2008 10:18 am
Posts: 1
Location: Hamburg, Germany
I had a similar problem too. Lots of unnessecary left outer joins in my query... Unfortunatly I could not use a single table or generics. But the following helped me.

I changed the mapping from joined-subclass to subclass with a join and defining the fetch-strategy as "select" (see chapter 13.2.4 in [1]):

<subclass name="MySubclass">
<join table="MYTABLE" fetch="select">
...
</join>
</subclass>

subclass needs a discrimator. I don't have one, but you can compute one with a formula :-)

<discrimnator formula="case when MYCOLUMN is not null then 'A' else 'B'" type="string"/> (see chapter 5.1.3 in [1])



[1] C. Bauer, G. King "Java Persistence with Hibernate"


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 25, 2008 7:42 am 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
Sharon. wrote:
Thanks for your answer.

Is there any clever idea that can have entity with genric?


Hibernate creates its mappings on startup (creation of the sessionFactory) that is.

At that time there is no information about the Type parameter left except its erasure. So in order for Hibernate to be able to use this the erasure must be some Hibernate Entity.

So if you use type parameters like <T extends SomeEntity> for references, what Hibernate sees is (AFAIK) 'SomeEntity'. So if it knows how to map those it should be able to performe the mapping.

_________________
Please rate useful posts.


Schauderhaft: Softwaredevelopment, Projectmanagement, Qualitymanagement and all things "schauderhaft"


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.