wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes Single table inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Single table inheritance" Watch "Single table inheritance" New topic
Author

Single table inheritance

Alexander Curvers
Greenhorn

Joined: Apr 03, 2009
Posts: 26
is this a good setup for inheritance?

http://pastebin.com/m27c80b10

im not sure about the constructor i want to easily create the object not just the specialized part'


tnx!
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Well, there are a number of ways of mapping inheritance, and it depends on what types of benefits and drawbacks you're interested in dealing with.

Joined, Single_Table and TABLE_PER_CLASS are your three options for mapping inheritance with Hibernate and JPA.



I don't see anything wrong with this code, so I would say that it should work if it fits your bill. I've always been a fan of single table inheritance myself.


The single table inheritance type is often the best choice for performing inheritance mapping. First of all, it minimizes the number of tables that need to be queried, which can be significant for large class hierarchies. Furthermore, mapping associations can be very difficult with other strategies.

For example, imagine we were using a single class per table strategy, and we decided an Ancestor could be associated in a one to one manner with another type, say a Location type. Since this relationship would be true for all subclasses, and given the fact that associations are implemented at the database level by foreign keys, we would have to add a new foreign key column to every table that was based on a sub-class of Ancestor. In a large class hierarchy, adding those foreign key fields would be extremely onerous and error prone. On the other hand, with the single table inheritance strategy, only one column would need to be added to one table, which would be a much, much simpler process.


Mapping Inheritance with the Java Persistence API (JPA)

-Cameron McKenzie
Alexander Curvers
Greenhorn

Joined: Apr 03, 2009
Posts: 26
HI Cameron

thanks for your reply
all the info was very helpfull

regards Alexander
Rahul Babbar
Ranch Hand

Joined: Jun 28, 2008
Posts: 210
Hi,

Although i am a fan of single table inheritance strategy, it is worth noticing its drawback...

If you have two entities A and B, in which a column x is present in A, but not in B, and is not null.

In this case, you cannot use a single table inheritance, simply because that column x is not present in entity B, and should be null for rows of B.

There are however some workarounds for it, like specifying the default value of column x, but each of them has some consequences(lets not go into the details of those.. )


Rahul Babbar
Rahul Babbar
Ranch Hand

Joined: Jun 28, 2008
Posts: 210
Alexander Curvers wrote:
im not sure about the constructor i want to easily create the object not just the specialized part'


I think it will be good idea to add a default no arg constructor for your classes, since you are looking to make them serializable otherwise you may get an error like this class cannot be serialized
Alexander Curvers
Greenhorn

Joined: Apr 03, 2009
Posts: 26
yeah tnx i noticed it when jboss didnt start because of it :-)
 
GeeCON Prague 2014
 
subject: Single table inheritance