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:modelo.SolicitudesDAO.java

public List<Solicitudes> librosPorCalificar(int id) {
    SessionFactory factory;/*w  ww.  j ava2s .c om*/
    List<Solicitudes> misprestamos = null;
    try {
        factory = new Configuration().configure().buildSessionFactory();
    } catch (Throwable ex) {
        System.err.println("Failed to create sessionFactory object." + ex);
        throw new ExceptionInInitializerError(ex);
    }
    Session session = factory.openSession();
    Transaction tx = null;
    try {
        tx = session.beginTransaction();
        String sql = "SELECT * FROM solicitudes WHERE aceptado = TRUE AND califlibro = FALSE AND usridusuario = "
                + id;
        SQLQuery query = session.createSQLQuery(sql);
        query.addEntity(Solicitudes.class);
        misprestamos = query.list();
        tx.commit();
        return misprestamos;
    } catch (HibernateException e) {
        if (tx != null)
            tx.rollback();
        return null;
    } finally {
        session.close();
    }
}

From source file:nc.noumea.mairie.annuairev2.saisie.dao.impl.GuestDao.java

License:Open Source License

@Override
public List<Guest> findByNomEtService(String nom, String service) {

    String queryString;// www.ja v a 2 s  .  co m

    if (nom != null) {
        if (service == null)
            queryString = "select g.* from " + Guest.TABLENAME + " g where lower(g.nom) like :nom";
        else
            queryString = "select g.* from " + Guest.TABLENAME + " g" + " left join " + Sectorisation.TABLENAME
                    + " s on g." + Guest.JOIN_COLUMNNAME_SERVICE + " = s." + Sectorisation.COLUMNNAME_ID
                    + " where lower(s.libelle) like :serviceName "
                    + " and lower(g.nom) like :nom or lower(g.prenom) like :nom ";
    } else {
        queryString = "select g.* from " + Guest.TABLENAME + " g" + " left join " + Sectorisation.TABLENAME
                + " s on g." + Guest.JOIN_COLUMNNAME_SERVICE + " = s." + Sectorisation.COLUMNNAME_ID
                + " where lower(s.libelle) like :serviceName ";
    }

    SQLQuery query = getCurrentSession().createSQLQuery(queryString);
    query.addEntity(Guest.class);
    if (nom != null)
        query.setParameter("nom", "%" + nom.toLowerCase() + "%");
    if (service != null)
        query.setParameter("serviceName", "%" + service.toLowerCase() + "%");

    return (List<Guest>) query.list();
}

From source file:nc.noumea.mairie.annuairev2.saisie.dao.impl.GuestDao.java

License:Open Source License

@Override
public Guest findByIdentifiant(String identifiant) {

    SQLQuery query = getCurrentSession().createSQLQuery("select * from " + Guest.TABLENAME
            + " where ('G' || lpad(cast(id as varchar(4)), 4, '0')) = :identifiant");
    query.setParameter("identifiant", identifiant);
    query.addEntity(Guest.class);
    return (Guest) query.uniqueResult();
}

From source file:nc.noumea.mairie.annuairev2.saisie.dao.impl.LocalityDao.java

License:Open Source License

@Override
public List<Locality> findByNomEtService(String nom, String service) {
    StringBuilder sqlQuery = new StringBuilder();
    sqlQuery.append("select l.* from " + Locality.TABLENAME + " l ");
    sqlQuery.append(" left join " + Sectorisation.TABLENAME + " s on l." + Locality.JOIN_COLUMNNAME_SERVICE
            + " = s." + Sectorisation.COLUMNNAME_ID);
    sqlQuery.append(" where ");
    sqlQuery.append(nom != null ? "lower(l." + Locality.COLUMNNAME_NOM + ") like :nom " : "");
    sqlQuery.append((service != null && nom != null) ? " and " : "");
    sqlQuery.append(service != null ? " s." + Sectorisation.COLUMNNAME_LIBELLE + " = :serviceName " : "");

    SQLQuery query = getCurrentSession().createSQLQuery(sqlQuery.toString());
    query.addEntity(Locality.class);
    if (nom != null)
        query.setParameter("nom", "%" + nom.toLowerCase() + "%");
    if (service != null)
        query.setParameter("serviceName", service);

    List<Locality> results = (List<Locality>) query.list();
    if (results == null)
        results = new ArrayList<>();

    return results;
}

From source file:net.digitalprimates.persistence.hibernate.utils.services.HibernateService.java

License:Open Source License

