【发布时间】: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