JPA kiedy muszę skorzystać z pośredniej tabeli

0

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ń:

  1. Taka metoda powinna być bardziej w StudentsDao czy w LecturesDao?
  2. Taka metoda lepiej i wydajniej jak jest w DAO niż w biznesie prawda?
  3. Czy używanie Query jest okey czy powinnam forsować QueryBuilder itd... ?
  4. 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 ?

1
  1. Skoro szukasz studentów to siłą rzeczy StudentsDao. Nie komplikuj niepotrzebnie.
  2. Nie rozumiem pytania. Nie wykonujesz żadnej operacji biznesowej tylko ciagniesz dane z bazy. Logiczne że siedzi to w DAO.
  3. Ja bym sugerował Criteria, ale to kwestia gustu.
    Lepiej stosować Set zamiast list. Po pierwsze dlatego że jest automatycznie indeksowane, po drugie dlatego ze Hibernate wysypie sie jak będziesz miała więcej niż jedną nieindeksowaną listę w encji ;)
0

@Shalom aha ok dzięki. Jeszcze jutro będę mieć kilka pytań ;p

1 użytkowników online, w tym zalogowanych: 0, gości: 1