AnnotationException:引用dayHibernate的外键...列数错误。应该是3

时间:2019-01-03 21:00:49

标签: java hibernate

我只想从两个表中从标准员工db MySQL获取数据:

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

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

这是许多列关键字:

@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
// ................

}

还有实体:

第一个实体

@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

}

第二个实体

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

而EmployeeDao是:

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

当我尝试获取此数据时,会有下一个异常

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

如何从这两个表中获取数据并避免异常?帮我吃饭

2 个答案:

答案 0 :(得分:0)

在所引用实体上使用复合键时,不能将@JoinColumn与@ManyToOne一起使用。异常完全说明了这一点,Employee试图仅使用emp_no来引用Title,但复合键中还有两个字段。

在这种情况下,我认为这是整个实体模型的设计问题。由于TitleId包含Employee的主键,因此Title实体应该不能与Employee建立多对一关系,但是Employee应该可以拥有许多标题。

由于Emp_no在复合键中并且是外键,因此您应该可以使用@MapsId,see for example this post

这对我有用:

员工

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

标题

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

}

答案 1 :(得分:0)

将@JoinColumns添加到Employee表

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