简体   繁体   中英

AnnotationException: A Foreign key refering dayHibernate … wrong number of column. should be 3

I want to get data from standard employees db MySQL from only two table:

EMPLOYEES {
   EMP_NO, -- PK
   BIRTH_DATE,
   ....
}

TITLES {
   EMP_NO, -- PK, and FK to EMPLOYEES
   TITLE, -- PK
   FROM_DATE, -- PK
   TO_DATE,
   ....
}

This is many columns key:

@Embeddable
public class TitleId implements Serializable {
    @Column(name = "emp_no")
    private long empNumber;

    @Column(name = "title")
    private String title;

    @Column(name = "from_date")
    private java.sql.Date fromDate;

// constructor, hascode, setter, getter
// ................

}

And there are entities:

First entity

@Entity(name = "Title")
@Table(name = "titles")
public class Title implements Serializable {

    @EmbeddedId
    private TitleId titleId;

    @Column(name = "to_date")
    private java.sql.Date toDate;

    @OneToMany(mappedBy = "title")
    private List<Employee> employees = new ArrayList<>();

// constructor, setter, getter

}

Second entity

@Entity
@Table(name = "employees")
public class Employee {

    @Id
    @Column(name = "emp_no")
    private long empNo;

    @ManyToOne(optional = false)
    @JoinColumn(name = "emp_no", insertable = false, updatable = false)
    private Title title;

 //   setter, getter, constructor, other coluns
}

And EmployeeDao is:

    List employees = session.createQuery("FROM Employee").list();
    for (Iterator iterator = employees.iterator(); iterator.hasNext();){
        Employee employee = (Employee) iterator.next();
        System.out.print("First Name: " + employee.getFirstName());
        System.out.print("  Last Name: " + employee.getLastName());
    }

And when I try to get this DATA, there is the next exception

AnnotationException: A Foreign key refering dayHibernate.Title from dayHibernate.Employee has the wrong number of column. should be 3

How to get data from this two table and avoid exception? help me plese

You can't use @JoinColumn with @ManyToOne when using a Composite Key at the referred entity. The exception says it all really, Employee is trying to refer to Title with only emp_no but it has two more fields in the composite key.

I think it is a design problem of the whole entity model in this case. Since TitleId contains the primary key of Employee the Title entity should not be able to have a Many To One relationsship with Employee but the Employee should be able to have many titles.

Since the Emp_no is in the composite key and is a foreign key you should be able to use @MapsId, see for example this post .

This works for me:

Employee

@Entity @Table(name = "employees") public class Employee {

    @Id
    @Column(name = "emp_no")
    private long empNo;

    @OneToMany(mappedBy="employee")
    private List<Title> titles = new ArrayList<>();

    ... }

TitleId

@Embeddable
public class TitleId implements Serializable {

    private static final long serialVersionUID = 1L;

    @Column(name = "emp_no")
    private long empNumber;

    @Column(name = "title")
    private String title;

    @Column(name = "from_date")
    private java.sql.Date fromDate;
}

Title

@Entity(name = "Title")
@Table(name = "titles")
public class Title implements Serializable {

    @EmbeddedId
    private TitleId titleId;

    @Column(name = "to_date")
    private java.sql.Date toDate;

    @MapsId("empNumber")
    @JoinColumn(name = "emp_no")
    @ManyToOne
    private Employee employee;

}

Add @JoinColumns to Employee table

@Entity
@Table(name = "employees")
public class Employee {

    @Id
    @Column(name = "emp_no")
    private long empNo;

    @ManyToOne(optional = false)
      @JoinColumns ({
      @JoinColumn(name="emp_number", referencedColumnName = "emp_no"),
      @JoinColumn(name="title", referencedColumnName = "title"),
      @JoinColumn(name="from_date", referencedColumnName = "from_date")
    })
    private Title title;

 //   setter, getter, constructor, other coluns
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM