Example usage for org.hibernate SQLQuery addEntity

List of usage examples for org.hibernate SQLQuery addEntity

Introduction

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

Prototype

SQLQuery<T> addEntity(Class entityType);

Source Link

Document

Declare a "root" entity, without specifying an alias.

Usage

From source file:gradebook.StudentHelper.java

public List getStudents() {

    List<Student> studentList = null;

    String sql = "SELECT * FROM student";

    try {/*www  .  j a  va 2s . co m*/

        if (!this.session.getTransaction().isActive()) {
            session.beginTransaction();
        }

        SQLQuery query = session.createSQLQuery(sql);

        query.addEntity(Student.class);

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

    return studentList;
}

From source file:gradebook.StudentHelper.java

public int getStudentNumber() {

    List<Student> studentList = null;

    String sql = "select * from student";

    try {//from   w ww . j  a  v  a 2  s  . co  m

        if (!this.session.getTransaction().isActive()) {
            session.beginTransaction();
        }

        SQLQuery q = session.createSQLQuery(sql);

        q.addEntity(Student.class);

        studentList = (List<Student>) q.list();

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

    return studentList.size();
}

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 {/*from w w  w .j  a va 2  s  . 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;
}

From source file:gradebook.StudentHelper.java

public Student getStudentID(String fName, String lName) {

    Student student = null;/*from www  .j  a  va  2 s.c  om*/
    // int studentID = 0;

    String sql = "SELECT * FROM STUDENT WHERE STUDENT_FNAME = :fname AND STUDENT_LNAME = :lname "
            + "ORDER BY STUDENT_ID DESC LIMIT 1";

    try {

        //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("fname", fName);
        q.setParameter("lname", lName);

        student = (Student) q.uniqueResult();
        // studentID = student.getStudentId();

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

From source file:gradebook.StudentHelper.java

public int insertStudent(String fname, String lname) {

    // Initialize result value
    int result = 0;

    // Insert student
    String sql = "INSERT INTO Student " + "(student_fname, student_lname) " + "VALUES (:fname, :lname)";

    try {/* w  ww . j a  v  a 2  s  . com*/
        // Initialize transaction if none initialized
        if (!this.session.getTransaction().isActive()) {
            session.beginTransaction();
        }

        // Create SQL query
        SQLQuery query = session.createSQLQuery(sql);

        query.addEntity(Student.class);

        query.setParameter("fname", fname);
        query.setParameter("lname", lname);

        result = query.executeUpdate();

        session.getTransaction().commit();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return result;
}

From source file:gradebook.StudentHelper.java

public int deleteStudent(int studentID) {
    int result = 0;

    String sql = "delete from student" + "where studentID = :sid";

    try {/*from ww w.  j  a  v a  2s  .c  o  m*/

        if (!this.session.getTransaction().isActive()) {
            session.beginTransaction();
        }

        SQLQuery q = session.createSQLQuery(sql);
        q.addEntity(Student.class);
        q.setParameter("sid", studentID);

        result = q.executeUpdate();

        session.getTransaction().commit();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:gradebook.StudentHelper.java

public Student getStudent(int sid) {

    List<Student> studentList = null;

    String sql = "select * from student WHERE student_id = :sid";

    try {/*from  w w w.j a v a2s  .  c  o  m*/

        if (!this.session.getTransaction().isActive()) {
            session.beginTransaction();
        }

        SQLQuery q = session.createSQLQuery(sql);

        q.addEntity(Student.class);
        q.setParameter("sid", sid);

        studentList = (List<Student>) q.list();

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

    if (studentList.size() == 1) {
        return studentList.get(0);
    } else {
        return null;
    }
}

From source file:hibernate.dao.TbCareersDao.java

public List<Object[]> SelectStudentsAndCareerRows(String conditions) throws HibernateException {
    SQLQuery SqlQuery;
    List<Object[]> list = null;
    String query = "SELECT * FROM Tb_Careers, Tb_Students WHERE Tb_Students.fl_fk_career=Tb_Careers.fl_pk_career";
    try {/*from w  w w .  j  a v  a2  s .c o m*/
        session = HibernateUtil.getSessionFactory().openSession();
        SqlQuery = session.createSQLQuery(query);
        SqlQuery.addEntity(TbCareers.class);
        SqlQuery.addEntity(TbStudents.class);
        list = SqlQuery.list();
    } catch (HibernateException ex) {
        throw new HibernateException("Error in access to data...  \b ->" + ex);
    } finally {
        session.close();
    }
    return list;
}

From source file:it.cyberdyne.dss.places.ManageDistances.java

public List<Distance> listDistances() {
    helper.openSession();/*w w w .  ja v a2 s  .co m*/
    Session session = helper.getSession();
    SQLQuery query = session.createSQLQuery("SELECT * FROM PlaceDistances WHERE 1");
    query.addEntity(Distance.class);

    List<Distance> list = query.list();

    helper.closeSession();
    return list;
}

From source file:it.cyberdyne.dss.places.ManageTravelTimes.java

public List<TravelTime> listTimes() {
    helper.openSession();//from   www .  j ava2  s .  c om
    Session session = helper.getSession();
    SQLQuery query = session.createSQLQuery("SELECT * FROM TravelTimes WHERE 1");
    query.addEntity(TravelTime.class);
    //TODO filter for userId
    List<TravelTime> list = query.list();

    helper.closeSession();
    return list;
}