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:db.manage.MeasureManage.java

public MeasureEntity receiveMeasureByCaseID(String caseID) {
    MeasureEntity measureEntity = null;//from  w  ww .  j av  a2  s.  c  om
    Session session = sessionFactory.openSession();
    Transaction tx = null;
    try {
        tx = session.beginTransaction();
        String sql = "SELECT * FROM MEASURE WHERE CASE_ID = " + "'" + caseID + "'";
        SQLQuery query = session.createSQLQuery(sql);
        query.addEntity(MeasureEntity.class);
        List measures = query.list();
        if (measures.isEmpty()) {
            measureEntity = new MeasureEntity();
            measureEntity.setNew1(1);
            measureEntity.setTotal(0);
            measureEntity.setCaseId(caseID);
            measureEntity.setCustomerid(0);
            measureEntity.setNumber1insulatatticblownche(0);
            measureEntity.setNumber1insulatatticblownstring("");
            measureEntity.setSqft1(0);
            measureEntity.setNumber1insulatatticblownresult(0);
            measureEntity.setNumber2removeandreplaceche(0);
            measureEntity.setSqft2(0);
            measureEntity.setNumber2removeandreplaceresult(0);
            measureEntity.setNumber3installbattedinionche(0);
            measureEntity.setSqft3(0);
            measureEntity.setNumber3installbattedinresult(0);
            measureEntity.setNumber4replaceductscheckbox(0);
            measureEntity.setNumber4replaceductsstring("");
            measureEntity.setSqft4(0);
            measureEntity.setNumber4replaceductsresult(0);
            measureEntity.setSqft5(0);
            measureEntity.setNumber5airsealpcheckbox(0);
            measureEntity.setNumber5airsealstring("");
            measureEntity.setNumber5airsealparesult(0);
            measureEntity.setSqft5a(0);
            measureEntity.setNumber5acheckbox(0);
            measureEntity.setNumber5airsealstring("");
            measureEntity.setNumber5airsealparesult(0);
            measureEntity.setSqft6(0);
            measureEntity.setNumber6checkbox(0);
            measureEntity.setNumber6string("");
            measureEntity.setNumber6result(0);
            measureEntity.setSqft7a(0);
            measureEntity.setNumber7ainstallductingcheckbox(0);
            measureEntity.setNumber7ainstallductingstring("");
            measureEntity.setNumber7ainstallductingresult(0);
            measureEntity.setSqft7b(0);
            measureEntity.setNumber7bcheckbox(0);
            measureEntity.setNumber7bstring("");
            measureEntity.setNumber7bresult(0);
            measureEntity.setSqft8a(0);
            measureEntity.setNumber8acheckbox(0);
            measureEntity.setNumber8anewhvacstring("");
            measureEntity.setNumber8aresult(0);
            measureEntity.setSqft8b(0);
            measureEntity.setNumber8bcheckbox(0);
            measureEntity.setNumber8bnewhvacsysstring("");
            measureEntity.setNumber8bresult(0);
            measureEntity.setNumber8ccheckbox(0);
            measureEntity.setSqft8c(0);
            measureEntity.setNumber8cresult(0);
            measureEntity.setNumber8dresult(0);
            measureEntity.setNumber8dcheckbox(0);
            measureEntity.setSqft8bii(0);
            measureEntity.setNumber8biiincludecheckbox(0);
            measureEntity.setNumber8biiincludestring("");
            measureEntity.setNumber8biiincludresult(0);
            measureEntity.setSqft9(0);
            measureEntity.setNumber9singledualzcheckbox(0);
            measureEntity.setNumber9singledualzostring("");
            measureEntity.setNumber9singledresult(0);
            measureEntity.setSqft91(0);
            measureEntity.setNumber91arelocheckbox(0);
            measureEntity.setNumber91areloint(0);
            measureEntity.setNumber91arelocresult(0);
            measureEntity.setNumber91bcutinsupplyvcheckbox(0);
            measureEntity.setNumber91bcutinsupplyint(0);
            measureEntity.setNumber91bcutinsupplyvresult(0);
            measureEntity.setNumber91chcheckbox(0);
            measureEntity.setNumber91chvaint(0);
            measureEntity.setNumber91chvaccutinresult(0);
            measureEntity.setNumber91dducheckbox(0);
            measureEntity.setNumber91dducint(0);
            measureEntity.setNumber91dductresult(0);
            measureEntity.setNumber91enecheckbox(0);
            measureEntity.setNumber91eneint(0);
            measureEntity.setNumber91eneresult(0);
            measureEntity.setNumber91fenlacheckbox(0);
            measureEntity.setNumber91fenlargint(0);
            measureEntity.setNumber91fenlargerresult(0);
            measureEntity.setNumber92notescheckbox(0);
            measureEntity.setNumber92notesstring("");
            measureEntity.setNumber93costperkwcheckbox(0);
            measureEntity.setNumber93costperkwint(0);
            measureEntity.setNumber93costperkwresalt(0);
            measureEntity.setNumber93dckwsdouble(0);
            measureEntity.setNumber93dckwsresalt(0);
            measureEntity.setNumber10installwindowscheckbox(0);
            measureEntity.setNumber10numofwindows("");
            measureEntity.setNumber10totalunitedinchesint(0);
            measureEntity.setNumber10installresult(0);
            measureEntity.setNumber10numofsliders("");
            measureEntity.setNumber10linearfeet("");
            measureEntity.setNumber10linearfeetresult(0);
            measureEntity.setNumber101acutindoorcheckbox(0);
            measureEntity.setNumber101acutindoorint(0);
            measureEntity.setNumber101acutindoorresult(0);
            measureEntity.setNumber101baddheadercheckbox(0);
            measureEntity.setNumber101baddheaderint(0);
            measureEntity.setNumber101baddheaderresult(0);
            measureEntity.setNumber102acutdowncheckbox(0);
            measureEntity.setNumber102acutdownwiresult(0);
            measureEntity.setNumber102belectricalcheckbox(0);
            measureEntity.setNumber102belectricalreresult(0);
            measureEntity.setNumber102cgardencheckbox(0);
            measureEntity.setNumber102cgardenresult(0);
            measureEntity.setNumber102dgardenwicheckbox(0);
            measureEntity.setNumber102dgardenwiresult(0);
            measureEntity.setNumber103allglazingcheckbox(0);
            measureEntity.setNumber104wicheckbox(0);
            measureEntity.setNumber11installpoolcheckbox(0);
            measureEntity.setNumber11installpoolpumpresult(0);
            measureEntity.setNumber12installwholecheckbox(0);
            measureEntity.setNumber12installwholehoint(0);
            measureEntity.setNumber12installwholehoresult(0);
            measureEntity.setNumber13installwacheckbox(0);
            measureEntity.setNumber13installwaterhedouble(0);
            measureEntity.setNumber13installwaresult(0);
            measureEntity.setNumber14permitscheckbox(0);
            measureEntity.setNumber14permitsresult(0);
            measureEntity.setNumber15installbatterycheckbox(0);
            measureEntity.setNumber15installbaresult(0);
            measureEntity.setNumber15ainstallkwdcsocheckbox(0);
            measureEntity.setNumber15ainstallkwdcsodouble(0);
            measureEntity.setNumber15bincludesexcheckbox(0);
            measureEntity.setNumber15csolarcheckbox(0);
            measureEntity.setNumber15dincludessycheckbox(0);
            measureEntity.setNumber15eicheckbox(0);
            measureEntity.setNumber15fcanadiansocheckbox(0);
            measureEntity.setN16checkbox(0);
            measureEntity.setN16int(0);
            measureEntity.setN16result(0);
            measureEntity.setN17checkbox(0);
            measureEntity.setN17result(0);
            measureEntity.setN17sqft(0);
            measureEntity.setN17string("");
            measureEntity.setN18checkbox(0);
            measureEntity.setN18result(0);
            measureEntity.setN18string("");
            measureEntity.setWholecost(0);
            measureEntity.setSolarcost(0);
            measureEntity.setMarginpercentage(0);
            measureEntity.setPgerebateamount(0);
            measureEntity.setPgerebatepoints(0);
            measureEntity.setSmudrebateamount(0);
        } else {
            measureEntity = (MeasureEntity) measures.get(0);
            System.out.println("total = " + measureEntity.getTotal());
        }
        tx.commit();
    } catch (HibernateException e) {
        if (tx != null) {
            tx.rollback();
        }
        e.printStackTrace();
    } finally {
        session.close();
    }
    return measureEntity;
}