public List executeSql(String sql, Class entity) {
    Session session = null;/* w ww.  ja va  2 s .c om*/
    List result = new ArrayList();

    try {
        session = HibernateUtil.getCurrentSession();

        long tStart = new Date().getTime();

        SQLQuery query = session.createSQLQuery(sql);
        if (entity != null) {
            query.addEntity(entity);
        }

        result = query.list();

        long tEnd = new Date().getTime();
        log.debug("{list()}" + (tEnd - tStart) + "ms  size=" + result.size() + "\n sql=" + sql);

    } catch (HibernateException ex) {
        HibernateUtil.rollbackTransaction();
        ex.printStackTrace();
        throw ex;
    } catch (RuntimeException ex) {
        HibernateUtil.rollbackTransaction();
        ex.printStackTrace();
        throw ex;
    } catch (Throwable ex) {
        HibernateUtil.rollbackTransaction();
        ex.printStackTrace();
    }

    return result;
}

From source file:net.lojika.gtfs.dao.impl.StopTimesDaoImpl.java

@Transactional
@Override/*w w w .j av a 2s .com*/
public StopTimes existsStopTimes(String stopId, String tripId) {
    SQLQuery query = getSession()
            .createSQLQuery("select * from stop_times where trip_id =:tripId and stop_id =:stopId");
    query.setParameter("tripId", tripId);
    query.setParameter("stopId", stopId);
    query.addEntity(StopTimes.class);
    List<StopTimes> list = query.list();

    int size = list.size();

    if (size > 1) {
        for (StopTimes stopTimes : list) {
            remove(stopTimes);
            size--;
        }
    }

    return size == 1 ? list.get(0) : null;
}

From source file:net.umpay.mailbill.hql.orm.hibernate.HibernateDao.java

License:Apache License

/**
 * SQL.//from w  w w  .  j a va2  s .co  m
 * 
 * @param page ?.??orderBy?.
 * @param sql sql?.
 * @param values ????,?.
 * 
 * @return , ??.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public Page<T> findSQLPage(final Page<T> page, final String sql, final Object... values) {
    Assert.notNull(page, "page?");

    SQLQuery q = createSQLQuery(sql, values);

    if (page.isAutoCount()) {
        long totalCount = countSqlResult(sql, values);
        page.setTotalCount(totalCount);
    }

    setPageParameter(q, page);

    String className = entityClass.getSimpleName();
    if (!className.equals("Object") && !className.equals("Object[]")) {
        q.addEntity(entityClass);
    }
    List result = q.list();
    page.setResult(result);
    return page;
}

From source file:net.umpay.mailbill.hql.orm.hibernate.HibernateDao.java

License:Apache License

/**
 * SQL.//from   w w w .  j av a2s  . com
 * 
 * @param page ?.
 * @param sql hql?.
 * @param values ???,??.
 * 
 * @return , ??.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public Page<T> findSQLPage(final Page<T> page, final String sql, final Map<String, ?> values) {
    Assert.notNull(page, "page?");

    SQLQuery q = createSQLQuery(sql, values);

    if (page.isAutoCount()) {
        long totalCount = countSqlResult(sql, values);
        page.setTotalCount(totalCount);
    }

    setPageParameter(q, page);

    String className = entityClass.getSimpleName();
    if (!className.equals("Object") && !className.equals("Object[]")) {
        q.addEntity(entityClass);
    }
    List result = q.list();
    page.setResult(result);
    return page;
}

From source file:net.umpay.mailbill.hql.orm.hibernate.SimpleHibernateDao.java

License:Apache License

/**
 * SQL.//ww  w. j av  a2s  .co  m
 * 
 * @param values ????,?.
 */
public <X> List<X> findSQL(final String sql, final Object... values) {
    SQLQuery q = createSQLQuery(sql, values);
    String className = entityClass.getSimpleName();
    if (!className.equals("Object") && !className.equals("Object[]")) {
        q.addEntity(entityClass);
    }
    return q.list();
}

From source file:net.umpay.mailbill.hql.orm.hibernate.SimpleHibernateDao.java

License:Apache License

/**
 * SQL.//from  w  w w  .  j  a  va 2s .c o m
 * 
 * @param values ???,??.
 */
public <X> List<X> findSQL(final String sql, final Map<String, ?> values) {
    SQLQuery q = createSQLQuery(sql, values);
    String className = entityClass.getSimpleName();
    if (!className.equals("Object") && !className.equals("Object[]")) {
        q.addEntity(entityClass);
    }
    return q.list();
}