0
votes

i have quick question related to hibernate and the use of its detachedcriteria.

Three tables all have their ID individually as the parent key.

The tables are table Course, table Teacher and their join table TeacherCourse. Course and Teacher both have one to many relationship to TeacherCourse.

My question now is how do I get all the unique Courses with a Teacher ID

My current code is like

public static ArrayList<Course> getCoursesByTeacher(Teacher teacher){
    ArrayList<Course> courses = new ArrayList<Course>();
    DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Course.class);
    detachedCriteria.add(Restrictions.eq(Key.TEACHER, teacher));
    detachedCriteria.add(Restrictions.eq(Key.OBJSTATUS, Value.ACTIVED));
    List<Object> list = HibernateUtil.detachedCriteriaReturnList(detachedCriteria);
    for(Object o : list){
        courses.add((Course) o);
    }
    return courses;
}

But the TeacherCourse is missing in between. How do I slot in the teacherCourse and find all the unique courses for a teacher.

1
Using DetachedCriteria to do that is shooting yourself in the foot. Also, why don't you return the list directly returned by the query? All you need is a super-simple JPQL query: select c from Course c join c.teacherCourses tc where tc.teacher = :teacher - JB Nizet
hey, thanks JB. I never use JPQL before, may I know where and how should i stuff this line of code inside mine? - Ray
public List<Course> getCoursesByTeacher(Teacher teacher) { return session.createQuery("select c from Course c join c.teacherCourses tc where tc.teacher = :teacher").setParameter("teacher", teacher).list();}. This is all documented: docs.jboss.org/hibernate/orm/5.0/userGuide/en-US/html_single/… - JB Nizet
I had my session created in my static method in my HibernateUtil class, was trying to recycle the codes. Now I was trying to use the JPQL suggested by you. Here is my code, but I think I am just plugging things in.. not sure where is the correct place to put the codes - Ray
public ArrayList<Course> getCoursesByTeacher(Teacher teacher){ ArrayList<Course> courses = new ArrayList<Course>(); Session session = HibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); List<Object> list = session.createQuery("select c from Course c join c.teacherStudentCourses tsc where tsc.teacher = :teacher") .setParameter("teacher", teacher).list(); session.getTransaction().commit(); session.close(); for(Object o : list){ courses.add((Course) o); } return courses; } An improved version - Ray

1 Answers

0
votes

JB Nizet give the correct answer, just to make it clearer, here is my solution in my situation:

public static ArrayList<Course> getCoursesByTeacher(Teacher teacher){
    ArrayList<Course> courses = new ArrayList<Course>();
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    List<Object> list = session.createQuery("select c from Course c join c.teacherCourses tc where tc.teacher = :teacher")
            .setParameter("teacher", teacher).list();
    session.getTransaction().commit();
    session.close();
    for(Object o : list){
        courses.add((Course) o);
    }
    return courses;
}