From 15d4d30c1fcfd6901ad34e62eb0ab3b13017f128 Mon Sep 17 00:00:00 2001 From: Jan Vales Date: Wed, 28 Mar 2018 02:58:21 +0200 Subject: [PATCH] [1.2.2] "complex" query. --- .../dst/ass1/jpa/dao/impl/LecturerDAO.java | 21 ++++++++++++++++++- .../dst/ass1/jpa/model/impl/Lecturer.java | 5 +++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/ass1-jpa/src/main/java/dst/ass1/jpa/dao/impl/LecturerDAO.java b/ass1-jpa/src/main/java/dst/ass1/jpa/dao/impl/LecturerDAO.java index 8db0297..1aefce4 100644 --- a/ass1-jpa/src/main/java/dst/ass1/jpa/dao/impl/LecturerDAO.java +++ b/ass1-jpa/src/main/java/dst/ass1/jpa/dao/impl/LecturerDAO.java @@ -5,8 +5,14 @@ import dst.ass1.jpa.model.ICourse; import dst.ass1.jpa.model.ILecturer; import dst.ass1.jpa.model.ILesson; import dst.ass1.jpa.model.impl.Lecturer; +import dst.ass1.jpa.model.impl.Lesson; +import dst.ass1.jpa.util.Constants; import javax.persistence.EntityManager; +import javax.persistence.NoResultException; +import java.util.Collection; +import java.util.Date; +import java.util.HashMap; import java.util.Map; public class LecturerDAO extends GenericDAO implements ILecturerDAO { @@ -16,6 +22,19 @@ public class LecturerDAO extends GenericDAO implements ILec @Override public Map findUpcomingLessonForLecturersOfCourse(ICourse course) { - return null; + try { + Collection res = em.createNamedQuery(Constants.Q_UPCOMING_LESSONS, Object[].class) + .setParameter("now", new Date()).setParameter("course", course).getResultList(); + + Map ret = new HashMap<>(); + for (Object[] o : res) { + Lecturer lec = (Lecturer) o[0]; + Lesson les = (Lesson) o[1]; + if (!ret.containsKey(lec)) ret.put(lec, les); + } + return ret; + } catch (NoResultException e) { + return new HashMap<>(); + } } } diff --git a/ass1-jpa/src/main/java/dst/ass1/jpa/model/impl/Lecturer.java b/ass1-jpa/src/main/java/dst/ass1/jpa/model/impl/Lecturer.java index 8f5024e..988527d 100644 --- a/ass1-jpa/src/main/java/dst/ass1/jpa/model/impl/Lecturer.java +++ b/ass1-jpa/src/main/java/dst/ass1/jpa/model/impl/Lecturer.java @@ -17,6 +17,11 @@ import java.util.Collection; " WHERE size(l.lessons) >= ALL(" + " SELECT size(le.lessons) FROM " + Constants.T_LECTURER + " as le GROUP BY le" + " )") +@NamedQuery(name = Constants.Q_UPCOMING_LESSONS, query = "SELECT lec,les" + + " FROM " + Constants.T_LECTURER + " as lec " + + " JOIN lec.lessons les" + + " WHERE les.course = :course AND les.start > :now" + + " ORDER BY les.start") public class Lecturer extends Person implements ILecturer { @OneToMany(targetEntity = Course.class) private Collection courses = new ArrayList<>(); -- 2.43.0