Hi,
If your compound primary key have only surrogate keys, use @EmbeddableId
@Embeddable
public class CompoundIdA implements Serializable {
private Integer field0;
private Integer field1;
private Integer field2;
private Integer field3;
@Column(name="FIELD_0")
public Integer getField0() {
return this.field0;
}
@Column(name="FIELD_1")
public Integer getField1() {
return this.field1;
}
@Column(name="FIELD_2")
public Integer getField2() {
return this.field2;
}
@Column(name="FIELD_3")
public Integer getField3() {
return this.field3;
}
public boolean equals(Object o) {
if(o == null)
return false;
if(!(o instanceof CompoundIdA))
return false;
final CompoundIdA other = (CompoundIdA) o;
if(!(getField0().equals(other.getField0()))
return false;
if(!(getField1().equals(other.getField1()))
return false;
if(!(getField2().equals(other.getField2()))
return false;
if(!(getField2().equals(other.getField2()))
return false;
return true;
}
// hashcode impl
}
In ClassA, we have
@Entity
public class ClassA {
private CompoundIdA compoundIdA;
@EmbeddedId
public CompoundIdA getCompoundIdA() {
return this.CompoundIdA;
}
}
If your compound primary key have both natural and surrogate keys, use again @EmbeddableId
// Let's suppose field0 and field1 are both natural keys
@Entity
public class ClassA {
private CompoundIdA compoundIdA;
private Integer field0;
private Integer field1;
@EmbeddedId
public CompoundIdA getCompoundIdA() {
return this.CompoundIdA;
}
@Column(name="FIELD_0", insertable=false, updateable=false)
public Integer getField0() {
return this.field0;
}
@Column(name="FIELD_1", insertable=false, updateable=false)
public Integer getField1() {
return this.field1;
}
}
Notice you have to set up insertable=false and updateable=false because more than one property share the same column. Otherwise, Hibernate will complain some errors.
If your compound primary key have only natural keys, use @IdClass
@Entity
@IdClass(CompoundIdA.class)
public class ClassA {
private Integer field0;
private Integer field1;
private Integer field2;
private Integer field3;
@Id
@Column(name="FIELD_0")
public Integer getField0() {
return this.field0;
}
@Id
@Column(name="FIELD_1")
public Integer getField1() {
return this.field1;
}
@Id
@Column(name="FIELD_2")
public Integer getField2() {
return this.field2;
}
@Id
@Column(name="FIELD_3")
public Integer getField3() {
return this.field3;
}
}
In ClassB, you can use the same approach as shown above, but if you want to define a @ManyToOne property, you have to set up insertable=false and updateable=false as follows
@Entity
public class ClassB {
private ClassA classA;
@ManyToOne
@JoinColumns ({
@JoinColumn(name="FIELD_0", referencedColumnName="FIELD_0", insertable=false, updateable=false),
@JoinColumn(name="FIELD_1", referencedColumnName="FIELD_1", insertable=false, updateable=false),
@JoinColumn(name="FIELD_2", referencedColumnName="FIELD_2", insertable=false, updateable=false),
@JoinColumn(name="FIELD_3", referencedColumnName="FIELD_3", insertable=false, updateable=false)
})
public ClassA getClassA() {
return this.classA;
}
}
regards,