[英]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: 我只想从两个表中从标准员工db MySQL中获取数据:
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: 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());
}
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
帮我pl
You can't use @JoinColumn with @ManyToOne when using a Composite Key at the referred entity. 在所引用实体上使用复合键时,不能将@JoinColumn与@ManyToOne一起使用。 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.
异常确实说明了一切,Employee试图仅使用emp_no来引用Title,但复合键中还有两个字段。
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.
由于TitleId包含Employee的主键,因此Title实体应该不能与Employee建立多对一关系,但是Employee应该可以拥有许多标题。
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 . 由于Emp_no在复合键中,并且是外键,因此您应该可以使用@MapsId, 请参阅此示例 。
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 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 将@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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.