复合键之间的多对一关系

时间:2014-05-02 19:39:05

标签: java spring hibernate spring-mvc jpa

我正在构建一个带有hibernate的spring mvc应用程序,而JPA需要建模一些底层MYSQL数据表,每个表都有具有相同两种数据类型的复合键,因此每个表都有自己的复合键类,即使所有复合键基于相同的两种数据类型,具有完全相同的属性名称。当我尝试编译应用程序时,我收到了一个hibernate映射错误,我想知道这是否是因为hibernate可能无法将不同的主键类等同起来。 有人可以告诉我如何解决这个问题,以便我的应用可以编译吗?

以下是我的Description类的一部分,它根据相应的复合主键类在ManyToOneDescription类之间建立Concept关系:

@ManyToOne
@JoinColumn(name="descriptionPK", referencedColumnName = "conceptPK")
private Concept concept;

以下是我收到的错误:

Caused by: org.hibernate.MappingException:  
Unable to find column with logical name:  
conceptPK in org.hibernate.mapping.Table(sct2_concept) and its related supertables and secondary tables

ConceptPK的代码是:

@Embeddable
class ConceptPK implements Serializable {

@Column(name="id", nullable=false)
protected BigInteger id;

@Column(name="effectiveTime", nullable=false)
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
private DateTime effectiveTime;

public ConceptPK() {}
public ConceptPK(BigInteger bint, DateTime dt) {
    this.id = bint;
    this.effectiveTime = dt;
}

/** getters and setters **/
public DateTime getEffectiveTime(){return effectiveTime;}
public void setEffectiveTime(DateTime ad){effectiveTime=ad;}

public void setId(BigInteger id) {this.id = id;}
public BigInteger getId() {return id;}

@Override
public boolean equals(Object obj) { 
    if (this == obj) return true;
    if (obj == null) return false;
    if (getClass() != obj.getClass()) return false;
    final ConceptPK other = (ConceptPK) obj;
    if (effectiveTime == null) {
        if (other.effectiveTime != null) return false;
    } else if (!effectiveTime.equals(other.effectiveTime)) return false;
    if (id == null) {
        if (other.id != null) return false;
    } else if (!id.equals(other.id)) return false;
    return true;
}

@Override
public int hashCode() { 
    int hash = 3;
    hash = 53 * hash + ((effectiveTime == null) ? 0 : effectiveTime.hashCode());
    hash = 53 * hash + ((id == null) ? 0 : id.hashCode());
    return hash;
}
}

DescriptionPK的代码是:

@Embeddable
class DescriptionPK implements Serializable {
@Column(name="id", nullable=false)
protected BigInteger id;

@Column(name="effectiveTime", nullable=false)
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
private DateTime effectiveTime;

public DescriptionPK() {}
public DescriptionPK(BigInteger bint, DateTime dt) {
    this.id = bint;
    this.effectiveTime = dt;
}

/** getters and setters **/
public DateTime getEffectiveTime(){return effectiveTime;}
public void setEffectiveTime(DateTime ad){effectiveTime=ad;}

public void setId(BigInteger id) {this.id = id;}
public BigInteger getId() {return id;}

@Override
public boolean equals(Object obj) { 
    if (this == obj) return true;
    if (obj == null) return false;
    if (getClass() != obj.getClass()) return false;
    final DescriptionPK other = (DescriptionPK) obj;
    if (effectiveTime == null) {
        if (other.effectiveTime != null) return false;
    } else if (!effectiveTime.equals(other.effectiveTime)) return false;
    if (id == null) {
        if (other.id != null) return false;
    } else if (!id.equals(other.id)) return false;
    return true;
}

@Override
public int hashCode() { 
    int hash = 3;
    hash = 53 * hash + ((effectiveTime == null) ? 0 : effectiveTime.hashCode());
    hash = 53 * hash + ((id == null) ? 0 : id.hashCode());
    return hash;
}
}

1 个答案:

答案 0 :(得分:1)

您需要更改@ManyToOne注释以使用多个列,如下所示,如果所有属性相同,您也不需要创建重复的两个可嵌入类ConceptPK和DescriptionPK,只需创建一个EmbeddablePK并在​​两个实体中使用。

@OneToMany(mappedBy = "concept", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    public List<Description> descriptions = new LinkedList<Description>();

描述类:

@ManyToOne
@JoinColumns({ @JoinColumn(name = "A_COLUMN", referencedColumnName = "A_COLUMN", insertable = false, updatable = false),
        @JoinColumn(name = "B_COLUMN", referencedColumnName = "B_COLUMN", insertable = false, updatable = false),
})
public Concept concept;