HIbernate - “引用的外键”从<表2>列数错误。应该是2”错误

发布于 2024-10-16 14:26:11 字数 8792 浏览 8 评论 0原文

我已经寻找这个问题的解决方案有一段时间了。我发现一些线程讨论导致此问题的多对多关系,但我认为这不适用于我的情况,因此我将其发布在新线程中。

我有以下数据库设计: ============================================= =====================================

用户表: PK USER_ID, USER_NAME 唯一, ...

项目表: PK ITEM_ID, FK ITEM_SELLER ->与user.USER_ID的多对一关系, FK ITEM_BUYER ->与user.USER_ID的多对一关系, ...

出价表(用户和项目之间的桥梁): 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:从 GavelDB.Bid 引用 GavelDB.User 的外键列数错误。应该是 2 在 org.hibernate.cfg.annotations.TableBinder.bindFk(TableBinder.java:276) 在 org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:89) 在org.hibernate.cfg.AnnotationConfiguration.processFkSecondPassInOrder(AnnotationConfiguration.java:499) 在 org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:304) 在 org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1286) 在 org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859) 正如我

之前所说,我之前搜索此问题的所有尝试都将我指向复合键,但我已努力在本设计中避免使用它们,所以我不明白这会是怎样的问题。我是休眠新手,所以我还没有从头开始编写文件的知识库。如果有人能给我一些见解,我将不胜感激。提前致谢!

以下是错误中提到的两个文件供您参考:

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;
    }

}

I've been searching for the solution to this question for a while now. I have found a few threads talking about many-to-many relationships causing this issue, but I don't think that applies to my situation so I'm posting this in a new thread.

I have the following database design: ==============================================================================

user table:
PK USER_ID,
USER_NAME UNIQUE,
...

item table:
PK ITEM_ID,
FK ITEM_SELLER -> Many to One relationship with user.USER_ID,
FK ITEM_BUYER -> Many to One relationship with user.USER_ID,
...

bid table (bridge between user and item):
PK BID_ID,
FK BIDDER_ID -> Many to One relationship with user.USER_ID,
FK ITEM_ID -> Many to One relationship with item.ITEM_ID,
...

category table:
PK CAT_ID,
...

item_category table (bridge between category and item):
PK ITEM_CAT_ID,
FK ITEM_ID -> Many to One relationship with item.ITEM_ID,
FK CAT_ID -> Many to One relationship with category.CAT_ID,
...

==============================================================================

I'm attempting to set this up using hibernate through NetBeans. I found a tutorial that walked me through setting up the hibernate.cfg, reveng, and util files and showed me how to generate POJOs using the NetBeans tools. In the tutorial you are then supposed to right click on the cfg file and run HQL queries to ensure everything is working properly. I've tested this process using a simple table (the user table above) and everything works. However, when I try to put everything together I get the following error:


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)

As I stated before all of my previous attempts of searching for this problem have pointed me to composite keys, but I have made an effort to avoid them in this design so I don't see how that would be the problem. I am new to hibernate, so I don't have the knowledge base to write the files from scratch yet. If anyone can offer me some insight it would be greatly appreciated. Thanks ahead of time!

Here are the two files that are mentioned in the error for your reference:

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;
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文