-->
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.  [ 9 posts ] 
Author Message
 Post subject: 61 table join limit with MySQL
PostPosted: Mon Oct 08, 2007 7:07 pm 
Newbie

Joined: Mon Oct 08, 2007 6:55 pm
Posts: 2
Location: california
Hi Guys,

I am working on a production application with about 100 different object types. My object hierarchy requires all objects to extend class 'Human'. When I call a 'load' call on a Human object the SQL sent to MySQL is gigantic... there are about 100 left outer joins. I guess Hibernate does this to figure out if any child objects exist that belong to the loaded object.

Is there a way to turn that feature off? I need full polymorphic features so I have the following configuration

Code:
<class name="Human">
    <joined-subclass name="Child"...>
    <joined-subclass>

    <joined-subclass name="Parent"...>
    <joined-subclass>

    <joined-subclass name="Baby"...>
    <joined-subclass>

    ...etc
</class>



Hibernate version: 3.2

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Code:
Query q = session.createQuery ( "select hu from Human as hu where hu.id=:id" );
q.setString ( "id", Id.toString() );
Human pa = (Human) q.uniqueResult ();
System.out.println(pa.getName());


Full stack trace of any exception that occurs:

Code:
14:31:24,065 ERROR JDBCExceptionReporter:78 - Too many tables; MySQL can only use 61 tables in a join


Name and version of the database you are using:

MySQL 5

The generated SQL (show_sql=true):

Gigantic SQL code with 100 LEFT OUTER JOINs

Debug level Hibernate log excerpt:

Debug

_________________
Thanks!, Omid S


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 09, 2007 6:32 am 
Newbie

Joined: Tue Oct 09, 2007 6:30 am
Posts: 2
Maybe that's MySQL limitation?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 20, 2007 6:06 am 
Newbie

Joined: Tue Dec 05, 2006 3:00 am
Posts: 15
Then how to avoid joins while working with single table?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 20, 2007 6:49 am 
Regular
Regular

Joined: Sat Nov 25, 2006 11:37 am
Posts: 72
That is IMO not a sensible design. I am sure other database systems will choke on that as well. And 61 left outer joins will just be horrific performance wise on any non trivial datasets. Do you really have 100+ different tables in the database modelling the different types of humans?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 21, 2007 1:35 am 
Newbie

Joined: Tue Dec 05, 2006 3:00 am
Posts: 15
Can't we avoid JOINS ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 21, 2007 2:13 am 
Regular
Regular

Joined: Sat Nov 25, 2006 11:37 am
Posts: 72
Without more details about your database structure and what you want to achieve it is very hard to make sensible suggestions. Of course you can avoid joins by not using inheritance mapped via joined subclasses. Basically if you want inheritance, that is you want an entity hierarchy, and your underlying database structure uses the table per subclass model, then you get outer joins. No way around that. Your only other option, assuming you cannot change the underlying database structure, is to model these as independent entities, that is don't use an inheritance hierarchy, and model the relationships as you would normally do with foreign keys, i.e. one-to-many, many-to-one, one-to-one, whatever is appropriate. Then by defining those relationships as lazy loading you get pretty much what you need.

The inheritance bit you could then graft on in various ways. For example define an interface HumanKind which has all the methods of Human you want to expose. Obviously Human will implement it. Provide an abstract class, lets say SuperHuman which also implements it but based on a abstract getter which provides access to a Human object. This getter will be present in all subclasses because they will have a foreign key relationship to Human. Here is a simple example (based on JPA annotations):

Code:
interface HumanKind {
    Date getDateOfBirth();
}

abstract class SuperHuman implements HumanKind {
    abstract Human getHuman();

    Date getDateOfBirth() {
        return getHuman().getDateOfBirth();
    }
}

@Entity
class Parent extends SuperHuman {
    private Human human = new Human();

    @ManyToOne(....)
    @JoinColumn(....)
    Human getHuman() {
        return Human;
    }

    void setHuman(Human human) {
        this.human = human;
    }
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 21, 2007 2:36 am 
Newbie

Joined: Tue Dec 05, 2006 3:00 am
Posts: 15
My scenario is :

I Have Classes like

A
B extends A
C extends B
D extends C
E extends D
F extends E

And Tables Like

A (
col1 Primarykey,
col2 ,
..
..
..
col10

)

B (
col1 primarKey,FK(A.col1)
..
..
..
)

C (
col1 primarKey,FK(B.col1)
..
..
..
)

...
...
...
...



F (
col1 primarKey,FK(E.col1)
..
..
..
)


Here I used <joined-Subclass> in HBM files for child classes


and when am calling "session.get(A.Class, 10)" , Hibernate generating JOINS with child classes like B,C,D,E,F . So here it taking more time to execute. My requirement is just I dont want JOINS while am working with single table.


Let me know if am not clear or need any information


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 21, 2007 2:59 am 
Regular
Regular

Joined: Sat Nov 25, 2006 11:37 am
Posts: 72
As I said above, the moment you define <joined-subclass> you get joins because that is what you tell Hibernate to do. All queries are automatically polymorphic, that is they retrieve all subclasses. If you don't want that don't use inheritance for your entities use relationships instead.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 21, 2007 3:54 am 
Newbie

Joined: Tue Dec 05, 2006 3:00 am
Posts: 15
Is there any other approach to avoid JOINS while working with Inheritence with above scenario?


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