From source file:db.manage.PropertiesManage.java

public int receiveCaseIdAddition() {
    int result = -1;
    PropertiesEntity propertiesEntity = null;
    Session session = sessionFactory.openSession();
    Transaction tx = null;/* ww  w .ja  va 2  s.co  m*/
    try {
        tx = session.beginTransaction();
        int i = 0;
        String sql = "SELECT * FROM PROPERTIES WHERE ID = 0";
        SQLQuery query = session.createSQLQuery(sql);
        query.addEntity(PropertiesEntity.class);
        List properties = query.list();
        if (properties.isEmpty()) {
            System.err.println("properties have broken: class PropertiesManage ");
        } else {
            propertiesEntity = (PropertiesEntity) properties.get(0);
        }
        result = propertiesEntity.getCaseNumber();
        result++;
        propertiesEntity.setCaseNumber(result);
        session.update(propertiesEntity);
        tx.commit();
    } catch (HibernateException e) {
        if (tx != null) {
            tx.rollback();
        }
        e.printStackTrace();
    } finally {
        session.close();
    }
    return result;
}

From source file:de.codesourcery.eve.skills.db.dao.InventoryGroupDAO.java

License:Apache License

@Override
public List<InventoryGroup> getBlueprintProductGroups() {

    /*/*from  w  ww  .  j a  v a 2 s  . c om*/
     select distinct g.*from InventoryGroup g
     inner join InventoryType types on g.groupID=types.groupID
     inner join BlueprintType bpos on types.typeID=bpos.productTypeID  
     where 
       g.allowManufacture<>0;  
     */

    return (List<InventoryGroup>) execute(new HibernateCallback<List<InventoryGroup>>() {

        @SuppressWarnings("unchecked")
        @Override
        public List<InventoryGroup> doInSession(Session session) {

            final SQLQuery query = session.createSQLQuery("select distinct g.* from invGroups g, "
                    + " invTypes types , invBlueprintTypes  bpos WHERE "
                    + " g.groupID = types.groupID and types.typeID=bpos.productTypeID "
                    + " and g.allowManufacture<>0 and exists( select * from typeActivityMaterials mats where "
                    + " mats.typeID=bpos.blueprintTypeID and mats.activityID=1 and mats.quantity >0 )");
            query.addEntity(InventoryGroup.class);
            return query.list();
        }
    });
}

