vendredi 20 mars 2015

Map part of composite PK to composite FK



My data model consists of items with a history. I'll call a point in time an "instant"; all tables therefore have an "instant_id" that specifies how that item was configured at that instant. The "instant_id" is rolled into a composite primary key for all tables. Imagine the following example:



Table Computer
============
PK int instant_id <-- Shared id
PK int computer_id <-- Child id
int computer_type_id <-- Parent id
varchar foo

Table ComputerType
==================
PK int instant_id <-- Shared id
PK int computer_type_id <-- Parent id
varchar bar


There is a foreign key in Computer mapping (instant_id, computer_type_id) to the ComputerType primary key.


We use something like



@Embeddable ComputerId {
@Column(name='instant_id', nullable=false) int instant_id,
@Column(name='computer_id', nullable=false) int computer_id
}


Then:



Computer {
@EmbeddedId ComputerId id;

@MapsId('instant_id')
@ManyToOne
@JoinColumns({
@JoinColumn(name='instant_id',...),
@JoinColumn(name='computer_type_id',...)
})
ComputerType computerType;
}


No matter how I combine MapsId with JoinColumns, I can't seem to get this to work. Any ideas?




Aucun commentaire:

Enregistrer un commentaire