Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Single table inheritance

 
Alexander Curvers
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Cameron

thanks for your reply
all the info was very helpfull

regards Alexander
 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah tnx i noticed it when jboss didnt start because of it :-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic