java hibernate @ManyToMany with composite key and common field org.hibernate.MappingException:collection

时间:2018-01-29 15:40:55

标签: java hibernate many-to-many

我有这张表:

  • tbShop with fields

    • idShop
    • idAppli
    • 名称
  • 带字段的tbCategorie

    • idCategorie
    • idAppli
    • categName
  • tbCategorieShop使用tbShop和tbCategorie之间的多种关系与字段

    • idShop
    • idAppli
    • idCategorie

实体店:

@Entity
@Table(name = "tbShop")
public class Shop implements Serializable {
    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected ShopPK shopPK;
    @Column(name = "commNom")
    private String name;

    @ManyToMany( fetch=FetchType.EAGER)
    @Cascade({CascadeType.SAVE_UPDATE, CascadeType.ALL})
    @JoinTable(name = "tbCategorieShop",
            joinColumns = {
                @JoinColumn(name = "idAppli", referencedColumnName = "idAppli")
                , @JoinColumn(name = "idShop", referencedColumnName = "idShop")}
            , inverseJoinColumns = {
                @JoinColumn(name = "idCategorie", referencedColumnName = "idCategorie")
                , @JoinColumn(name = "idAppli", referencedColumnName = "idAppli")}

    )
    private List<Categorie> tbCategorie ;

实体分类:

@Entity
@Table(name = "tbCategorie")
public class Categorie implements Serializable {

    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected CategoriePK categoriePK;

    @Column(name = "categName")
    private String categName;

    @JoinTable(name = "tbCategorieShop", joinColumns = {
        @JoinColumn(name = "idCategorie", referencedColumnName = "idCategorie")
        , @JoinColumn(name = "idAppli", referencedColumnName = "idAppli")}
           , inverseJoinColumns = {
        @JoinColumn(name = "idAppli", referencedColumnName = "idAppli")
        , @JoinColumn(name = "idShop", referencedColumnName = "idShop")}
    )
    @ManyToMany
    private Collection<Shop> shopCollection;
    ... 

Primary Key ShopPk

@Embeddable
public class ShopPK implements Serializable {

    @Basic(optional = false)
    @Column(name = "idAppli")
    private int idAppli;
    @Basic(optional = false)
    @Column(name = "idShop")
    private String idShop;
... }   

Primary Key CategoriePk

@Embeddable
public class CategoriePK implements Serializable {
    @Basic(optional = false)
    @Column(name = "idCategorie")
    private int idCategorie;
    @Basic(optional = false)
    @Column(name = "idAppli")
    private int idAppli;

我有错误

Caused by: org.hibernate.MappingException: Repeated column in mapping for collection: hello.Categorie.shopCollection column: id_appli

对于ManyToMany关系,是否有可能在这个实体中有共同的领域?

1 个答案:

答案 0 :(得分:0)

您面临的问题是因为字段“idAppli”在Shop和Categorie实体中重复出现。 您可以将此字段标记为insertable = false,并在其中一个位置将updatable = false标记为应解决问题