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(String tableAlias, Class entityType);

Source Link

Document

Declare a "root" entity.

Usage

From source file:org.bedework.util.hibernate.HibSessionImpl.java

License:Apache License

/** Create a sql query ready for parameter replacement or execution.
 *
 * @param s             String hibernate query
 * @param returnAlias// w  w  w .j  a v a2 s  . com
 * @param returnClass
 * @throws HibException
 */
@Override
public void createSQLQuery(final String s, final String returnAlias, final Class returnClass)
        throws HibException {
    if (exc != null) {
        // Didn't hear me last time?
        throw new HibException(exc);
    }

    try {
        SQLQuery sq = sess.createSQLQuery(s);
        sq.addEntity(returnAlias, returnClass);

        q = sq;
        crit = null;
    } catch (Throwable t) {
        handleException(t);
    }
}

From source file:org.bonitasoft.engine.persistence.SQLQueryBuilder.java

License:Open Source License

private void setReturnType(String builtQuery, SQLQuery sqlQuery) {
    if (isCountQuery(builtQuery)) {
        sqlQuery.addScalar("count", LongType.INSTANCE);
    } else {/*from   www  .j  av  a  2 s  .co  m*/
        String hqlAlias = classAliasMappings.get(entityType.getName());
        String sqlAlias = hqlToSqlAlias.containsKey(hqlAlias) ? hqlAlias.replace("user", "user_") : hqlAlias;
        Class<? extends PersistentObject> entityClass = interfaceToClassMapping.get(entityType.getName());
        sqlQuery.addEntity(sqlAlias, entityClass.getName());
    }
}

From source file:org.generationcp.middleware.dao.AttributeDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<Attribute> getAttributeValuesByTypeAndGIDList(final Integer attributeType,
        final List<Integer> gidList) {
    List<Attribute> returnList = new ArrayList<>();
    if (gidList != null && !gidList.isEmpty()) {
        try {/*  w w w .ja va  2 s . com*/
            final String sql = "SELECT {a.*}" + " FROM atributs a" + " WHERE a.atype=:attributeType"
                    + " AND a.gid in (:gidList)";
            final SQLQuery query = this.getSession().createSQLQuery(sql);
            query.addEntity("a", Attribute.class);
            query.setParameter("attributeType", attributeType);
            query.setParameterList("gidList", gidList);
            returnList = query.list();
        } catch (final HibernateException e) {
            throw new MiddlewareQueryException("Error with getAttributeValuesByTypeAndGIDList(attributeType="
                    + attributeType + ", gidList=" + gidList + "): " + e.getMessage(), e);
        }
    }
    return returnList;
}

From source file:org.generationcp.middleware.dao.AttributeDAO.java

License:Open Source License

public Attribute getAttribute(final Integer gid, final String attributeName) {
    Attribute attribute = null;/* w w  w .  j a  v a  2s.  c o m*/
    try {
        final String sql = "SELECT {a.*} FROM atributs a INNER JOIN udflds u ON (a.atype=u.fldno)"
                + " WHERE a.gid = :gid AND u.ftable='ATRIBUTS' and u.fcode=:name";
        final SQLQuery query = this.getSession().createSQLQuery(sql);
        query.addEntity("a", Attribute.class);
        query.setParameter("gid", gid);
        query.setParameter("name", attributeName);
        final List<Attribute> attributes = query.list();
        if (!attributes.isEmpty()) {
            attribute = attributes.get(0);
        }

    } catch (final HibernateException e) {
        throw new MiddlewareQueryException(
                "Error with getAttribute(gidList=" + gid + ", " + attributeName + "): " + e.getMessage(), e);
    }
    return attribute;
}

From source file:org.generationcp.middleware.dao.CharacterLevelDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<CharacterLevel> getByFactorAndDatasetID(Factor factor, Integer datasetId)
        throws MiddlewareQueryException {
    try {//  w  w  w.j av  a 2 s  .c o  m
        SQLQuery query = getSession().createSQLQuery(CharacterLevel.GET_BY_FACTOR_AND_REPRESNO);
        query.setParameter("factorid", factor.getFactorId());
        query.setParameter("labelid", factor.getId());
        query.setParameter("represno", datasetId);

        query.addEntity("lc", CharacterLevel.class);

        return query.list();
    } catch (HibernateException e) {
        throw new MiddlewareQueryException("Error with getByFactorAndDatasetID(factor=" + factor
                + ", datasetId=" + datasetId + ") query from CharacterLevel: " + e.getMessage(), e);
    }
}

From source file:org.generationcp.middleware.dao.dms.GeolocationDao.java

License:Open Source License

