【问题标题】:@ManyToMany - composite PK - Exception: org.hibernate.AnnotationException: A Foreign key refering has the wrong number of column. should be 2@ManyToMany - 复合 PK - 异常:org.hibernate.AnnotationException:引用的外键列数错误。应该是 2
【发布时间】:2015-05-04 04:01:20
【问题描述】:

我遇到了这样的异常...

org.hibernate.AnnotationException: A Foreign key refering com.mysite.model.SellerProduct from com.mysite.model.Product has the wrong number of column. should be 2

我正在开发一个 Web 应用程序,其中场景是:一个产品可以被许多卖家销售。

我正在努力实现:如果我加载产品对象,所有销售该产品的卖家都必须附带产品对象。

表结构:

create table product(
    productid int(12) not null AUTO INCREMENT,
    // other attributes
    PRIMARY KEY(productid)
);

create table seller(
    SellerID int(12) not null AUTO INCREMENT,
    // other attributes
    PRIMARY KEY(SellerID)
);

create table seller_product(
    SellerID int(12) not null AUTO INCREMENT,
    productid int(12) not null AUTO INCREMENT,
    // other attributes
    PRIMARY KEY(SellerID,productid),
    key `productid_fk` (`productid`),
    constraint `productid_fk` FOREIGN KEY (`productid`) REFERENCES `product` (`productid`),
    constraint `sellerid_fk` FOREIGN KEY (`sellerid`) REFERENCES `seller` (`SellerID`)
);

Java 代码如下所示:

Product.java

package com.mysite.model;

// all import statements

@Entity
@Table(name="product")
public class Product {

    @Id
    @Column(name="productid")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int productId;

    @ManyToMany(cascade = CascadeType.ALL, fetch=FetchType.EAGER)
    @JoinTable(name = "SELLER_PRODUCT", 
        joinColumns = { @JoinColumn(name = "productid")}, 
        inverseJoinColumns = { @JoinColumn(name="sellerid") })
    private Set<SellerProduct> seller;

    public Set<SellerProduct> getSeller() {
        return seller;
    }

    public void setSeller(Set<SellerProduct> seller) {
        this.seller = seller;
    }

    public int getProductId() {
        return productId;
    }

    public void setProductId(int productId) {
        this.productId = productId;
    }

    // other variables and getter-setters
}

Seller.java

package com.mysite.model;

// all import packages

@Entity
@Table(name="SELLER")
public class Seller {

    @Id
    @Column(name="SellerID")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int sellerId;

    @ManyToMany (mappedBy="seller")
    private Set<Product> products = new HashSet<Product>();

    public int getSellerId() {
        return sellerId;
    }

    public void setSellerId(int sellerId) {
        this.sellerId = sellerId;
    }

    public Set<Product> getProducts() {
        return products;
    }

    public void setProducts(Set<Product> products) {
        this.products = products;
    }

    // other variables and getter-setters

}

SellerProduct.java

package com.mysite.model;

// all import packages 

@Entity
@Table(name="product")
public class SellerProduct {

    @EmbeddedId
    SellerProductId sellerProductId;

    public SellerProductId getSellerProductId() {
        return sellerProductId;
    }

    public void setSellerProductId(SellerProductId sellerProductId) {
        this.sellerProductId = sellerProductId;
    }

    // other variables and getter setters
}

SellerProductId.java

package com.mysite.model;

// all import packages

@Embeddable
public class SellerProductId { 
    @Column(name="productId")
    private int productId;

    @Column(name="sellerId")
    private int sellerId;

    public int getProductId() {
        return productId;
    }

    public void setProductId(int productId) {
        this.productId = productId;
    }

    public int getSellerId() {
        return sellerId;
    }

    public void setSellerId(int sellerId) {
        this.sellerId = sellerId;
    }    
}

我在某些映射中错了吗? (我尝试在谷歌上搜索但无法成功!)

谁能帮忙????

【问题讨论】:

    标签: hibernate jpa many-to-many composite-primary-key


    【解决方案1】:

    最后,我找到了解决方案。 提示,我来自First LinkSecond Link

    实际上,我正在尝试通过中间表中的一些附加列实现双向多对多关系。 问题出在我的映射中。 我按照First Link 链接中的建议进行了映射。

    【讨论】:

      猜你喜欢
      • 2014-07-25
      • 2017-09-11
      • 2015-01-28
      • 1970-01-01
      • 2014-10-21
      • 1970-01-01
      • 1970-01-01
      • 2022-12-19
      • 1970-01-01
      相关资源
      最近更新 更多