我一直在寻找这个问题的解决方案。我发现一些线程正在讨论导致此问题的多对多关系,但我不认为这适用于我的情况,所以我将其发布到一个新线程中。
我有以下数据库设计:======================================== ======================================
用户表: PK USER_ID, USER_NAME UNIQUE, ...
项目表: PK ITEM_ID, FK ITEM_SELLER - >与user.USER_ID的多对一关系, FK ITEM_BUYER - >与user.USER_ID的多对一关系, ...
bid表(用户和项目之间的桥梁): PK BID_ID, FK BIDDER_ID - >与user.USER_ID的多对一关系, FK ITEM_ID - >与item.ITEM_ID的多对一关系, ...
类别表: PK CAT_ID, ...
item_category表(类别和项目之间的桥梁): PK ITEM_CAT_ID, FK ITEM_ID - >与item.ITEM_ID的多对一关系, FK CAT_ID - >与category.CAT_ID的多对一关系, ...
=============================================== ===============================
我正在尝试通过NetBeans使用hibernate来设置它。我找到了一个教程,介绍了如何设置hibernate.cfg,reveng和util文件,并向我展示了如何使用NetBeans工具生成POJO。在本教程中,您应该右键单击cfg文件并运行HQL查询以确保一切正常。我已经使用一个简单的表(上面的用户表)测试了这个过程,一切正常。但是,当我尝试将所有内容放在一起时,我收到以下错误:
org.hibernate.AnnotationException: A Foreign key refering GavelDB.User from GavelDB.Bid has the wrong number of column. should be 2
at org.hibernate.cfg.annotations.TableBinder.bindFk(TableBinder.java:276)
at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:89)
at org.hibernate.cfg.AnnotationConfiguration.processFkSecondPassInOrder(AnnotationConfiguration.java:499)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:304)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1286)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859)
正如我之前所说的,我之前尝试搜索此问题的所有尝试都指向了复合键,但我已经努力在这个设计中避免它们,所以我不明白这是怎么回事。我是hibernate的新手,所以我还没有从头开始编写文件的知识库。如果有人能给我一些见解,我将不胜感激。提前谢谢!
以下是错误中提到的两个文件供您参考:
BID.JAVA
package GavelDB;
// Generated Feb 10, 2011 12:41:04 PM by Hibernate Tools 3.2.1.GA
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/**
* Bid generated by hbm2java
*/
@Entity
@Table(name="bid"
,catalog="gavel1"
)
public class Bid implements java.io.Serializable {
private Integer bidId;
private User user;
private Item item;
private Date bidDate;
private double bidAmt;
private Double bidMax;
public Bid() {
}
public Bid(User user, Item item, Date bidDate, double bidAmt) {
this.user = user;
this.item = item;
this.bidDate = bidDate;
this.bidAmt = bidAmt;
}
public Bid(User user, Item item, Date bidDate, double bidAmt, Double bidMax) {
this.user = user;
this.item = item;
this.bidDate = bidDate;
this.bidAmt = bidAmt;
this.bidMax = bidMax;
}
@Id @GeneratedValue(strategy=IDENTITY)
@Column(name="BID_ID", unique=true, nullable=false)
public Integer getBidId() {
return this.bidId;
}
public void setBidId(Integer bidId) {
this.bidId = bidId;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="BIDDER_ID", nullable=false)
public User getUser() {
return this.user;
}
public void setUser(User user) {
this.user = user;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="ITEM_ID", nullable=false)
public Item getItem() {
return this.item;
}
public void setItem(Item item) {
this.item = item;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name="BID_DATE", nullable=false, length=19)
public Date getBidDate() {
return this.bidDate;
}
public void setBidDate(Date bidDate) {
this.bidDate = bidDate;
}
@Column(name="BID_AMT", nullable=false, precision=22, scale=0)
public double getBidAmt() {
return this.bidAmt;
}
public void setBidAmt(double bidAmt) {
this.bidAmt = bidAmt;
}
@Column(name="BID_MAX", precision=22, scale=0)
public Double getBidMax() {
return this.bidMax;
}
public void setBidMax(Double bidMax) {
this.bidMax = bidMax;
}
}
ITEM.JAVA
package GavelDB;
// Generated Feb 10, 2011 12:41:04 PM by Hibernate Tools 3.2.1.GA
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
/**
* User generated by hbm2java
*/
@Entity
@Table(name="user"
,catalog="gavel1"
, uniqueConstraints = @UniqueConstraint(columnNames="USER_NAME")
)
public class User implements java.io.Serializable {
private Integer userId;
private String userName;
private String pswd;
private String email;
private String street;
private String city;
private String state;
private Integer zip;
private Integer phone;
private Set<Item> itemsForItemSeller = new HashSet<Item>(0);
private Set<Item> itemsForItemBuyer = new HashSet<Item>(0);
private Set<Bid> bids = new HashSet<Bid>(0);
public User() {
}
public User(String userName) {
this.userName = userName;
}
public User(String userName, String pswd, String email, String street, String city, String state, Integer zip, Integer phone, Set<Item> itemsForItemSeller, Set<Item> itemsForItemBuyer, Set<Bid> bids) {
this.userName = userName;
this.pswd = pswd;
this.email = email;
this.street = street;
this.city = city;
this.state = state;
this.zip = zip;
this.phone = phone;
this.itemsForItemSeller = itemsForItemSeller;
this.itemsForItemBuyer = itemsForItemBuyer;
this.bids = bids;
}
@Id @GeneratedValue(strategy=IDENTITY)
@Column(name="USER_ID", unique=true, nullable=false)
public Integer getUserId() {
return this.userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
@Column(name="USER_NAME", unique=true, nullable=false)
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@Column(name="PSWD", length=30)
public String getPswd() {
return this.pswd;
}
public void setPswd(String pswd) {
this.pswd = pswd;
}
@Column(name="EMAIL")
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
@Column(name="STREET")
public String getStreet() {
return this.street;
}
public void setStreet(String street) {
this.street = street;
}
@Column(name="CITY")
public String getCity() {
return this.city;
}
public void setCity(String city) {
this.city = city;
}
@Column(name="STATE")
public String getState() {
return this.state;
}
public void setState(String state) {
this.state = state;
}
@Column(name="ZIP")
public Integer getZip() {
return this.zip;
}
public void setZip(Integer zip) {
this.zip = zip;
}
@Column(name="PHONE")
public Integer getPhone() {
return this.phone;
}
public void setPhone(Integer phone) {
this.phone = phone;
}
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="userByItemSeller")
public Set<Item> getItemsForItemSeller() {
return this.itemsForItemSeller;
}
public void setItemsForItemSeller(Set<Item> itemsForItemSeller) {
this.itemsForItemSeller = itemsForItemSeller;
}
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="userByItemBuyer")
public Set<Item> getItemsForItemBuyer() {
return this.itemsForItemBuyer;
}
public void setItemsForItemBuyer(Set<Item> itemsForItemBuyer) {
this.itemsForItemBuyer = itemsForItemBuyer;
}
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="user")
public Set<Bid> getBids() {
return this.bids;
}
public void setBids(Set<Bid> bids) {
this.bids = bids;
}
}