-->
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: should I use Single Table or Table per Class?
PostPosted: Tue Nov 22, 2005 12:02 am 
Senior
Senior

Joined: Tue Sep 13, 2005 2:01 am
Posts: 137
I have a class hierarchy. some new subclasses might be added in the future.

Should I use Single Table or Table per Class?

some cons/pros I can think of :

Single Table:
---------------
pros: better performance
cons: many unrelevant column are NULL. when a new subclass is added in the future, new columns will need to be added to existing table. Is it a good idea to change table schema?


Table per Class
-----------------
pros: for adding new subclasses, no changes to existing tables, no NULLs for unrelevant columns.
cons: perform not as good


which mapping strategy should I use for a class hierarchy that may add more subclasses in the future?

suggestion/comments appreciated. Thanks,

Dave


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 22, 2005 12:26 am 
Newbie

Joined: Mon Nov 21, 2005 3:38 pm
Posts: 5
If you are planning to add more classes in the future I think that Table-per-class or joined subclass would be better options. You don't want to have to make columns NULL-able that should be NOT NULL because of the way you've done the O/R mapping.

IMO, joined subclass is nice if you are going to be treating the objects polymorphically in your code. Table-per-class is better if inheritence primarily just makes the Java code cleaner. If you use table-per-class and you want to find all instances across all concrete subtypes, this is a pain to do especially if you also need to have some SQL code that uses the same tables. With joined subclass it is easy because every entity has a record in the base table.

Joined-subclass does introduce some extra load on the database because of the join but if it's just 1 join it's probably not going to be a big deal. I have seen quite large tables work fine with the joined subclass strategy.

Of course one of the nice things about using Hibernate is that you can change your mind pretty easily. You won't have to rewrite all your queries if you swtich from one strategy to the other.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 22, 2005 6:58 pm 
Senior
Senior

Joined: Tue Sep 13, 2005 2:01 am
Posts: 137
Thanks.

The class hierarchy in my case may have 3 - 5 levels in depth. I am concerned about the performance if using JOINED. The nice thing about Table-per-class is that I can see all properties of a class in a table, and it may perform better than JOINED because of fewer joins.

For table-per-class, To find all instances of a class hierarchy,
EJB QL : select * from base-class.

this will involve:

Union(all tables) order by id

For JOINED, to find all instances of a class hierarchy,
EJB QL: select * from base-class.

this will involve:

select * from base-table joined with all other tables

For treating class hierarchy polymorphically, why JOINED is better than Table-per-class? Thanks.!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 22, 2005 8:18 pm 
Newbie

Joined: Mon Nov 21, 2005 3:38 pm
Posts: 5
You have to join to get the complete objects, but you can find the Ids (and types) of all the objects and query on shared properties with just the base table. You can load proxies to the objects without doing the joins, then execute the joins to fetch the properties on demand.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 22, 2005 11:21 pm 
Senior
Senior

Joined: Tue Sep 13, 2005 2:01 am
Posts: 137
For JOINED, we can not tell row class type by looking at base table, right? because there is no discriminatorColumn as SINGLE_TABLE does. For a specific instance with id, we need to look for the id in other tables to find out its class type. This seems very unefficient. I do not know how Hibernate does it like in em.find(base-class-type, id).


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.