Taking in Array as path param and hibernate query - sql

I am trying to retrieve a list of students from a list inserted into the pathparam which retrieve the students based on their studentId using hibernate.
To retrieve a single student with a single studentId, I use the following code which is working.
student = (student) session.createQuery("from student as student
where student.studentId = :studentId")
.setString( "studentId", studentId ).uniqueResult();
However, when I try to pass in a list of studentIDs to return more than 1 student, I tried this code but it doesn't work. Hopefully someone can shed some light.
Any help is appreciated.
students = session.createQuery("from student as student
where student.studentId in :studentList").setParameterList( "studentList", studentList ).list();

Probably is due to missing parenthesis around :studentList; correct syntax for IN clause is IN (values list) so change your query as
students = session.createQuery("from student as student
where student.studentId in (:studentList)").setParameterList( "studentList", studentList ).list();
From Query.setParametersList() javadoc:
Bind multiple values to a named query parameter. The Hibernate type of
the parameter is first detected via the usage/position in the query
and if not sufficient secondly guessed from the class of the first
object in the collection. This is useful for binding a list of values
to an expression such as foo.bar in (:value_list).

Related

SQL Select query isn't working with my Access Form

So I am doing a project where we are attempting to allow our user to open the form, select (via a combobox) an instructor, and then return all the students who have had that instructor over the years in a table.
Without the WHERE clause the code runs, returning all the students who have had that instructor. But when I use the [Forms]![STUDENT FORM]![INSTRUCTOR] in the WHERE clause we suddenly get no results. Can anyone explain why this is? It has worked in the same form with a different combobox, but we didn't need to use any joins, so I'm wondering if that's the issue?
This is the SQL code - our form is named STUDENT FORM and the combobox is named INSTRUCTOR:
SELECT STUDENT.STU_LNAME, STUDENT.STU_FNAME, STUDENT.[STU_ INT],
STUDENT.STU_NICK, STUDENT.STU_YEAR, STUDENT.STU_PHONE, STUDENT.STU_EMAIL,
INSTRUCTOR.INSTRUCTOR_LNAME FROM
(INSTRUCTOR INNER JOIN CLASS ON
INSTRUCTOR.[INSTRUCTOR_ID] = CLASS.[INSTRUCTOR_ID]) INNER JOIN
(STUDENT INNER JOIN ENROLL ON STUDENT.[STU_NUM] = ENROLL.[STU_NUM])
ON CLASS.[CLASS_ID] = ENROLL.[CLASS_ID]
WHERE (((INSTRUCTOR.INSTRUCTOR_LNAME)=[Forms]![STUDENT FORM]![INSTRUCTOR]));
Please let me know if you need any more information!
I bet your INSTRUCTOR combobox has two columns, with INSTRUCTOR_ID being the bound column and having width 0 (so only INSTRUCTOR_LNAME is visible).
So the value you need to filter for is INSTRUCTOR_ID, and you need to change the WHERE clause to:
WHERE (((INSTRUCTOR.INSTRUCTOR_ID)=[Forms]![STUDENT FORM]![INSTRUCTOR]));

Entity Framework Many-to-Many Query

I'm struggling writing a query in Entity Framework that deals with a many to many relationship that I have set up. What I want to do is get the items from TableA that belong to a relationship with TableB and at the same time know from the results which relationship was a correct match.
For instance, if I'm using Students and Courses, I want to look for all the students that are in a set of courses and also return only those courses that matched. I very specifically want to start the query with Students, as this can easily be accomplished by just looking at the Courses navigation property to get the list of students.
What I want is a list of Students where each Student contains only the set of Courses in my query (NOT every course the student is taking).
Something like the below is close, I get the correct list of Students, but the navigation property for Courses shows all Courses, not the subset from my query. I want to avoid having to query again if possible, and just return the set of Students / Courses I need.
Dim listOfStudents = From s In Students
From c In s.Courses
Where listOfCourseIds.Contains(c.CourseId)
If there's no junction table between the two, then try:
from s in dc.Students
from c in s.Courses
where c.CourseID == courseID
select s;
If entity has a junction table between the two, try:
from s in dc.Students
from e in s.StudentsCourses
where e.Course.CourseID == courseID
select s;

Filtering out records in SQL with a join

I am creating an app that makes guest lists for greek life events at universities.
The two tables I am working with are 'student' table and 'participant' table.
The fields in the student table are: student_id, student_name, university, and chapter.
Students with chapter id's are considered members, and students without chapter id's are considered guests when making guest lists(participant table).
The participant table fields are: participant_id, member(which is related to student_id), guest (which is also related to student_id), and event.
When trying to add guests to the guest list for an event, I wrote the following sql query to filter out students from different universities and that aren't in chapters and weren't already on the list:
$student = getColumn("SELECT guest FROM participant WHERE event = '$event'");
$university = getSqlValue("SELECT university FROM student WHERE student_id = '$member'");
$f->setOption('filter',
"SELECT student_name
FROM `student`
LEFT JOIN participant ON student.student_id = participant.guest
WHERE student.chapter = ''
AND student.university = '$university'
AND participant.guest != '$student'");
So, I know this isn't going to work, because I have a whole array for $student, but even if I try it with one student id, the query doesn't work. It returns empty. If I remove the last AND particpant.guest!= $student, then the query returns all the students at the university that are not members of a chapter.
My question has two parts:
Why wouldn't that query work with one value for student?
Can someone think of a better way to go about doing this?

SQL query that limits results based on appearance in other tables

I'm a SQL novice, but need to write some SQL statements for a Java program that has to interact with a database. Our Java textbook covers only very basic SQL commands, and I am having trouble getting a more advanced (by my standards) one to work.
Here's the situation:
The database has 5 tables.
Teacher: TeacherID (PK), LastName
Class: ClassID (PK), Description
Room: Building, Room Number, PK is the combo of those two
TeachingAssignments: TeacherID(FK), ClassID(FK)
ClassRoomAssignments: ClassID(FK), Building, Room Number(combo is FK)
I need to give just the LastName, ClassID, and Building of only those teachers, classes, and rooms that are fully assigned. I.e., if a class both has a teacher and a room assignment, then I need to give that class's ID, the assigned teacher's last name, and the assigned building.
I have little idea how to proceed.
I've been playing around with statements like the following but they aren't working for me:
SELECT Teacher.LastName, Class.ClassID, Room.Building
FROM Teacher, Class, Room, TeachingAssignments, ClassRoomAssignments
WHERE Teacher.TeacherID = TeachingAssignments.TeacherID
AND Room.Building = ClassRoomAssignments.Building
AND Class.ClassID = TeachingAssignments.ClassID
AND Class.ClassID = ClassRoomAssignments.ClassID
Can anyone help? Thanks!
Your problem is that you need to add the respective joins for your table.
instead of doing:
SELECT Teacher.LastName, Class.ClassID, Room.Building
FROM Teacher, Class, Room, TeachingAssignments, ClassRoomAssignments
WHERE Teacher.TeacherID = TeachingAssignments.TeacherID
AND Room.Building = ClassRoomAssignments.Building
AND Class.ClassID = TeachingAssignments.ClassID
AND Class.ClassID = ClassRoomAssignments.ClassID
you need something like that
SELECT Teacher.LastName, Class.ClassID, Room.Building
FROM
Teacher INNER JOIN TeachingAssignments
ON Teacher.TeacherID = TeachingAssignments.TeacherID
INNER JOIN Class
ON Class.ClassID = TeachingAssignments.ClassID
INNER JOIN ClassRoomAssignments
ON Class.ClassID = ClassRoomAssignments.ClassID
INNER JOIN Room
ON Room.Building = ClassRoomAssignments.Building
As you can see every INNER Join is followed by it respective ON clause which is in charge of designing which element is going to be joined.

How to fetch out complex result from the database - Yii

i am facing problem to fetch out records from table using Yii relationship,
i have 3 tables
1) Students -> ID, Name, Roll_Number
2) Subjects -> ID, Name
3) Students_taken_Subjects -> ID, Student_ID, Subject_ID
Suppose there are number of students have taken more than one subjects which are stored in the 3rd table "Students_taken_Subjects" then how i can fetch out the list of students taken any specific subject?
e.g. list of students taken maths
which one from below relationships are correct and how can i get results into the $dataProvider variable?
'Students'=>array(self::HAS_MANY, 'Subjects', 'Student_ID'),
and
return array(
'Students'=>array(self::MANY_MANY, 'Subjects',
'Students_taken_Subjects(student_id, subject_id)'),
);
The relationship between Subjects and Students is MANY_MANY, but you've written it a bit wrong, This is what you need:
class Subjects extends CActiveRecord
{
// ...
public function relations()
{
return array(
'Students'=>array(self::MANY_MANY, 'Students', 'Students_taken_Subjects(Subject_ID, Student_ID)'),
);
}
// ...
}
Once you've written this relation, the Subjects active record will have a Students property that returns an array with the 0 or more students taking that subject. You can access them like this:
$subject = Subjects::model()->findByPk($pk);
$students = $subject->Students; // an array with the subject's students
The above code will result in two DB accesses, one for the $subject and one for the related $students. This might be fine, but if you are accessing a lot of subjects it could become a problem with too much "lazy loading". You can tell Yii to "eager load" the students along with the subjects like this:
$subjects = Subjects::model()->with('Students')->findAll();
Here you are finding all of the subjects, but alerting Yii--using with('Students')--that you'll be wanting each subject's student information as well. This ensures that all of the students related to the subjects you find will be grabbed at once. An alternative to the with() function is to use a criteria's with property:
$criteria=new CDbCriteria;
$criteria->with = array('Students');
$subjects = Subjects::model()->findAll($criteria);
Either way, when you do ask for a subject's students like this:
$subjects[0]->Students;
$subjects[1]->Students;
// ...
you will not get another DB call each time because Yii already loaded the data.
You'll need to provide more details about what you are wanting to do with the students in the data provider before I can give any more details about that.