Hibernate 中带注解的复合主键

Composite primary keys in Hibernate with annotations

我正在尝试使用 Hibernate + 注释来处理以下关系:

但是,我得到这个错误:

"A foreign key referring *.street from *.house has the wrong number of column: should be 2".

这是我的代码:

房子

@Entity
@Table(name = "HOUSE")
@GenericGenerator(name = "pkgenerator", strategy = "increment")
public class House extends Entity {
private static final long serialVersionUID = 1L;

private Integer codHouse;
private Group codGroup;
private Street codStreet;

@Transient
public Integer getPrimaryKey() {
    return codHouse;
}
@Transient
public void setPrimaryKey(Integer primaryKey) {
    setCodHouse(primaryKey);
}

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY, generator = "pkgenerator")
@Column(name="CODHOUSE", nullable=false, length=9)
public Integer getCodHouse() {
    return codHouse;
}
public void setCodHouse(Integer codHouse) {
    this.codHouse = codHouse;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn (name="CODGROUP", nullable=false)
public Group getCodGroup() {
    return codGroup;
}
public void setCodGroup(Group codGroup) {
    this.codGroup = codGroup;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn (name="CODSTREET", nullable=false)
public Street getStreet() {
    return codStreet;
}
public void setStreet(Street codStreet) {
    this.codStreet = codStreet;
}

街道

@Entity
@Table(name = "STREET")
@GenericGenerator(name = "pkgenerator", strategy = "increment")
@AssociationOverrides({
    @AssociationOverride(name = "primaryKey.codGroup", 
        joinColumns = @JoinColumn(name = "CODGROUP")),
    @AssociationOverride(name = "primaryKey.codStreet",
        joinColumns = @JoinColumn(name = "CODSTREET"))
})
public class Street extends Entity implements Serializable{

    private static final long serialVersionUID = 1L;

    private StreetPK primaryKey;
    private String name;

    @EmbeddedId
    public StreetPK getPrimaryKey() {
        return primaryKey;
    }
    public void setPrimaryKey(StreetPK primaryKey) {
        this.primaryKey = primaryKey;
    }

    @Column(name = "NAME", nullable = true, length = 30)
    public String getName() {
        return name;
    }
    public void setName(String nname) {
        this.name = name;
    }
}

街头PK

@Embeddable
public class StreetPK implements Serializable {

    private static final long serialVersionUID = 1L;

    private Group codGroup;
    private Integer codStreet;

    public StreetPK() {}

    public StreetPK(Group codGroup, Integer codStreet){
        this.codGroup = codGroup;
        this.codStreet = codStreet;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="CODGROUP", nullable=false)
    public Group getCodGroup() {
        return codGroup;
    }
    public void setCodGroup(Group codGroup) {
        this.codGroup = codGroup;
    }

    @JoinColumn(name="CODSTREET", nullable=false)
    public Integer getCodStreet() {
        return codStreet;
    }

    public void setCodStreet(Integer codStreet) {
        this.codStreet = codStreet;
    }
}

@Entity
@Table(name = "GROUP")
public class Group extends Entity implements Serializable{

    private static final long serialVersionUID = 1L;

    private Integer codGroup;

    @Transient
    public Integer getPrimaryKey() {
        return codGroup;
    }
    @Transient
    public void setPrimaryKey(Integer primaryKey) {
        setCodGroup(primaryKey);
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY, generator = "pkgenerator")
    @Column(name="CODGROUP", nullable=false, length=9)
    public Integer getCodGroup() {
        return codGroup;
    }
    public void setCodGroup(Integer codGroup) {
        this.codGroup = codGroup;
    }
}

问题出在 StreetPK 中,如果我不在此处包含 codGroup,错误就会消失。 提前致谢。

在注释 getStreet() getter 时,您需要将带有两个 @JoinColumn 注释的 @JoinColumns 注释放在指定组合 Street 键的两列中:

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns ({
    @JoinColumn(name = "CODSTREET", referencedColumnName = "CODSTREET", nullable = false),
    @JoinColumn(name = "CODGROUP", referencedColumnName = "CODGROUP", nullable = false)
})
public Street getStreet() { ... }