Hej. Mam trzy tabele: studenci, grupy i wykłady. Student może być zapisany do jakieś jednej grupy a grupa może być zapisana na wiele różnych wykładów. Chcę teraz znaleźć studentów, którzy są zapisani na dany wykład - siłą rzeczy muszę skorzystać z grup (np. tabela groups_to_lectures
) żeby jakoś skojarzyć studenta z wykładem.
Schematu tabel już nie mogę ruszać, mam kilka pytań:
- Taka metoda powinna być bardziej w StudentsDao czy w LecturesDao?
- Taka metoda lepiej i wydajniej jak jest w DAO niż w biznesie prawda?
- Czy używanie
Query
jest okey czy powinnam forsowaćQueryBuilder
itd... ? - Czy to:
@Override
public List<Student> findByLecture(Lecture l) {
Query q = em.createQuery("select s from Student s where :l member of s.group.lectures");
q.setParameter("l", l);
return q.getResultList();
}
jest okey?
poniżej zamieszczam encje.
@Entity
@Table(name = "students")
public class Student implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "student_id",unique = true)
private Long id;
@Column(name = "name", nullable = false)
private String name;
@Column(name = "surname", nullable = false)
private String surname;
@ManyToOne
@JoinColumn(name = "group_id")
private Group group;
@Entity
@Table(name = "groups")
public class Group implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "group_id", unique = true)
private Long id;
@Column(name = "group_name", nullable = false, unique = true)
private String name;
@OneToMany(mappedBy = "group",fetch = FetchType.EAGER)
private List<Student> students = new ArrayList<Student>();
@ManyToMany
@JoinTable(
name = "groups_to_lectures",
joinColumns = {@JoinColumn(
name = "group_id",
referencedColumnName = "group_id")},
inverseJoinColumns = {@JoinColumn(
name = "lecture_id",
referencedColumnName = "lecture_id")}
)
private List<Lecture> lectures = new ArrayList<Lecture>();
@Entity
@Table(name = "lectures")
public class Lecture implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "lecture_id")
private Long id;
@Column(name = "name", nullable = false, unique = true)
private String name;
@ManyToMany(mappedBy = "lectures")
private List<Group> groups = new ArrayList<Group>();
poza tym macie jakieś uwagi? Nie lepiej stosować Set
zamiast List
?