Hibernate:将外键映射到复合键的一部分

时间:2018-04-19 06:51:52

标签: java hibernate jpa orm

我有两个表TABLE_ATABLE_B,结构如下:

TABLE_A

|------------|-----------|
| COMPANY_ID | (column)  |
|------------|-----------|
| SERVICE_ID | (column)  | 
|------------|-----------|
| .... other columns ..  |
|------------|-----------|

**COMPANY_ID and SERVICE_ID are composite Key**
-------------

TABLE_B:

|------------|-------------|
| ID         | (column)  PK|
|------------|-------------|
| COMPANY_ID | (column)    |
|------------|-------------|
| .... other columns ..    |
|------------|-------------|

所以我想将TABLE_B.COMPANY_ID(视为外键)映射到TABLE_A.COMPANY_ID(复合主键的一部分)

我创建了以下代码:

Embeddable类:

@Embeddable
public class CompanyId implements Serializable {
/**
 * 
 */
private static final long serialVersionUID = 1L;

private Long companyId;
private Long serviceId;

/**
 * Zero-args constructor 
 */
public CompanyId() {

}

/**
 * 
 * @param companyId
 * @param serviceId
 */
public CompanyId(Long companyId, Long serviceId) {

    this.companyId = companyId;
    this.serviceId = serviceId;
}

@Column(name = "COMPANY_ID")
public Long getCompanyId() {
    return companyId;
}

public void setCompanyId(Long companyId) {
    this.companyId = companyId;
}

@Column(name = "SERVICE_ID")
public Long getServiceId() {
    return serviceId;
}

public void setServiceId(Long serviceId) {
    this.serviceId = serviceId;
}

@Override
public int hashCode() {

    return Objects.hash(getCompanyId(), getServiceId());
}

@Override
public boolean equals(Object obj) {

    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    CompanyId other = (CompanyId) obj;
    if (companyId == null) {
        if (other.companyId != null)
            return false;
    } else if (!companyId.equals(other.companyId))
        return false;
    if (serviceId == null) {
        if (other.serviceId != null)
            return false;
    } else if (!serviceId.equals(other.serviceId))
        return false;
    return true;
}
}

TABLE_A的类模型

@Entity
@Table(name = "TABLE_A")
public class TableA implements Serializable{
/**
 * 
 */
private static final long serialVersionUID = 1L;

private CompanyId id;


private Set<TableB> tableB = new HashSet<TableB>();

    @EmbeddedId
public CompanyId getId() {
    return id;
}

public void setId(CompanyId id) {
    this.id = id;
}


    @OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "COMPANY_ID", referencedColumnName = "COMPANY_ID")
public Set<TableB> getTableB() {
    return tableB;
}

public void setTableB(Set<TableB> tableB) {
    this.tableB = tableB;
}
}

TABLE_B的类模型

@Entity
@Table(name = "TABLE_B")
public class TableB implements Serializable{

private Integer id;
private Set<TableA> tableA = new HashSet<TableA>();

@Id
@Column(name = "ID")
public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

@MapsId("companyId")
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "COMPANY_ID", referencedColumnName = "COMPANY_ID",   insertable = false, updatable = false)
public Set<TableA> getTableA() {
    return tableA;
}

public void setTableA(Set<TableA> tableA) {
    this.tableA = tableA;
}
}

但是这段代码无效,我收到了以下错误:

org.hibernate.AnnotationException: Unable to map collection 
com.TableA.tableB

Caused by: org.hibernate.AnnotationException: 
referencedColumnNames(COMPANY_ID) of com.TableB referencing com.TableA not 
mapped to a single property

所以我的问题是:

如上所述,可以将单个外键映射到复合键的一部分吗?我是如何实现它的?

谢谢

0 个答案:

没有答案