Example usage for org.hibernate SQLQuery setParameter

List of usage examples for org.hibernate SQLQuery setParameter

Introduction

In this page you can find the example usage for org.hibernate SQLQuery setParameter.

Prototype

@Override
    NativeQuery<T> setParameter(int position, Object val);

Source Link

Usage

From source file:gradebook.StudentAssignmentHelper.java

public List<Student> getStudentsExcludeAtAid(int aid) {
    // Create the list
    List<Student> studentAssignmentList = null;

    String sql = "SELECT * FROM student "
            + "LEFT JOIN student_assignment ON student.student_id = student_assignment.student_id "
            + "WHERE student_assignment.assignment_id = :aid";

    try {/*from  w ww  .  j a  v a  2 s. c o m*/
        // Begin new transaction if we have an inactive one
        if (!this.session.getTransaction().isActive()) {
            session.beginTransaction();
        }

        // Create an SQL query from the SQL string
        SQLQuery query = session.createSQLQuery(sql);

        // Add an entity
        query.addEntity(Student.class);

        // Binding parameters
        query.setParameter("aid", aid);

        // Execute query
        studentAssignmentList = (List<Student>) query.list();
    } catch (Exception e) {
        e.printStackTrace();
    }

    // Return gradebooks
    return studentAssignmentList;

}

From source file:gradebook.StudentAssignmentHelper.java

