Hello,
I have inherited a database with a composite key, each table has the following:
int id
int classId
These two columns are combined as the key of almost every table in this db. I have discovered the way to correctly write the entity classes with the corresponding <class name>Id class that contains the component key. Here's an example of this that is based on the
rp.batch table:
Code:
@Entity
@Table(name = "rp.batch")
public class HBatch {
private HBatchId key;
public HBatch() {
}
// Property accessors
@EmbeddedId
@AttributeOverrides( {
@AttributeOverride(name="id", column=@Column(name="id", unique=false, nullable=false, insertable=true, updatable=true) ),
@AttributeOverride(name="classId", column=@Column(name="classId", unique=false, nullable=false, insertable=true, updatable=true) ) } )
public HBatchId getKey() {
return this.key;
}
public void setKey(HBatchId key) {
this.key = key;
}
...
With of course, the requisite key class:
Code:
@Embeddable
public class HBatchId implements java.io.Serializable {
// Fields
private int id;
private int classId;
...
Now in another table,
rp.transfer, I have two columns that refer to the primary key of the batch table: batchId and batchClassId. I have an entity class HTransfer that refelects this (
probably wrong). The data members of the HTransfer entity class with these values are below:
Code:
@Column(name = "batchId")
private Long batchId;
@Column(name = "batchClassId")
private Long batchClassId;
What I need to do know is figure out how to show the one-to-one relationship between HTransfer and HBatch. I've read the section one advanced entity association mappings in chapter 7 of
Java Persistence with Hibernate but it doesn't tell me how to map a composite key. I believe that if I can do this then one of the earlier queries I've used to attempt so solve my earlier question: (
http://forum.hibernate.org/viewtopic.php?p=2405460#2405460) will probably work.
Does anyone out there know how to do this?
Setup Info:
Hibernate version: Core 3.3.1 GA, Annotations 3.4.0
Mapping documents: hibernate.cfg.xml
Regards,
Tim