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:
even after you optimize the database for this type of usage and optimize your queries it will be very expensive way of storage.
group related attributes/columns and store them in separate tables with same ID
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>
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 { ... }
Related posts:
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.
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