我阅读了有关此错误的所有主题,但不了解我如何在我的代码中修复它。请帮帮我。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [ru/itpark/config/PersistenceConfig.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: A Foreign key refering ru.itpark.model.UserAnswers from ru.itpark.model.TheoreticTask has the wrong number of column. should be 2
用户类
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name="users")
public class User {
@Id
//@EmbeddedId
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column
private String userName;
@Column
private String password;
@Column
private String name;
@OneToMany
@JoinTable(name = "user_answers",
joinColumns =
@JoinColumn(name = "user_id"),
inverseJoinColumns =
@JoinColumn(name = "id"))
private List<UserAnswers> userAnswers;
}
PracticTask类
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name="practic_tasks")
public class PracticTask {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column( name = "theme_id")
private int themeId;
@Column
private String question;
@Column
private String answer;
@OneToMany
@JoinTable(name = "user_answers",
joinColumns =
@JoinColumn(name = "practic_task_last"),
inverseJoinColumns =
@JoinColumn(name = "id"))
private List<UserAnswers> userAnswers;
}
主题课
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name="themes")
public class Theme {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
int id;
@Column
private String name;
@OneToMany
@JoinTable(name = "practic_tasks",
joinColumns =
@JoinColumn(name = "theme_id"),
inverseJoinColumns =
@JoinColumn(name = "id"))
private List<PracticTask> practicTaskList;
@OneToMany
@JoinTable(name = "theoretic_tasks",
joinColumns =
@JoinColumn(name = "theme_id"),
inverseJoinColumns =
@JoinColumn(name = "id"))
private List<TheoreticTask> theoreticTaskList;
@OneToMany
@JoinTable(name = "user_answers",
joinColumns =
@JoinColumn(name = "theme_id"),
inverseJoinColumns =
@JoinColumn(name = "id"))
private List<UserAnswers> userAnswers;
}
TheoreticTask类
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name="theoretic_tasks")
public class TheoreticTask {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column( name = "theme_id")
private int themeId;
@Column
private String question;
@Column( name = "first_answer")
private String firstAnswer;
@Column( name = "second_answer")
private String secondAnswer;
@Column( name = "third_answer")
private String thirdAnswer;
@OneToMany
@JoinTable(name = "user_answers",
joinColumns =
@JoinColumn(name = "theoretic_task_last"),
inverseJoinColumns =
@JoinColumn(name = "id"))
private List<UserAnswers> userAnswers;
}
UserAnswer类
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
@Table(name = "user_answers")
public class UserAnswers implements Serializable {
@EmbeddedId
private UserAnswersPK id;
@Column(name = "theoretic_task_last")
private int theoreticTaskLast;
@Column(name = "practic_task_last")
private int practicTaskLast;
@Column(name = "practic_answer_count")
private int practicAnswerCount;
@Column(name = "theoretic_answer_count")
private int theoreticAnswerCount;
}
UserAnswerPK类
@Embeddable
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
public class UserAnswersPK implements Serializable {
@Column(name="user_id")
protected int user_id;
@Column(name="theme_id")
protected int theme_id;
}
所以我读到我需要制作JoinColumns但不了解。
答案 0 :(得分:1)
您的映射存在许多问题。我认为如果你在做这种关系之前阅读将是好的,除非你正在学习。
最好在ManytoOne
侧而不是OnetoMany
侧定义连接映射,我认为您不需要基于数据库关系图和连接表的连接表名。
我将向您展示UserAnswers和TheoreticTask之间的映射关系。您需要在所有(1..n)关系中类似地更改此内容。
@Entity
@Table(name="theoretic_tasks")
public class TheoreticTask {
// Other Columns and Relations
@OneToMany(mappedBy="theoreticTask")
private List<UserAnswers> userAnswers;
}
@Entity
@Table(name = "user_answers")
public class UserAnswers implements Serializable {
// Other Columns and Relations
@ManyToOne
@JoinColumn(name = "theoretic_task_last")
private TheoreticTask theoreticTask;
}