简体   繁体   English

Spring:无法确定类型:java.util.List

[英]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.

相关问题 无法确定类型:java.util.List - Could not determine type for: java.util.List ManyToMany:MappingException:无法确定以下类型:java.util.List - ManyToMany : MappingException: Could not determine type for: java.util.List org.hibernate.MappingException:无法确定类型:java.util.List,在表:大学,列:[org.hibernate.mapping.Column(students)] - org.hibernate.MappingException: Could not determine type for: java.util.List, at table: College, for columns: [org.hibernate.mapping.Column(students)] org.hibernate.MappingException:无法确定类型:java.util.List,在表:Schedule_assignedRoles,对于列:AssignedRoles - org.hibernate.MappingException: Could not determine type for: java.util.List, at table: Schedule_assignedRoles, for columns: assignedRoles 无法确定类型:java.util.List,在表:file_post,列:[org.hibernate.mapping.Column(file)] - Could not determine type for: java.util.List, at table: file_post, for columns: [org.hibernate.mapping.Column(file)] org.hibernate.MappingException:无法确定类型:java.util.List,在表:user处,用于列:[org.hibernate.mapping.Column(events)] - org.hibernate.MappingException: Could not determine type for: java.util.List, at table: user, for columns: [org.hibernate.mapping.Column(events)] 更改java.util.List的输出类型 - change type of the output of java.util.List 泛型类型java.util.List继承 - Generic type java.util.List inheritance 获取 java.util.List 的泛型类型 - Get generic type of java.util.List Java 类 java.util.List 和 Java 类型 java.util.List 的消息正文阅读器 - A message body reader for Java class java.util.List,and Java type java.util.List<Em>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM