from has the wrong number of columns. There must be an error of 2" I have been look...">

HIbernate - "The foreign key referencing <table1> from <table2> has the wrong number of columns. There must be an error of 2"

I have been looking for a solution to this issue for a while. I found several threads talking about many-to-many relationships causing this problem, but I don’t think this is relevant to my situation, so I am posting this to a new thread.

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

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

position table: PK ITEM_ID, FK ITEM_SELLER → Many-to-One Relationships with user.USER_ID, FK ITEM_BUYER → Many-to-One Relationships with user.USER_ID, ...

betting table (bridge between the user and the item): PK BID_ID, FK BIDDER_ID → Many-to-One Relationships with user.USER_ID, FK ITEM_ID → Many-to-One Relationships 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 Relationships with item.ITEM_ID, FK CAT_ID → Many-to-One Relationships with the Category. CAT_ID, ...

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

NetBeans. , hibernate.cfg, reveng util , POJO NetBeans. 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)

, , , , , . , , . - , . !

, :

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

}
+3

Source: https://habr.com/ru/post/1791291/


All Articles