使用注释的Hibernate双向一对一复合键类

时间:2014-06-17 11:34:22

标签: java hibernate annotations one-to-one composite-primary-key

我无法解决这个问题。 如何在Organization和OrganisationAddress之间创建一对一的双向映射?这是我到目前为止所尝试的:

我有两张桌子:

**ORGANISATION**
ORGNO <pk>
BANKNO <pk>

**ORGANISATION_ADDRESS**
ORGNO <pk, fk>
BANKNO <pk, fk>

每个表的Java复合键类:

@Embeddable
public class OrganisationPK implements Serializable {

    @Column(name="ORGNO", length=9)
    private String orgno;

    @Column(name="BANKNO", length=4)
    private String bankno;

    ... getters/setters ...
}

@Embeddable
public class OrganisationAddressPK implements Serializable {

    @Column(name="ORGNO", length=9)
    private String orgno;

    @Column(name="BANKNO", length=4)
    private String bankno;

    ... getters/setters ...
}

这是Organization and OrganisationAddress的Java类:

@Entity
@Table(name="ORGANISATION")
public class Organisation implements Serializable{

    @Id
    private OrganisationPK comp_id;

    /** bi-directional one-to-one association to OrganisationAddress */
    @OneToOne(mappedBy = "organisation", cascade = CascadeType.ALL)
    private OrganisationAddress organisationAddress;
}

@Entity
@Table(name="ORGANISATION_ADDRESS")
public class OrganisationAddress implements Serializable {

    @Id
    @AttributeOverrides({
            @AttributeOverride(name = "orgno", column=@Column(name="ORGNO")),
            @AttributeOverride(name = "bankno", column=@Column(name="BANKNO"))
    })
    private OrganisationAddressPK comp_id;

    /** bi-directional one-to-one association to Organisation */
    @OneToOne(cascade = CascadeType.ALL)
    @PrimaryKeyJoinColumns({
            @PrimaryKeyJoinColumn(name = "ORGNO", referencedColumnName = "ORGNO"),
            @PrimaryKeyJoinColumn(name = "BANKNO", referencedColumnName = "BANKNO")
    })
    private Organisation organisation;

当我运行测试时,它给了我这个错误:

  

org.hibernate.TypeMismatchException:为类org.hibernate.OrganisationAddress提供了错误类型的id。

     

预期:类org.hibernate.OrganisationAddressPK,得到类org.hibernate.OrganisationPK

来自此电话:

private void deleteOrganisation(Organisation organisation){
    Organisation hibOrganisation = (Organisation) getCurrentSession().get(
        Organisation.class, new OrganisationPK(
            organisation.getOrgNo(), BANKNO.getValue()));
...

0 个答案:

没有答案