Never thought of this.
However, how would I go about doing this if the setValue method is actually an overriden one. For example, say my concrete class's setValue() has the logic built in, but the base class's setValue() is simple:
Code:
public abstract class BaseItem {
private boolean inStock = false;
private BigDecimal value;
public void setInStock(boolean inStock) {
this.inStock = inStock;
}
public void setValue(BigDecimal value) {
this.value = value;
}
}
public class StockItem extends BaseItem {
public void setValue(BigDecimal value) {
if (inStock) {
this.value = value;
}
else {
this.value = BigDecimal.ZERO;
}
}
public class CustomizedItem extends BaseItem {
// keeps base class setValue()
}
Know if I use hibernate's table per class hierarchy mixed with table per subclass for these three classes above with your suggestion, it would create duplicate columns-- one for BASE_ITEM table and the other for STOCK_ITEM table... I'm thinking of a hibernate mapping like this:
Code:
<class name="BaseItem" table="BASE_ITEM">
<id name="id" type="long" column="BASE_ITEM_ID">
<generator class="native"/>
</id>
<discriminator column="ITEM_TYPE" type="string"/>
<property name="inStock" column="IN_STOCK"/>
<property name="value" column="VALUE"/>
...
<subclass name="StockItem" discriminator-value="StockItem">
<join table="STOCK_ITEM">
<property name="fakeValue" column="FAKE_VALUE"/>
</join>
</subclass>
<subclass name="CustomizedItem" discriminator-value="CustomizedItem">
...
</subclass>
</class>
Hence the tables are not normalized since there will value and fake value, two columns to represent the same thing...
-los