I’m working on a JPA project. I have an ExportProfile object:
@Entity
public class ExportProfile{
@Id
@GeneratedValue
private int id;
private String name;
private ExtractionType type;
//...
}
ExtractionType is an interface implemented by several classes, each for a different extraction type, these classes are singletons.
So type is a reference to a singleton object. I don’t have an ExtractionType table in my DB, but i have to persist the extraction type of my export profile.
How can I persist the ExportProfile object using JPA, saving the reference to type object?
NOTE: The number of ExtractionType implementations is not defined, because new implementation can be added anytime. I’m also using Spring, can this help?
Here’s an idea: make an
ExtractionTypeEnum, an enumeration with one element for each of the possible singletons that implementExtractionType, and store it as a field in your entity, instead ofExtractionType. Later on, if you need to to retrieve the singleton corresponding to aExtractionTypeEnumvalue, you can implement a factory that returns the correct singleton for each case:In the above, I’m assuming that both
ConcreteExtractionType1andConcreteExtractionType2implementExtractionType.