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.StudentHelper.java

public Student getStudentID(String fName, String lName) {

    Student student = null;/*ww w.  j a v a  2  s.  c  o m*/
    // 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 {//from   w ww.  j  av  a2 s  .c o m
        // 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   w  w  w .  j av  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 www  .  ja v  a  2 s.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:gradeservice.model.helper.MarkHelper.java

public void delete(int entityId) {
    if (!session.isOpen()) {
        NewHibernateUtil.getSessionFactory().openSession();
    }/* w ww  . j av a  2  s  . com*/
    try {
        org.hibernate.Transaction tx = session.beginTransaction();
        SQLQuery q = session.createSQLQuery("delete from Mark where id = :entity_id");
        q.setParameter("entity_id", entityId);
        q.executeUpdate();
        session.getTransaction().commit();
    } catch (Exception e) {
        if (session.isOpen() == true) {
            session.getTransaction().rollback();
        }
    } finally {
        if (session.isOpen() == true) {
            session.close();
        }
    }
}

From source file:gradeservice.model.helper.StudentHelper.java

public void delete(int id) {
    List<Student> entityList = null;
    if (session.isOpen() == false) {
        NewHibernateUtil.getSessionFactory().openSession();
    }//from ww  w.j  a  va2  s. c  o  m
    if (session.getTransaction().wasCommitted()) {
        session.getTransaction().commit();
    }
    try {
        org.hibernate.Transaction tx = session.beginTransaction();
        SQLQuery q = session.createSQLQuery("delete from Student where id = :student_id");
        q.setParameter("student_id", id);
        q.executeUpdate();
        session.getTransaction().commit();
    } catch (Exception e) {
        if (session.isOpen() == true) {
            session.getTransaction().rollback();
        }
    } finally {
        if (session.isOpen() == true) {
            session.close();
        }
    }
}

From source file:gradeservice.model.helper.SubjectHelper.java

public void delete(int entityId) {
    org.hibernate.Transaction tx = session.beginTransaction();
    SQLQuery q = session.createSQLQuery("delete from Subject where id = :entity_id");
    q.setParameter("entity_id", entityId);
    q.executeUpdate();//from w  w w. j  ava 2  s  .  c om
    session.getTransaction().commit();
}

From source file:ispyb.server.biosaxs.services.core.analysis.Analysis3ServiceBean.java

License:Open Source License

@Override
public List<Map<String, Object>> getExperimentListByProposalId(int proposalId) {
    String mySQLQuery = SQLQueryKeeper.getExperimentListByProposalId(proposalId);
    Session session = (Session) this.entityManager.getDelegate();
    SQLQuery query = session.createSQLQuery(mySQLQuery);
    query.setParameter("proposalId", proposalId);
    query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
    @SuppressWarnings("unchecked")
    List<Map<String, Object>> aliasToValueMapList = query.list();
    return aliasToValueMapList;
}

From source file:ispyb.server.biosaxs.services.core.analysis.Analysis3ServiceBean.java

License:Open Source License

@Override
public List<Map<String, Object>> getExperimentListByProposalId(int proposalId, String experimentType) {
    String mySQLQuery = SQLQueryKeeper.getExperimentListByProposalId(proposalId, experimentType);
    Session session = (Session) this.entityManager.getDelegate();
    SQLQuery query = session.createSQLQuery(mySQLQuery);
    query.setParameter("proposalId", proposalId);
    query.setParameter("experimentType", experimentType);
    query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
    @SuppressWarnings("unchecked")
    List<Map<String, Object>> aliasToValueMapList = query.list();
    return aliasToValueMapList;
}

From source file:ispyb.server.biosaxs.services.core.analysis.Analysis3ServiceBean.java

License:Open Source License

@Override
public List<Map<String, Object>> getExperimentListBySessionId(Integer proposalId, Integer sessionId) {
    String mySQLQuery = SQLQueryKeeper.getExperimentListBySessionId(proposalId, sessionId);
    Session session = (Session) this.entityManager.getDelegate();
    SQLQuery query = session.createSQLQuery(mySQLQuery);
    query.setParameter("proposalId", proposalId);
    query.setParameter("sessionId", sessionId);
    query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
    @SuppressWarnings("unchecked")
    List<Map<String, Object>> aliasToValueMapList = query.list();
    return aliasToValueMapList;
}