From source file:de.codesourcery.eve.skills.db.dao.InventoryTypeDAO.java

License:Apache License

private List<InventoryType> getInventoryTypesByIDs(final Collection<Long> invTypeIds) {
    final Set<Long> ids = new HashSet<>(invTypeIds);

    final List<InventoryType> result = new ArrayList<>();
    for (Iterator<Long> it = ids.iterator(); it.hasNext();) {
        final Long id = it.next();
        InventoryType type = typeByID.get(id);
        if (type != null) {
            result.add(type);/*from   w ww .ja v  a  2  s  . c o  m*/
            it.remove();
        }
    }
    if (ids.isEmpty()) {
        return result;
    }
    return execute(new HibernateCallback<List<InventoryType>>() {

        @Override
        public List<InventoryType> doInSession(Session session) {
            final String idsString = StringUtils.join(ids, ",");
            final SQLQuery query = session
                    .createSQLQuery("SELECT * FROM invTypes WHERE typeID IN ( " + idsString + ")");
            query.addEntity(InventoryType.class);

            final List<InventoryType> notCached = query.list();
            result.addAll(notCached);
            for (InventoryType type : notCached) {
                typeByName.putIfAbsent(type.getName(), type);
                typeByID.putIfAbsent(type.getId(), type);
            }
            return result;
        }
    });
}

From source file:edu.uoc.pelp.model.dao.DeliverDAO.java

License:Open Source License

