对于那些使用 Hibernate 的人,您可以(至少从 JPA 2.1 开始)。以下代码完美适用于我的环境(hibernate-entitymanager 4.3.6.Final):
@Entity
@Table(name = "CODIFICATIONS")
@IdClass(CodificationId.class)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = Codification.DISCRIMINATOR_COLUMN, discriminatorType = DiscriminatorType.INTEGER)
public abstract class Codification implements Serializable {
public static final String DISCRIMINATOR_COLUMN = "TABLE_ID";
private static final long serialVersionUID = 1L;
@Column(name = "CODIFICATION_ID")
protected Long codificationId;
@Id
@Column(name = DISCRIMINATOR_COLUMN, insertable = false, updatable = false)
protected Long tableId;
@Id
@Column(name = "CODE_ID", insertable = false, updatable = false)
protected Long codeId;
@Column(name = "LONG_NAME")
protected String longName;
@Column(name = "SHORT_NAME")
protected String shortName;
}
public class CodificationId implements Serializable {
private static final long serialVersionUID = 1L;
private Long tableId;
private Long codeId;
public Long getTableId() {
return tableId;
}
public void setTableId(Long tableId) {
this.tableId = tableId;
}
public Long getCodeId() {
return codeId;
}
public void setCodeId(Long codeId) {
this.codeId = codeId;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((tableId == null) ? 0 : tableId.hashCode());
result = prime * result + ((codeId == null) ? 0 : codeId.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
CodificationId other = (CodificationId) obj;
if (tableId == null) {
if (other.tableId != null)
return false;
} else if (!tableId.equals(other.tableId))
return false;
if (codeId == null) {
if (other.codeId != null)
return false;
} else if (!codeId.equals(other.codeId))
return false;
return true;
}
}
@Entity
@DiscriminatorValue(Status.DISCRIMINATOR_VALUE)
public class Status extends Codification {
public static final String DISCRIMINATOR_VALUE = "2";
private static final long serialVersionUID = 1L;
}
然后我使用以下代码配置与 Status 的关联:
@ManyToOne
@JoinColumnsOrFormulas({
@JoinColumnOrFormula(formula = @JoinFormula(referencedColumnName = Codification.DISCRIMINATOR_COLUMN, value = Status.DISCRIMINATOR_VALUE)),
@JoinColumnOrFormula(column = @JoinColumn(name = "STATUS", referencedColumnName = "CODE_ID")) })
private Status status;