Java Hibernate Annotation异常,referenceColumnNames未映射到单个属性

时间:2017-08-30 01:24:57

标签: java hibernate jpa

我想获取每个客户卡的客户帐户列表,然后获取每个客户帐户的产品描述。

我使用Java Hibernate收到以下错误:

            org.hibernate.AnnotationException: referencedColumnNames(ProductID) of query.dao.ProductDescription.productDescription referencing query.dao.ClientAccount not mapped to a single property

请参阅ClientCard,ClientAccount和ProductDescription。

ClientCard.java

@Table(name="ClientCard")
@Entity
@Getter
@Setter
public class ClientCard {

    @Id
    @Column(name = "ClientID", insertable = false, updatable = false)
    private int clientId;

    @Column(name = "ClientCardNumber", insertable = false, updatable = false)
    private String clientCardNumber;

    @Column(name = "MaskAccounts", insertable = false, updatable = false)
    private String maskAccounts;

    @OneToMany
    @JoinColumn (name = "ClientID", referencedColumnName = "ClientID", insertable = false, updatable = false)
    private List<ClientAccount> clientAccounts;

}

ClientAccount.java

@Table(name="ClientAccount")
@Entity
@Getter
@Setter
public class ClientAccount {

    @EmbeddedId
    private ClientAccountKey key;

    @Column(name="NickName", insertable = false, updatable = false)
    private String nickName;

    @Column(name="HideInSummary", insertable = false, updatable = false)
    private String hideInSummary;

    @Column(name = "MobileNickName", insertable = false, updatable = false)
    private String mobileNickName;

    @Column(name = "HideInMobile", insertable = false, updatable = false)
    private String hideInMobile;

    @OneToMany
    @JoinColumn(name = "ProductID", referencedColumnName = "ProductID", insertable = false, updatable = false)
    private List<ProductDescription> productDescription;

}

ProductDescription.java

@Table(name="ProductDescription")
@Entity
@Getter
@Setter
public class ProductDescription {

    @EmbeddedId
    private ProductDescriptionKey key;

    @Column(name = "Description", insertable = false, updatable = false)
    private String description;
}

ClientAccountKey.java

@Setter
@Getter
@EqualsAndHashCode
@Embeddable
public class ClientAccountKey implements Serializable {

    @Column(name = "ClientID", insertable = false, updatable = false)
    private int clientId;

    @Column(name="AccountID", insertable = false, updatable = false)
    private String accountId;

    @Column(name="ProductID", insertable = false, updatable = false)
    private String productId;

    @Column(name="TransitID", insertable = false, updatable = false)
    private String transitId;
}

ProductDescriptionKey.java

@Setter
@Getter
@EqualsAndHashCode
@Embeddable
public class ProductDescriptionKey implements Serializable {

    @Column(name = "ProductID", insertable = false, updatable = false)
    private String ProductID;

    @Column(name = "LanguageID", insertable = false, updatable = false)
    private String languageID;

}

任何帮助都会感激不尽。

1 个答案:

答案 0 :(得分:0)

现在在您的代码中:

@OneToMany
@JoinColumn(name = "ProductID", referencedColumnName = "ProductID", insertable = false, updatable = false)
private List<ProductDescription> productDescription;

由于您的ProductDescription表具有复合键,因此您需要有两列要加入,因此您应将其更改为:

 @OneToMany
 @JoinTable(name = "ProductDescription", joinColumns = { @JoinColumn(name = "ProductID") }, inverseJoinColumns = { @JoinColumn(name = "LanguageID") })
 private List<ProductDescription> productDescription;