我的数据库中有这两个表:
CREATE TABLE classroom_trainee (
id BIGINT(20) NOT NULL AUTO_INCREMENT,
trainee_id BIGINT(20) NOT NULL,
classroom_id BIGINT(20) NOT NULL,
completed BOOLEAN,
notes VARCHAR(255),
result INT(3),
feedback BOOLEAN DEFAULT 0,
PRIMARY KEY (id),
FOREIGN KEY (trainee_id)
REFERENCES user (id),
FOREIGN KEY (classroom_id)
REFERENCES classroom (id)
);
CREATE TABLE trainee_presence (
id BIGINT(20) NOT NULL AUTO_INCREMENT,
classroom_trainee_id BIGINT(20) NOT NULL,
day INT(2) NOT NULL,
presence BOOLEAN,
notes VARCHAR(255),
PRIMARY KEY (id),
FOREIGN KEY (classroom_trainee_id)
REFERENCES classroom_trainee (id)
);
这是我拥有的实体:
UserEntity.java:
....
public class UserEntity implements Serializable {
...
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "manager_id")
private UserEntity manager;
@Column(name = "email", nullable = false, unique = true)
private String email;
@Column(name = "forename")
private String forename;
@Column(name = "surname")
private String surname;
...
ClassroomTraineeEntity.java
....
public class ClassroomTraineeEntity {
.....
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@ManyToOne
@JoinColumn(name="trainee_id")
private UserEntity trainee;
@ManyToOne
@JoinColumn(name="classroom_id")
private ClassroomEntity classroom;
....
TraineePresenceEntity.java
....
public class TraineePresenceEntity {
.......
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@ManyToOne
@JoinColumn(name="classroom_trainee_id")
private ClassroomTraineeEntity classroomTrainee;
@Column(name = "day")
private Integer day;
@Column(name = "presence")
private Boolean presence;
...
在TraineePresenceEntity中我使用
@Column(name="classroom_trainee_id")
private Integer classroomTraineeId;
一切正常,花花公子,但有了ClassroomTraineeEntity对象,我得到了这个例外:
引起:org.hibernate.MappingException:外键(FKn85t08ggb5n6ail459koe0sm8:trainee_presence [classroom_trainee_id]))必须与引用的主键具有相同的列数(classroom_trainee [classroom_id,trainee_id])
任何人都知道如何解决这个问题?