I am trying to define a non-abstract base class with an embeddedid like so:
@Entity @Table(name="quote")
@Inheritance(strategy = InheritanceType.JOINED)
public class Quote implements Serializable
{
private final static long serialVersionUID = 1L;
@EmbeddedId
protected QuoteId id;
@Embeddable
private static class QuoteId implements Serializable {
private final static long serialVersionUID = 1L;
protected String symbol;
protected Date time;
public QuoteId()
{
}
public QuoteId(final String symbol, final Date time)
{
this.symbol = symbol;
this.time = time;
}
public String getSymbol() {
return this.symbol;
}
public void setSymbol(final String argSymbol) {
this.symbol = argSymbol;
}
public Date getTime() {
return this.time;
}
public void setTime(final Date argTime) {
this.time = argTime;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
QuoteId that = (QuoteId) o;
if (!symbol.equals(that.symbol)) return false;
return time.equals(that.time);
}
@Override
public int hashCode() {
int result = symbol.hashCode();
result = 31 * result + time.hashCode();
return result;
}
}
// more members, etc
}
I would like to subclass the Quote class, which I currently do like so:
@Entity @Table(name="option_quote")
public class OptionQuote extends Quote implements Serializable
{
private final static long serialVersionUID = 1L;
protected float strike;
protected float price;
// more members, etc
}
I allow Hibernate to create the schema for me which in Postgres which creates a quote table and an option_quote table which both have time and symbol columns. The option_quote table has a foreign key to the quote table which references quote(symbol, time).
My main issue at the moment is that I want to create and store instances of Quote objects when it is simply a stock quote and not an option quote. When I attempt to do this, JPA stores an “empty” option_quote row as well with all the member values 0. In addition to any comments and/or corrections to my model or annotations, I am hoping someone can point me in the right direction to prevent the option_quote from being created when I am trying to only store a Quote instance.
It may be that I have to change my model (which I control) to eliminate inheritance and simply have a StockQuote and an OptionQuote or some other model change. Thanks in advance for your insight!