@Override
public List<Deliver> findLastClassroom(IClassroomID classroom, ActivityID activity) {
    // Get the activity ID and classroom ID
    ClassroomPK classPK = ObjectFactory.getClassroomPK(classroom);
    if (classPK == null) {
        return null;
    }//  www .j av  a 2  s  .c  o  m
    ActivityPK activityPK = ObjectFactory.getActivityPK(activity);
    if (activityPK == null) {
        return null;
    }

    // Get the activity register
    /*Query query=getSession().getNamedQuery("Deliver.findAtivityLastDeliverByClass");
    query.setParameter("semester", activityPK.getSemester());
    query.setParameter("subject", activityPK.getSubject());
    query.setParameter("activityIndex", activityPK.getActivityIndex());
    query.setParameter("classroom", classPK.toString());
    List<edu.uoc.pelp.model.vo.Deliver> list=query.list();*/
    getSession().beginTransaction();
    String SQL = "SELECT d1.* " + "FROM pelp.deliver d1 "
            + "LEFT JOIN pelp.deliver d2 ON d1.semester=d2.semester and d1.subject=d2.subject and d1.activityIndex=d2.activityIndex and d1.userID=d2.userID and d1.deliverIndex<d2.deliverIndex "
            + "WHERE d2.deliverIndex is null " + "and d1.semester=:semester " + "and d1.subject=:subject "
            + "and d1.activityIndex=:activityIndex "
            + "and (d1.classroom=:classroom or d1.laboratory=:classroom) "
            + "ORDER BY d1.semester,d1.subject,d1.activityIndex,d1.userID;";
    SQLQuery query = getSession().createSQLQuery(SQL);
    query.addEntity(edu.uoc.pelp.model.vo.Deliver.class);
    //query.setEntity("d1", edu.uoc.pelp.model.vo.Deliver.class);
    query.setParameter("semester", activityPK.getSemester());
    query.setParameter("subject", activityPK.getSubject());
    query.setParameter("activityIndex", activityPK.getActivityIndex());
    query.setParameter("classroom", classPK.toString());
    List<edu.uoc.pelp.model.vo.Deliver> list = query.list();

    List<Deliver> listDelivers = getDeliverList(list);

    getSession().close();
    // Return the results
    return listDelivers;
}

From source file:es.logongas.fpempresa.dao.empresa.impl.OfertaDAOImplHibernate.java

License:Open Source License

@Override
public List<Oferta> getOfertasUsuarioTitulado(DataSession dataSession, Usuario usuario, Provincia provincia,
        Date fechaInicio, Date fechaFin) {
    Session session = (Session) dataSession.getDataBaseSessionImpl();

    StringBuilder sb = new StringBuilder();

    sb.append("SELECT oferta.*\n" + "FROM\n" + "   oferta   \n"
            + "      INNER JOIN  ofertaciclo   ON oferta.idOferta=ofertaciclo.idOferta \n"
            + "      INNER JOIN  municipio as municipiooferta ON oferta.idMunicipio=municipiooferta.idMunicipio\n"
            + "      INNER JOIN  empresa           ON oferta.idEmpresa=empresa.idEmpresa,\n" + "   usuario \n"
            + "      INNER JOIN  formacionacademica ON usuario.idTitulado=formacionacademica.idTitulado \n"
            + "      INNER JOIN  titulado                     ON usuario.idTitulado=titulado.idTitulado  \n"
            + "      INNER JOIN  municipio as municipiotitulado           ON titulado.idMunicipio=municipiotitulado.idMunicipio\n"
            + "WHERE\n" + "   oferta.cerrada <> 1 AND\n" + "   usuario.idIdentity=? AND\n"
            + "   empresa.idCentro IS NULL AND\n" + "   ofertaciclo.idCiclo=formacionacademica.idCiclo AND\n");

    if (provincia != null) {
        sb.append("   municipiooferta.idProvincia=? AND\n");
    }/*  w w w  .j a  v a 2  s  .  c  om*/
    if (fechaInicio != null) {
        sb.append("   oferta.fecha>=? AND\n");
    }
    if (fechaFin != null) {
        sb.append("   oferta.fecha<=? AND\n");
    }

    sb.append(
            "   not exists (SELECT * FROM  candidato WHERE candidato.idIdentity=usuario.idIdentity AND candidato.idOferta=oferta.idOferta)\n");

    sb.append(" UNION DISTINCT \n");

    sb.append("SELECT oferta.*\n" + "FROM\n" + "   oferta   \n"
            + "      INNER JOIN  ofertaciclo   ON oferta.idOferta=ofertaciclo.idOferta \n"
            + "      INNER JOIN  municipio as municipiooferta ON oferta.idMunicipio=municipiooferta.idMunicipio\n"
            + "      INNER JOIN  empresa           ON oferta.idEmpresa=empresa.idEmpresa,\n" + "   usuario \n"
            + "      INNER JOIN  formacionacademica ON usuario.idTitulado=formacionacademica.idTitulado   \n"
            + "WHERE\n" + "   oferta.cerrada <> 1 AND\n" + "   usuario.idIdentity=? AND\n"
            + "   empresa.idCentro = formacionacademica.idCentro AND\n"
            + "   ofertaciclo.idCiclo=formacionacademica.idCiclo AND\n");
    if (provincia != null) {
        sb.append("   municipiooferta.idProvincia=? AND\n");
    }
    if (fechaInicio != null) {
        sb.append("   oferta.fecha>=? AND\n");
    }
    if (fechaFin != null) {
        sb.append("   oferta.fecha<=? AND\n");
    }

    sb.append(
            "   not exists (SELECT * FROM candidato WHERE candidato.idIdentity=usuario.idIdentity AND candidato.idOferta=oferta.idOferta)");
    sb.append(" ORDER BY fecha DESC");

    SQLQuery sqlQuery = session.createSQLQuery(sb.toString());
    sqlQuery.addEntity(Oferta.class);

    int paramNumber = 0;

    sqlQuery.setInteger(paramNumber++, usuario.getIdIdentity());
    if (provincia != null) {
        sqlQuery.setInteger(paramNumber++, provincia.getIdProvincia());
    }
    if (fechaInicio != null) {
        sqlQuery.setDate(paramNumber++, fechaInicio);
    }
    if (fechaFin != null) {
        sqlQuery.setDate(paramNumber++, fechaFin);
    }
    sqlQuery.setInteger(paramNumber++, usuario.getIdIdentity());
    if (provincia != null) {
        sqlQuery.setInteger(paramNumber++, provincia.getIdProvincia());
    }
    if (fechaInicio != null) {
        sqlQuery.setDate(paramNumber++, fechaInicio);
    }
    if (fechaFin != null) {
        sqlQuery.setDate(paramNumber++, fechaFin);
    }

    return (List<Oferta>) sqlQuery.list();
}

