[英]Spring: Could not determine type for: java.util.List
I have the following scenario: I have a Student class and students
table.我有以下场景:我有一个学生 class 和
students
表。 I have Course class and courses
table.我有课程 class 和
courses
表。 Every student and course have unique ID.每个学生和课程都有唯一的 ID。 I would like to put a List into the Student class which is mapped by courses IDs.
我想将一个列表放入由课程 ID 映射的学生 class 中。
I have tried a lot of annotations and relations but nothing succeeded我尝试了很多注释和关系,但没有成功
@Entity
@Table(name = "courses")
public class Course {
private long id;
private String name;
public Course() {
}
public Course(String name, int size) {
this.name = name;
this.size = size;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Column(name = "name", nullable = false)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Entity
@Table(name = "students")
public class Student {
private long id;
private String name;
@OneToMany(cascade = CascadeType.ALL)
private List<Course> courses = new ArrayList<>();
public Student() {
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Column(name = "name", nullable = false)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Course> getCourses() {
return courses;
}
public void setCourses(List<Course> courses) {
this.courses = courses;
}
}
Can you help me hot to achieve that because i am a little newbie into the spring你能帮我实现这一目标吗,因为我是 spring 的新手
Use the following when declaring one to many relation声明一对多关系时使用以下内容
@Column(name="course_id")
private Set<Course> courses;
You need to use OneToMany annotation.您需要使用 OneToMany 注释。
@OneToMany(mappedBy = "students", cascade = CascadeType.ALL)
private List<Course> courses;
You need to give your list a type.你需要给你的列表一个类型。 For instance
List<Courses> courseList = new ArrayList<>();
例如
List<Courses> courseList = new ArrayList<>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.