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:Serv.SQLProcess.java

public void updateQuestionInDB(int eventId, String itemname, String tag, String type, String desc,
        String[] answers) {//  w w  w . j a v a  2  s . c  o  m
    Session session = HibernateUtil.getSessionFactory().openSession();

    String sql = "SELECT * FROM Questions " + "WHERE itemname = '" + itemname + "'" + "AND idevent=" + eventId;
    SQLQuery query = session.createSQLQuery(sql);
    ArrayList<Questions> results = (ArrayList) query.addEntity(Questions.class).list();

    System.out.println(results);

    Questions tempQ = results.get(0);

    session.beginTransaction();

    sql = "SELECT * FROM Answers " + "WHERE iditem=" + tempQ.getIditem();
    query = session.createSQLQuery(sql);
    ArrayList<Answers> answersQ = (ArrayList) query.addEntity(Answers.class).list();

    for (int i = 0; i < answersQ.size(); i++) {
        session.delete(answersQ.get(i));
    }

    tempQ.setItemname(itemname);
    tempQ.setTag(tag);
    tempQ.setType(type);
    tempQ.setDescription(desc);

    session.update(tempQ);

    Answers tempAns = new Answers();

    for (int i = 0; i < answers.length; i++) {
        tempAns = addAnswer(tempQ.getIditem(), answers[i], tempQ);

        session.save(tempAns);
    }

    session.getTransaction().commit();
    session.close();
}

From source file:Serv.SQLProcess.java

public void deleteQuestionFromDB(int eventId, String itemname) {
    Session session = HibernateUtil.getSessionFactory().openSession();

    String sql = "SELECT * FROM Questions " + "WHERE itemname = '" + itemname + "'" + "AND idevent=" + eventId;
    SQLQuery query = session.createSQLQuery(sql);
    ArrayList<Questions> results = (ArrayList) query.addEntity(Questions.class).list();

    System.out.println(results);//  w  w  w. ja  v  a2  s . c om

    Questions tempQ = results.get(0);

    session.beginTransaction();

    session.delete(tempQ);

    session.getTransaction().commit();
    session.close();
}

From source file:Serv.UploadProcess.java

public List<UploadEntity> fillData(int eventId) {
    ArrayList<UploadEntity> uploadEntity = new ArrayList<>();
    UploadEntity tempUE = new UploadEntity();

    Session session = HibernateUtil.getSessionFactory().openSession();

    //  //from w w  w  . jav  a 2  s  . c  o m
    Events event = (Events) session.get(Events.class, eventId);
    ArrayList<Users> eUsers = new ArrayList<>();
    ArrayList<Strings> uStrings = new ArrayList<>();
    ArrayList<Numbers> uNumbers = new ArrayList<>();
    ArrayList<Dates> uDates = new ArrayList<>();
    ArrayList<Blobs> uBlobs = new ArrayList<>();
    ArrayList<UploadItemAnswer> uia = new ArrayList<>();
    UploadItemAnswer tempUIA = new UploadItemAnswer();

    String sqlUser = "SELECT * FROM Users WHERE idevent=" + event.getIdevent();
    SQLQuery query = session.createSQLQuery(sqlUser);
    eUsers = (ArrayList) query.addEntity(Users.class).list();

    System.out.println(eUsers);

    for (int i = 0; i < eUsers.size(); i++) {

        String sqlString = "SELECT * FROM Strings WHERE iduser=" + eUsers.get(i).getId().getIduser()
                + " AND idevent=" + event.getIdevent();
        query = session.createSQLQuery(sqlString);
        uStrings = (ArrayList) query.addEntity(Strings.class).list();

        for (int j = 0; j < uStrings.size(); j++) {
            int id = uStrings.get(j).getId().getIditem();
            Questions tempQ = (Questions) session.get(Questions.class, id);
            tempUIA = new UploadItemAnswer(tempQ.getItemname(), uStrings.get(j).getId().getValue());
            uia.add(tempUIA);
        }

        String sqlNumber = "SELECT * FROM Numbers WHERE iduser=" + eUsers.get(i).getId().getIduser()
                + " AND idevent=" + event.getIdevent();
        query = session.createSQLQuery(sqlNumber);
        uNumbers = (ArrayList) query.addEntity(Numbers.class).list();

        for (int j = 0; j < uNumbers.size(); j++) {
            int id = uNumbers.get(j).getId().getIditem();
            Questions tempQ = (Questions) session.get(Questions.class, id);
            tempUIA = new UploadItemAnswer(tempQ.getItemname(),
                    String.valueOf(uNumbers.get(j).getId().getValue()));
            uia.add(tempUIA);
        }

        String sqlDate = "SELECT * FROM Dates WHERE iduser=" + eUsers.get(i).getId().getIduser()
                + " AND idevent=" + event.getIdevent();
        query = session.createSQLQuery(sqlDate);
        uDates = (ArrayList) query.addEntity(Dates.class).list();

        for (int j = 0; j < uDates.size(); j++) {
            int id = uDates.get(j).getId().getIditem();
            Questions tempQ = (Questions) session.get(Questions.class, id);
            tempUIA = new UploadItemAnswer(tempQ.getItemname(), uDates.get(j).getValue().toString());
            uia.add(tempUIA);
        }

        String sqlBlob = "SELECT * FROM Blobs WHERE iduser=" + eUsers.get(i).getId().getIduser()
                + " AND idevent=" + event.getIdevent();
        query = session.createSQLQuery(sqlBlob);
        uBlobs = (ArrayList) query.addEntity(Blobs.class).list();

        for (int j = 0; j < uBlobs.size(); j++) {
            int id = uBlobs.get(j).getId().getIditem();
            Questions tempQ = (Questions) session.get(Questions.class, id);
            tempUIA = new UploadItemAnswer(tempQ.getItemname(), uBlobs.get(j).getValue().toString());
            uia.add(tempUIA);
        }

        uploadEntity.add(
                new UploadEntity(event.getTitle(), eUsers.get(i).getSurname(), eUsers.get(i).getName(), uia));
        uia = new ArrayList();
    }

    session.close();

    return uploadEntity;
}

