KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have an object with a field that can be a number of object types. This object is encoded in a single table with a discriminator column for the field's subtypes. Each of these subtypes have their fields mapped to a column in the parent objects table. I cannot seem to model this in hibernate. The code bellow will return null for getSubfield() regardless of what subtype data is in the table. Schema id type whosit whatsit +----+------+--------+---------+ | 1 | "A" | "test" | null | | 2 | "B" | null | "test" | +----+------+--------+---------+ Domain Objects @Entity public class Parent { protected @Id @GeneratedValue int id; protected Subfield subfield; public Subfield getSubfield() {return subfield;} } @Embeddable @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name="type", discriminatorType=DiscriminatorType.STRING) public abstract class Subfield {} @DiscriminatorValue("A") public class TypeA extends Subfield { public String whosit; } @DiscriminatorValue("B") public class TypeB extends Subfield { public String whatsit; } "SELECT p FROM parent p" {id=1,subfield=null} {id=2,subfield=null} Is it possible to accomplish what I want with this object model, or do I need to get a bit more creative (this is a legacy database, changing the schema is not preferred)
Tags (comma-separated)
Save Edits
Cancel