HIbernate - "A Foreign Key referring <table1> from <table2> has the wrong number of column. Should be 2" error

2.2k views Asked by At

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

}
0

There are 0 answers