public List<Geolocation> getEnvironmentGeolocations(final Integer studyId) {
    List<Geolocation> returnList = new ArrayList<>();
    if (studyId != null) {
        try {//from w w  w.j  a  v a 2  s  . co  m
            final String sql = "SELECT DISTINCT g.* " + //
                    " FROM nd_geolocation g " + //
                    " INNER JOIN nd_experiment exp ON (exp.nd_geolocation_id = g.nd_geolocation_id) " + //
                    " INNER JOIN project envdataset on (envdataset.project_id = exp.project_ID) " + //
                    " WHERE envdataset.study_id = :studyId and envdataset.dataset_type_id = "
                    + DatasetTypeEnum.SUMMARY_DATA.getId();
            final SQLQuery query = this.getSession().createSQLQuery(sql);
            query.addEntity("g", Geolocation.class);
            query.setParameter("studyId", studyId);
            returnList = query.list();
        } catch (final HibernateException e) {
            throw new MiddlewareQueryException(
                    "Error with getEnvironmentGeolocations(studyId=" + studyId + "): " + e.getMessage(), e);
        }
    }
    return returnList;
}

From source file:org.generationcp.middleware.dao.FactorDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<Factor> getByRepresentationID(Integer representationId) throws MiddlewareQueryException {
    try {/*  w  ww.j  a v  a2 s . com*/
        SQLQuery query = getSession().createSQLQuery(Factor.GET_BY_REPRESENTATION_ID);
        query.setParameter("representationId", representationId);
        query.addEntity("f", Factor.class);

        List<Factor> results = query.list();
        return results;
    } catch (HibernateException e) {
        throw new MiddlewareQueryException("Error with getByRepresentationID(representationId="
                + representationId + ") query from Factor: " + e.getMessage(), e);
    }
}

From source file:org.generationcp.middleware.dao.FactorDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
public Factor getFactorOfDatasetGivenTraitid(Integer representationId, Integer traitid)
        throws MiddlewareQueryException {
    try {//from  ww w .j  ava  2s  . c o  m
        SQLQuery query = getSession().createSQLQuery(Factor.GET_FACTOR_OF_DATASET_GIVEN_TRAITID);
        query.setParameter("representationId", representationId);
        query.setParameter("traitid", traitid);
        query.addEntity("f", Factor.class);

        List<Factor> results = query.list();
        if (!results.isEmpty()) {
            Factor factor = results.get(0);
            return factor;
        }

        return null;
    } catch (HibernateException e) {
        throw new MiddlewareQueryException("Error with getFactorOfDatasetGivenTid(representationId="
                + representationId + ", traitid = " + traitid + ") query from Factor: " + e.getMessage(), e);
    }
}

From source file:org.generationcp.middleware.dao.GermplasmDAO.java

License:Open Source License

@Override
public Germplasm getById(final Integer gid) {
    try {//from  w w  w. jav  a 2 s  .  co  m
        final StringBuilder queryString = new StringBuilder();
        queryString.append("SELECT g.* FROM germplsm g WHERE g.deleted = 0 AND gid=:gid LIMIT 1");

        final SQLQuery query = this.getSession().createSQLQuery(queryString.toString());
        query.setParameter("gid", gid);
        query.addEntity("g", Germplasm.class);

        return (Germplasm) query.uniqueResult();

    } catch (final HibernateException e) {
        final String errorMessage = "Error with getById(gid=" + gid + GermplasmDAO.QUERY_FROM_GERMPLASM
                + e.getMessage();
        GermplasmDAO.LOG.error(errorMessage, e);
        throw new MiddlewareQueryException(errorMessage, e);
    }
}

From source file:org.generationcp.middleware.dao.GermplasmDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<Germplasm> getByNamePermutations(final String name, final Operation operation, final int start,
        final int numOfRows) {

    // Converting supplied value to combination of names that can exists in names
    final List<String> names = GermplasmDataManagerUtil.createNamePermutations(name);

    if (names == null || names.isEmpty()) {
        return new ArrayList<>();
    }//  ww  w  .  ja va  2  s.  c om

    try {

        final String originalName = names.get(0);
        final String standardizedName = names.get(1);
        final String noSpaceName = names.get(2);

        // Search using = by default
        SQLQuery query = this.getSession().createSQLQuery(Germplasm.GET_BY_NAME_ALL_MODES_USING_EQUAL);
        if (operation == Operation.LIKE) {
            query = this.getSession().createSQLQuery(Germplasm.GET_BY_NAME_ALL_MODES_USING_LIKE);
        }

        // Set the parameters
        query.setParameter("name", originalName);
        query.setParameter("noSpaceName", noSpaceName);
        query.setParameter("standardizedName", standardizedName);

        query.addEntity("g", Germplasm.class);
        query.setFirstResult(start);
        query.setMaxResults(numOfRows);

        return query.list();
    } catch (final HibernateException e) {
        final String errorMessage = "Error with getByName(names=" + names + GermplasmDAO.QUERY_FROM_GERMPLASM
                + e.getMessage();
        GermplasmDAO.LOG.error(errorMessage, e);
        throw new MiddlewareQueryException(errorMessage, e);
    }
}