From source file:es.logongas.fpempresa.dao.titulado.impl.TituladoDAOImplHibernate.java

@Override
public List<Titulado> getTituladosSuscritosPorProvinciaOfertaYCiclosOferta(DataSession dataSession,
        Oferta oferta) {/*from w w  w .  ja v a2  s .  c o m*/

    Session session = (Session) dataSession.getDataBaseSessionImpl();

    StringBuilder stringBuilder = new StringBuilder();

    String query = "" + "SELECT DISTINCT titulado.* FROM titulado\n"
            + "NATURAL JOIN tituladoprovincianotificacion\n" + "NATURAL JOIN formacionacademica\n"
            + "WHERE titulado.notificarPorEmail = 1\n" + "AND tituladoprovincianotificacion.idProvincia = (\n"
            + "   SELECT idProvincia\n" + "   FROM municipio\n" + "   INNER JOIN oferta\n"
            + "   ON municipio.idMunicipio = oferta.idMunicipio\n" + "   WHERE oferta.idOferta = ?\n" + "   )\n"
            + "AND formacionacademica.idCiclo IN (\n" + "   SELECT idCiclo\n" + "   FROM ofertaciclo\n"
            + "   WHERE ofertaciclo.idOferta = ?\n" + "   )\n";

    Centro centro = oferta.getEmpresa().getCentro();
    if (centro != null) {
        query += "AND titulado.idTitulado IN (\n" + "   SELECT idTitulado\n" + "   FROM formacionacademica\n"
                + "   WHERE formacionacademica.idCentro = ?\n" + "   )";
    }

    stringBuilder.append(query);

    SQLQuery sqlQuery = session.createSQLQuery(stringBuilder.toString());
    sqlQuery.addEntity(Titulado.class);
    sqlQuery.setInteger(0, oferta.getIdOferta());
    sqlQuery.setInteger(1, oferta.getIdOferta());

    if (centro != null) {
        sqlQuery.setInteger(2, centro.getIdCentro());
    }

    return (List<Titulado>) sqlQuery.list();
}

From source file:gradebook.StudentAssignmentHelper.java

public List getStudents() {

    List<Student> studentList = null;

    String sql = "select * from student";

    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);

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

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

    return studentList;
}

From source file:gradebook.StudentAssignmentHelper.java

public List getAssignments() {

    List<Assignment> assignmentList = null;

    String sql = "SELECT * FROM assignment";

    try {/*from   w w  w . jav a 2  s  . c  om*/
        // Initialize transaction if not already initialized
        if (!this.session.getTransaction().isActive()) {
            session.beginTransaction();
        }

        SQLQuery query = session.createSQLQuery(sql);

        query.addEntity(Assignment.class);

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

    return assignmentList;
}

From source file:gradebook.StudentAssignmentHelper.java

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

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

    try {// w w  w .ja v a  2s.  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("gaid", gaid);

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

    // Return gradebooks
    return studentList;

}