@manytomy-composite PK-Exception:org.hibernate.AnnotationException:外键引用的列数错误。应该是2
我得到了一个这样的例外@manytomy-composite PK-Exception:org.hibernate.AnnotationException:外键引用的列数错误。应该是2,hibernate,jpa,many-to-many,composite-primary-key,Hibernate,Jpa,Many To Many,Composite Primary Key,我得到了一个这样的例外 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应用程序,其中的场景是:一个产品可以被许多卖家出售。 我正在努力实现:如果我加载产品对象,所有销售该产品的卖家都必须附带产品对象
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
}
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;
}
}
在某些映射中我错了吗??(我尝试在谷歌上搜索,但没有成功!)
有人能帮忙吗???最后,我找到了解决办法。
提示,我从
实际上,我正试图通过在中间表中添加一些列来实现双向多对多关系。
问题在于我的映射。
我完全按照链接中的建议进行了映射
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;
}
}