Hi Ivan,
May I suggest using an abstract class rather than an interface?
for example
Code:
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="value_type",
discriminatorType=DiscriminatorType.STRING
)
@Table(name="house_property_value")
@javax.persistence.SequenceGenerator(
name="seq_house_property_value",
sequenceName="s_house_property_value",
allocationSize=1
)
public abstract class HousePropertyValue implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="seq_house_property_value")
Long id;
@Column(name="value_key", nullable = false)
String valueKey;
@ManyToOne (fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "fk_house_id", referencedColumnName = "id", nullable = false)
House house;
public abstract Object getValue();
public abstract void setValue(Object value);
}
Notice that I've annotated properties rather than methods (just my personal preference) and ommitted the getters and setters. This now means you can forget all about id,valueKey and house in subclasses!
Your subclass would then look something like this:
Code:
@Entity
@DiscriminatorValue("DATE")
@javax.persistence.SequenceGenerator(
name="seq_house_property_value",
sequenceName="s_house_property_value",
allocationSize=1
)
public class HousePropertyDateValue extends HousePropertyValue {
@Column(name="value_date", nullable=false)
private Date value = new Date();
public HousePropertyDateValue() {}
public HousePropertyDateValue(Date value) { this.value = value; }
public Object getValue() { return value; }
public void setValue( Date value ) { this.value = value; }
public void setValue( Object value ) { this.value = (Date) value; }
}
One last thing.. depending on the version of Java you are using - why not use generics with getValue and setValue?
Code:
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="value_type",
discriminatorType=DiscriminatorType.STRING
)
@Table(name="house_property_value")
@javax.persistence.SequenceGenerator(
name="seq_house_property_value",
sequenceName="s_house_property_value",
allocationSize=1
)
public abstract class HousePropertyValue<T> implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="seq_house_property_value")
Long id;
@Column(name="value_key", nullable = false)
String valueKey;
@ManyToOne (fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "fk_house_id", referencedColumnName = "id", nullable = false)
House house;
public abstract T getValue();
public abstract void setValue(T value);
}