Skip to main content
@Entity
public class Teacher {

@Id
@Column(name="teacher_id")
private int id;
@Column(name="teacher_name")
private String name;
@Column(name="teacher_phone")
private String phoneNumber;
private List<Course> courses;

public Teacher() {
    courses = new ArrayList<>();
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getPhoneNumber() {
    return phoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
    this.phoneNumber = phoneNumber;
}

@OneToMany(mappedBy="teacher")
public List<Course> getCourses() {
    return courses;
}

}

@Entity
public class Teacher {

    @Id
    @Column(name="teacher_id")
    private int id;
    @Column(name="teacher_name")
    private String name;
    @Column(name="teacher_phone")
    private String phoneNumber;
    private List<Course> courses;
    
    public Teacher() {
        courses = new ArrayList<>();
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    @OneToMany(mappedBy="teacher")
    public List<Course> getCourses() {
        return courses;
    }
    
 }
@Entity
public class Course {

@Id
@Column(name="course_id")
private int id;
@Column(name="course_name")
private String name;
@Column(name="course_schedule")
private String schedule;


private Teacher teacher;

public Course() {
    
}

@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name="teacher_id")
public Teacher getTeacher() {
    return teacher;
}


public void setTeacher(Teacher teacher) {
    this.teacher = teacher;
}

public int getId() {
    return id;
}


public void setId(int id) {
    this.id = id;
}


public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getSchedule() {
    return schedule;
}

public void setSchedule(String schedule) {
    this.schedule = schedule;
}

}

@Entity
public class Course {

    @Id
    @Column(name="course_id")
    private int id;
    @Column(name="course_name")
    private String name;
    @Column(name="course_schedule")
    private String schedule;
    
    
    private Teacher teacher;

    public Course() {
        
    }

    @ManyToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name="teacher_id")
    public Teacher getTeacher() {
        return teacher;
    }


    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }
    
    public int getId() {
        return id;
    }


    public void setId(int id) {
        this.id = id;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSchedule() {
        return schedule;
    }

    public void setSchedule(String schedule) {
        this.schedule = schedule;
    }
   }
@Entity
public class Teacher {

@Id
@Column(name="teacher_id")
private int id;
@Column(name="teacher_name")
private String name;
@Column(name="teacher_phone")
private String phoneNumber;
private List<Course> courses;

public Teacher() {
    courses = new ArrayList<>();
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getPhoneNumber() {
    return phoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
    this.phoneNumber = phoneNumber;
}

@OneToMany(mappedBy="teacher")
public List<Course> getCourses() {
    return courses;
}

}

@Entity
public class Course {

@Id
@Column(name="course_id")
private int id;
@Column(name="course_name")
private String name;
@Column(name="course_schedule")
private String schedule;


private Teacher teacher;

public Course() {
    
}

@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name="teacher_id")
public Teacher getTeacher() {
    return teacher;
}


public void setTeacher(Teacher teacher) {
    this.teacher = teacher;
}

public int getId() {
    return id;
}


public void setId(int id) {
    this.id = id;
}


public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getSchedule() {
    return schedule;
}

public void setSchedule(String schedule) {
    this.schedule = schedule;
}

}

@Entity
public class Teacher {

    @Id
    @Column(name="teacher_id")
    private int id;
    @Column(name="teacher_name")
    private String name;
    @Column(name="teacher_phone")
    private String phoneNumber;
    private List<Course> courses;
    
    public Teacher() {
        courses = new ArrayList<>();
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    @OneToMany(mappedBy="teacher")
    public List<Course> getCourses() {
        return courses;
    }
    
 }
@Entity
public class Course {

    @Id
    @Column(name="course_id")
    private int id;
    @Column(name="course_name")
    private String name;
    @Column(name="course_schedule")
    private String schedule;
    
    
    private Teacher teacher;

    public Course() {
        
    }

    @ManyToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name="teacher_id")
    public Teacher getTeacher() {
        return teacher;
    }


    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }
    
    public int getId() {
        return id;
    }


    public void setId(int id) {
        this.id = id;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSchedule() {
        return schedule;
    }

    public void setSchedule(String schedule) {
        this.schedule = schedule;
    }
   }
Fonte Link
user94991
  • 249
  • 1
  • 4
  • 22

Como mapear One to Many Corretamente no Hibernate

Boa tarde, estou criando um Restful API bem simples que implementa um sistema de cursos online, onde tenho apenas 2 tabelas "Cursos" e "Professores" (E estou tentando aprender hibernate no processo):

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

Eu consegui fazer tudo funcionar direitinho até eu precisar fazer um inner join entre as 2 tabelas, agora não consigo retornar os valores... eu queria algo como:

inserir a descrição da imagem aqui

Classe Teacher

@Entity
public class Teacher {

@Id
@Column(name="teacher_id")
private int id;
@Column(name="teacher_name")
private String name;
@Column(name="teacher_phone")
private String phoneNumber;
private List<Course> courses;

public Teacher() {
    courses = new ArrayList<>();
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getPhoneNumber() {
    return phoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
    this.phoneNumber = phoneNumber;
}

@OneToMany(mappedBy="teacher")
public List<Course> getCourses() {
    return courses;
}

}

Classe Course

@Entity
public class Course {

@Id
@Column(name="course_id")
private int id;
@Column(name="course_name")
private String name;
@Column(name="course_schedule")
private String schedule;


private Teacher teacher;

public Course() {
    
}

@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name="teacher_id")
public Teacher getTeacher() {
    return teacher;
}


public void setTeacher(Teacher teacher) {
    this.teacher = teacher;
}

public int getId() {
    return id;
}


public void setId(int id) {
    this.id = id;
}


public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getSchedule() {
    return schedule;
}

public void setSchedule(String schedule) {
    this.schedule = schedule;
}

}

Classe de Acesso ao banco de dados que faria a query

public interface CoursesRepository extends CrudRepository<Course, Integer>{


@Query("select c.course_id, c.course_name, c.course_schedule, t.teacher_name "
        + "from course c "
        + "inner join teacher t "
        + "on c.teacher_id = t.teacher_id")
public List<Course> findAllCoursesAndTeachers();

}

Eu estou recebendo um erro que o Spring não consegue criar o bean para a minha classe, mas creio que seja um problema com meus relacionamentos e/ou minha query (Na query que mostrei de exemplo eu só trazia o nome do professor, mas não teria problema se eu trouxesse o objeto inteiro) :

Caused by: org.hibernate.MappingException: Could not determine type for: java.util.List, at table: teacher, for columns: [org.hibernate.mapping.Column(courses)]

Isso quer dizer que eu não deveria ter a list de cursos na classe de professores?

Obrigado desde já.