From source file:survey.QuestionHelper.java

public int getSurvey() {

    List<Survey> surveyList = null;
    //int result = 0;

    String sql = "select * from survey order by Survey_ID desc limit 1";

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

        // if this transaction is not active, make it active
        if (!this.session.getTransaction().isActive()) {
            session.beginTransaction();
        }

        // creating actual query that will be executed against the database
        SQLQuery q = session.createSQLQuery(sql);

        // associating the actor table and the actor POJO
        q.addEntity(Question.class);

        //q.setParameter("userId", a.getUser());
        // executes the query and returns it as a list
        surveyList = (List<Survey>) q.list();
        //result = q.executeUpdate();

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

    return surveyList.get(0).getSurveyId();
    //return result;
}

From source file:survey.QuestionHelper.java

public int insertQuestion(Question a) {
    int result = 0;

    String sql = "insert into question(Question_Text, Survey_ID)" + "values (:question, :survey)";

    try {//  w w  w . j  a v a 2  s.co  m
        // checks to see if the transaction is active
        if (!this.session.getTransaction().isActive()) {
            session.beginTransaction();
        }

        // creating a query that can be executed
        SQLQuery q = session.createSQLQuery(sql);

        // associating User POJO and table with a query
        q.addEntity(Question.class);

        // binds values to the placeholders in the query
        q.setParameter("question", a.getQuestionText());
        //q.setParameter("survey", a.getSurvey());
        q.setParameter("survey", a.getSurvey());

        // executes the query
        result = q.executeUpdate();

        // commits the query to the database
        session.getTransaction().commit();

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

    return result;
}

From source file:ud.ing.modi.mapper.DivisaMapper.java

public List<Divisa> obtenerDivisas() throws HibernateException {
    List<Divisa> tipoDivs = null;
    String query = "SELECT * FROM DIVISA";
    System.out.println("QUERY: " + query);
    try {/*w w  w .ja  v a  2  s. c o  m*/
        iniciaOperacion();
        SQLQuery sqlquery = getSesion().createSQLQuery(query);
        //   System.out.println("QUERY: "+sesion.createSQLQuery(query).getQueryString());
        sqlquery.addEntity(Divisa.class);
        tipoDivs = sqlquery.list();
    } finally {
        getSesion().close();
    }
    return tipoDivs;
}

From source file:ud.ing.modi.mapper.DocumentoMapper.java

public List<TipoDocumento> obtenerDocs() throws HibernateException {
    List<TipoDocumento> tipoDoc = null;
    String query = "SELECT * FROM TIPO_DOCUMENTO";
    System.out.println("QUERY: " + query);
    try {/*from w w w . j  a va  2s.c om*/
        iniciaOperacion();
        SQLQuery sqlquery = getSesion().createSQLQuery(query);
        //   System.out.println("QUERY: "+sesion.createSQLQuery(query).getQueryString());
        sqlquery.addEntity(TipoDocumento.class);
        tipoDoc = sqlquery.list();
    } finally {
        getSesion().close();
    }
    return tipoDoc;
}

From source file:ud.ing.modi.mapper.EntidadFinancieraMapper.java

public List<EntidadFinanciera> obtenerEntidadesFinaciera() {
    List<EntidadFinanciera> listaEntidades = null;
    String query = "SELECT * FROM ENTIDAD_FINANCIERA";
    iniciaOperacion();// ww w . ja  va 2 s . c om
    SQLQuery sqlquery = getSesion().createSQLQuery(query);
    sqlquery.addEntity(EntidadFinanciera.class);
    listaEntidades = sqlquery.list();
    return listaEntidades;
}

From source file:ud.ing.modi.mapper.EstadoMonederoMapper.java

public List<EstadoMonedero> obtenerEstados() throws HibernateException {
    List<EstadoMonedero> tipoMons = null;
    String query = "SELECT * FROM ESTADO_MONEDERO";
    System.out.println("QUERY: " + query);
    try {// w w w .  ja  v a  2s . com
        iniciaOperacion();
        SQLQuery sqlquery = getSesion().createSQLQuery(query);
        //   System.out.println("QUERY: "+sesion.createSQLQuery(query).getQueryString());
        sqlquery.addEntity(EstadoMonedero.class);
        tipoMons = sqlquery.list();
    } finally {
        getSesion().close();
    }
    return tipoMons;
}

From source file:ud.ing.modi.mapper.TipoCuentaMapper.java

public List<TipoCuentaBancaria> obtenerTiposCuentasBancarias() {
    List<TipoCuentaBancaria> listaTiposCuentas = null;
    String query = "SELECT * FROM TIPO_CUENTA";
    iniciaOperacion();//from ww w .  j  a v a 2 s  .  c o m
    SQLQuery sqlquery = getSesion().createSQLQuery(query);
    sqlquery.addEntity(TipoCuentaBancaria.class);
    listaTiposCuentas = sqlquery.list();
    return listaTiposCuentas;
}