2
in my application I have a user entity that relates many to many with the entity courses (a user can enroll in many courses and a course can have multiple users) And the entity courses relates from one to many to the entity class (a course has several classes, but a class only has one course)
I am doing my application with spring boot and for me to say that the user has enrolled in a course I just have to do:
usuario.getCursos().add(curso)
curso.getUsuario().add(usuario)
usuarioRepository.save(usuario)
by relating a course to the user I can say that he enrolled in the course. But how to relate the user to a class to know for example which classes he completed? i can take advantage of this many user relationship?
Here is a small example of the entities to better exemplify how this relationship:
@Entity
public class Usuario{
private long id;
private String nome;
private String senha;
@ManyToMany(mappedBy = “usuariosMatriculados”, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable
private List cursosMatriculados;
//getters and setters
}
@Entity
public class Curso{
private long id;
private String nomeCurso;
private String descricaoCurso;
@ManyToMany(mappedBy = “cursosMatriculados”, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable
private List usuariosMatriculados;
@OnetoMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private List aulas;
//getters and setters
}
@Entity
public class Aula{
private long id;
private String nomeAula;
private String conteudoAula;
@ManyToOne(mappedBy = “aulas”, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable
private Curso curso;
//getters and setters
}
Thank you very much, I will search how to use an intermediary table.
– Davi Resio