public Assignment getAssignmentAtID(int aid) {

    List<Assignment> assignmentList = null;

    String sql = "SELECT * FROM assignment WHERE ASSIGNMENT_ID = :aid";

    try {/*from  w w w . ja  va2s.c  o m*/
        // Initialize transaction if not already initialized
        if (!this.session.getTransaction().isActive()) {
            session.beginTransaction();
        }

        SQLQuery query = session.createSQLQuery(sql);

        query.addEntity(Assignment.class);

        query.setParameter("aid", aid);

        assignmentList = (List<Assignment>) query.list();
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (assignmentList.size() != 0) {
        return assignmentList.get(0);
    } else {
        return null;
    }
}

From source file:gradebook.StudentAssignmentHelper.java

public List<Student> getStudentsFromGradebookAssignment(int gaid) {
    // Create the list
    List<Student> studentList = null;

    String sql = "SELECT * FROM student "
            + "INNER JOIN gradebook_student ON gradebook_student.STUDENT_ID = student.student_ID "
            + "INNER JOIN gradebook_assignment ON gradebook_student.GRADEBOOK_ID = gradebook_assignment.GRADEBOOK_ID "
            + "INNER JOIN student_assignment ON gradebook_assignment.GRADEBOOK_ASSIGNMENT_ID = student_assignment.GRADEBOOK_ASSIGNMENT_ID "
            + "WHERE student_assignment.GRADEBOOK_ASSIGNMENT_ID = :gaid";

    try {/* w  ww  . ja va  2 s  .  com*/
        // Begin new transaction if we have an inactive one
        if (!this.session.getTransaction().isActive()) {
            session.beginTransaction();
        }

        // Create an SQL query from the SQL string
        SQLQuery query = session.createSQLQuery(sql);

        // Add an entity
        query.addEntity(Student.class);

        // Binding parameters
        query.setParameter("gaid", gaid);

        // Execute query
        studentList = (List<Student>) query.list();
    } catch (Exception e) {
        e.printStackTrace();
    }

    // Return gradebooks
    return studentList;

}

From source file:gradebook.StudentAssignmentHelper.java

public StudentAssignment getStudentAssignment(int sid, int gaid) {

    List<StudentAssignment> gs = null;

    String sql = "SELECT * FROM Student_Assignment WHERE student_id = :sid AND gradebook_assignment_id = :gaid";

    try {//ww w  .ja v  a  2  s .c  o m
        if (!this.session.getTransaction().isActive()) {
            this.session.beginTransaction();
        }

        SQLQuery query = session.createSQLQuery(sql);
        query.addEntity(StudentAssignment.class);

        query.setParameter("sid", sid);
        query.setParameter("gaid", gaid);

        gs = (List<StudentAssignment>) query.list();
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (gs.size() != 0) {
        return gs.get(0);
    } else {
        return null;
    }
}

From source file:gradebook.StudentAssignmentHelper.java

public int changeGrade(int said, int grade) {

    int result = 0;

    String sql = "UPDATE Student_Assignment SET Student_Assignment_Grade = :grade WHERE Student_Assignment_ID = :said";

    try {/*from  w  w  w  .ja  v  a 2s. c  om*/
        if (!this.session.getTransaction().isActive()) {
            this.session.beginTransaction();
        }

        SQLQuery query = session.createSQLQuery(sql);
        query.addEntity(StudentAssignment.class);

        query.setParameter("grade", grade);
        query.setParameter("said", said);

        result = query.executeUpdate();
        session.getTransaction().commit();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return result;
}

From source file:gradebook.StudentAssignmentHelper.java

public StudentAssignment getGrade(int said) {
    // Create the list
    List<StudentAssignment> list = null;

    String sql = "SELECT * FROM Student_assignment WHERE STUDENT_ASSIGNMENT_ID = :said";

    try {//from   w ww .  ja  v  a 2  s .c o  m
        // Begin new transaction if we have an inactive one
        if (!this.session.getTransaction().isActive()) {
            session.beginTransaction();
        }

        // Create an SQL query from the SQL string
        SQLQuery query = session.createSQLQuery(sql);

        // Add an entity
        query.addEntity(StudentAssignment.class);

        // Binding parameters
        query.setParameter("said", said);

        // Execute query
        list = (List<StudentAssignment>) query.list();
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (list.size() != 0) {
        return list.get(0);
    } else {
        return null;
    }
}

From source file:gradebook.StudentAssignmentHelper.java

public int getGradebookAssignmentId(int gid, int aid) {
    // Create the list
    List<GradebookAssignment> gal = null;

    String sql = "SELECT * FROM gradebook_assignment WHERE GRADEBOOK_ID = :gid AND ASSIGNMENT_ID = :aid";
    try {//w w w  .  j a  v a2 s .c o m
        // Begin new transaction if we have an inactive one
        if (!this.session.getTransaction().isActive()) {
            session.beginTransaction();
        }

        // Create an SQL query from the SQL string
        SQLQuery query = session.createSQLQuery(sql);

        // Add an entity
        query.addEntity(GradebookAssignment.class);

        // Binding parameters
        query.setParameter("gid", gid);
        query.setParameter("aid", aid);

        // Execute query
        gal = (List<GradebookAssignment>) query.list();
        if (gal.size() != 0) {
            int gaid = gal.get(0).getGradebookAssignmentId();
            return gaid;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    // Return gradebooks
    return 0;

}

From source file:gradebook.StudentAssignmentHelper.java

public List<StudentAssignment> getStudentAssignmentList(int gaid) {
    // Create the list
    List<StudentAssignment> sal = null;

    String sql = "SELECT * FROM student_assignment WHERE GRADEBOOK_ASSIGNMENT_ID = :gaid";
    try {/*w ww. j av a 2  s.c o m*/
        // Begin new transaction if we have an inactive one
        if (!this.session.getTransaction().isActive()) {
            session.beginTransaction();
        }

        // Create an SQL query from the SQL string
        SQLQuery query = session.createSQLQuery(sql);

        // Add an entity
        query.addEntity(StudentAssignment.class);

        // Binding parameters
        query.setParameter("gaid", gaid);

        // Execute query
        sal = (List<StudentAssignment>) query.list();
    } catch (Exception e) {
        e.printStackTrace();
    }

    // Return gradebooks
    return sal;

}

From source file:gradebook.StudentAssignmentHelper.java

public List<Student> getStudentsFromGradebook(int gid) {
    // Create the list
    List<Student> studentList = null;

    String sql = "SELECT * FROM student "
            + "INNER JOIN gradebook_student ON gradebook_student.STUDENT_ID = student.student_ID "
            + "WHERE gradebook_student.GRADEBOOK_ID = :gid";

    try {/*from   ww w  .j a va2 s.co  m*/
        // Begin new transaction if we have an inactive one
        if (!this.session.getTransaction().isActive()) {
            session.beginTransaction();
        }

        // Create an SQL query from the SQL string
        SQLQuery query = session.createSQLQuery(sql);

        // Add an entity
        query.addEntity(Student.class);

        // Binding parameters
        query.setParameter("gid", gid);

        // Execute query
        studentList = (List<Student>) query.list();
    } catch (Exception e) {
        e.printStackTrace();
    }

    // Return gradebooks
    return studentList;

}

From source file:gradebook.StudentHelper.java

public int getStudentID() {
    int studentID = 0;

    String sql = "select * from student where student_id = :id ORDER BY STUDENT_ID DESC LIMIT 1";

    try {//  w w  w  . ja v a  2s  .co  m

        //3 lines of code are always consistant 
        if (!this.session.getTransaction().isActive()) {
            session.beginTransaction();
        }

        SQLQuery q = session.createSQLQuery(sql);

        q.addEntity(Student.class);

        q.setParameter("id", studentID);

        studentID = (int) q.uniqueResult();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return studentID;
}