你的问题有点混乱,所以我对它的解释可能不正确。
但是,我怀疑你可以通过将选项分成两个类来解决你的问题。使选项成为具有两个具体子类的抽象类,即ListOption和OTCOption,每个子类都有其自己的鉴别值(3或5)。在两个子类中可能没有任何不同的字段,但是这会让您使用两个InstrumentTypeID来表示Option。
问题是,这意味着重复您的代码结构中的InstrumentType表中的知识,这显然不是一件好事。
如果您想完全使用数据驱动,我认为您必须更改Option和Stock,以使它们不是Instrument的子类。他们可能可能是InstrumentDetails接口的实现,每个类本身都是一个实体。 InstrumentType也是一个实体。然后,您可以给InstrumentType像一些代码:
@Entity
public class InstrumentType {
private static final Map<String, Class<? extends InstrumentDetails>> STORAGE_CLASSES = new HashMap<String, Class<? extends InstrumentDetails>>();
static {
STORAGE_CLASSES.put("Option", Option.class);
STORAGE_CLASSES.put("Stock", Stock.class);
}
public InstrumentDetails getDetails(Instrument inst) {
return getEntityManager().find(STORAGE_CLASSES.get(getStorageClass()), inst.getID());
}
// NB implementation of getEntityManager() is left as an exercise to the reader
}
而且,为了方便,对仪器本身的方法:
@Entity
public class Instrument {
public InstrumentDetails getDetails() {
return getInstrumentType().getDetails(this);
}
}
这里,我从硬存储类的字符串细节映射类,但是你可以从配置文件或注入动态获取它,或许可以更容易地添加新的存储类。