Friday, March 12, 2010

Clean database schema with inheritance

Tuesday, February 10, 2009, 8:00
This news item was posted in Notes category and has 2 Comments so far.

ORM Diagram

It is very common way of usage that  when ever you need some new attribute for your model objects/table you add a new column to the related database table.  After several requirements you will notice:

  • there will be lots of column in the same table
  • most of them are not always used
  • there is key columns you have for which columns will be used (eg: if type==individual then read name,surname,mobile else company name,tel,vs… )

even after you optimize the database for this type of usage and optimize your queries it will be very expensive way of storage.

solution

group related attributes/columns and store them in separate tables with same ID

benefits

  • self documentation: related attributes are organized among themselves
  • query performance: since data is separted between multiple tables you index will be more efficient and there will be less empty data cells
  • more live object oriented thinking in schema design

ORM practices

hibernate:

http://www.hibernate.org/hib_docs/reference/en/html/inheritance.html#inheritance-mixing-tableperclass-tablepersubclass

<br>
<class name="Payment" table="PAYMENT"><br>
<id name="id" type="long" column="PAYMENT_ID"><br>
<generator class="native"/><br>
</id><br>
<discriminator column="PAYMENT_TYPE" type="string"/><br>
<property name="amount" column="AMOUNT"/><br>
...<br>
<subclass name="CreditCardPayment" discriminator-value="CREDIT"><br>
<join table="CREDIT_PAYMENT"><br>
<property name="creditCardType" column="CCTYPE"/><br>
...<br>
</join><br>
</subclass><br>
<subclass name="CashPayment" discriminator-value="CASH"><br>
...<br>
</subclass><br>
<subclass name="ChequePayment" discriminator-value="CHEQUE"><br>
...<br>
</subclass><br>
</class><br>

JPA:

sample from

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class Boat implements Serializable { ... }

@Entity
public class Ferry extends Boat { ... }

@Entity
@PrimaryKeyJoinColumn(name="BOAT_ID")
public class AmericaCupClass  extends Boat { ... }

reference:

  • http://www.hibernate.org/hib_docs/annotations/reference/en/html/entity.html
  • http://www.hibernate.org/hib_docs/reference/en/html/inheritance.html#inheritance-mixing-tableperclass-tablepersubclass
  • http://www.jpox.org/docs/1_2/jpa_orm/inheritance.html
Reblog this post [with Zemanta]
Share and Enjoy:
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • Wists
  • LinkedIn
  • Slashdot
  • Google Bookmarks
  • Reddit
  • StumbleUpon
  • email
  • Twitter

Related posts:

  1. hibernate native Id generator tip
  2. hibernate tip: smart id generator
  3. Annotations have been more successful than XML
  4. Object Model vs Database Schema Design
  5. hibernate cache injection: missing…

You can leave a response, or trackback from your own site.

2 Responses to “Clean database schema with inheritance”

  1. Byju Joy said on Tuesday, February 24, 2009, 18:52

    Say, if a table with 100 columns is split into 10 tables with 10 columns each, then to read a record/business entity we gotta have 10 read operations, instead of earlier 1. Unless of course if we add custom logic of what tables to read, what tables to skip.

    Instead of ‘read’, if it is a ‘write’ operation then writing to 10 tables is much more costlier.

    Your point, I believe is to reduce memory requirement. That’s to reduce the weight of business entity.

    For me it’s a trade-off between Processing time Vs Memory footprint.

  2. altuure said on Thursday, February 26, 2009, 9:43

    hi Byju,
    First sorry for late response but I was on holiday :)
    in fact the write cost if as you have said instead on one insert ,it would be 10 statements but if you are working on all table this approach is not appropriate, however if your data some independence grouping among some fields it is more useful

    for the read you can read ten tables at one statement with SQL or ORM with left joins/fetch features memory usage will be same processing time is expected to be same or even less for read operation with effective Foreign-Keys and indexes

    Goal of this approach is separate data in to meaningful pieces

    Regards

Leave a Reply