(using Hibernate 3.3.1 and Annotations 3.4) I have all of my mappings working for my legacy database but ran into a roadblock on the design of one them.
A quick overview. I have a base object:
Code:
@Entity
@Table(name = "Objects")
@PrimaryKeyJoinColumn(name = "objectid", referencedColumnName = "id"))
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class SuperObject {
And its identifier:
Code:
@Id
@DocumentId
@GeneratedValue(strategy = GenerationType.TABLE, generator = "ObjectId")
@TableGenerator(name = "ObjectId", table = "TableKeys", initialValue = 1, pkColumnName = "tablename", pkColumnValue = "Objects", valueColumnName = "id")
@Column(name = "id")
protected int getSurrogateKey() {
return _surrogateKey;
}
Several classes extend this object (and their corresponding tables use this surrogateKey as the foreign key to the superclass table). Example:
Code:
@Entity
@SecondaryTable(name = "Concepts")
@DiscriminatorValue(value = ValueTypeIds.CONCEPT)
public class Concept {
All is well so far, but then there is an exceptional case. I have a subclass of this SuperObject called "Relation". Relations are between concepts (shown above). Relations have one sourceConcept and one-to-many targetConcepts. But here's where it gets tricky. For performance reasons, all of this is stored in the Relations table. Thus, a single Relation can have multiple rows in the Relations table (multiple occurrences of the same surrogateKey), with the db composite key being the surrogateKey + target.
A more graphical explanation of the paragraph above...
Objects Table:
id
1 (concept)
2 (concept)
3 (concept)
4 (relation)
Relations Table:
id/surrogateKey_____sourceConcept_____targetConcept
4_________________1________________2___________
4_________________1________________3___________
So my basic question is how to map this in Hibernate.
Will the Relation class be allowed to extend the above SuperObject class and can Hibernate know to store and retrieve multiple rows from the Relations table given a single surrogateKey?
Obviously, I need to reflect the composite key in my object model. So I have played around with @Embeddable/@EmbeddedId and I believe I have that working but I have a total of three classes. Relations (which is an entity class and the business representation of a Relation), RelationEntity (which is also an entity class for the same table but includes a specific target). The Relations class has a list of RelationEntities to contain all the relation targets within one Relation. And then the RelationPK class, which is the @embeddable defining the actual composite key for Hibernate.
Does anyone have a different approach for this problem? Let me know if you need more information.
Thanks!