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.generationcp.middleware.dao.GermplasmDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<Germplasm> getByGIDList(final List<Integer> gids) {

    if (gids.isEmpty()) {
        return new ArrayList<>();
    }/*from w  w  w  . jav  a 2  s .  c o  m*/

    try {
        final StringBuilder queryString = new StringBuilder();
        queryString.append("SELECT {g.*} FROM germplsm g WHERE ");
        queryString.append("g.gid IN( :gids ) ");

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

        return query.list();

    } catch (final HibernateException e) {
        final String message = "Error with getByGIDList(gids=" + gids.toString()
                + GermplasmDAO.QUERY_FROM_GERMPLASM + e.getMessage();
        GermplasmDAO.LOG.error(message, e);
        throw new MiddlewareQueryException(message, e);
    }
}

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

License:Open Source License

public Germplasm getByLGid(final Integer lgid) {
    try {//from www . ja  v  a 2 s. c  o m
        final StringBuilder queryString = new StringBuilder();
        queryString.append("SELECT g.* FROM germplsm g WHERE g.deleted = 0 AND lgid=:lgid LIMIT 1");

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

        return (Germplasm) query.uniqueResult();

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

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

License:Open Source License

/**
 * Only return germplasm with no group assigned (mgid = 0 or mgid is null)
 * @param gids/*from   w w w  .  j a  v a2  s.c o m*/
 * @return
 */
public List<Germplasm> getGermplasmWithoutGroup(final List<Integer> gids) {

    if (gids.isEmpty()) {
        return new ArrayList<>();
    }

    try {
        final StringBuilder queryString = new StringBuilder();
        queryString.append("SELECT {g.*} FROM germplsm g WHERE ");
        queryString.append("g.gid IN( :gids ) AND (g.mgid = 0 || g.mgid IS NULL)");

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

        return query.list();

    } catch (final HibernateException e) {
        final String message = "Error with getGermplasmWithoutGroup(gids=" + gids.toString() + ") "
                + e.getMessage();
        GermplasmDAO.LOG.error(message, e);
        throw new MiddlewareQueryException(message, e);
    }
}

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

License:Open Source License

/**
 * Get Germplasm Lists with names like Q or germplasms with name like Q or
 * gid equal to Q// w ww .  j  av  a 2s  . c  om
 *
 * @param q
 * @param o
 *            - like or equal
 * @return List of GermplasmLists
 */
@SuppressWarnings("unchecked")
public List<GermplasmList> searchForGermplasmLists(final String searchedString, final String programUUID,
        final Operation o) {
    final String q = searchedString.trim();
    if ("".equals(q)) {
        return new ArrayList<>();
    }
    try {
        final SQLQuery query;

        if (o.equals(Operation.EQUAL)) {
            query = this.getSession().createSQLQuery(this.getSearchForGermplasmListsQueryString(
                    GermplasmListDAO.SEARCH_FOR_GERMPLASM_LIST_EQUAL, programUUID));
            query.setParameter("gidLength", q.length());
            query.setParameter("q", q);
            query.setParameter("qNoSpaces", q.replace(" ", ""));
            query.setParameter("qStandardized", GermplasmDataManagerUtil.standardizeName(q));
        } else {
            if (q.contains("%") || q.contains("_")) {
                query = this.getSession().createSQLQuery(this.getSearchForGermplasmListsQueryString(
                        GermplasmListDAO.SEARCH_FOR_GERMPLASM_LIST_GID_LIKE, programUUID));
                query.setParameter("q", q);
                query.setParameter("qNoSpaces", q.replace(" ", ""));
                query.setParameter("qStandardized", GermplasmDataManagerUtil.standardizeName(q));
            } else {
                query = this.getSession().createSQLQuery(this.getSearchForGermplasmListsQueryString(
                        GermplasmListDAO.SEARCH_FOR_GERMPLASM_LIST, programUUID));
                query.setParameter("gidLength", q.length());
                query.setParameter("q", q + "%");
                query.setParameter("qNoSpaces", q.replace(" ", "") + "%");
                query.setParameter("qStandardized", GermplasmDataManagerUtil.standardizeName(q) + "%");
            }

        }
        query.setParameter("gid", q);
        query.setParameterList(GermplasmListDAO.HIDDEN_LIST_TYPES_PARAM,
                GermplasmListDAO.HIDDEN_LIST_TYPES_ON_SEARCH);

        if (programUUID != null) {
            query.setParameter(GermplasmListDAO.PROGRAM_UUID, programUUID);
        }

        query.addEntity("listnms", GermplasmList.class);
        return query.list();

    } catch (final Exception e) {
        final String errorMessage = "Error with searchGermplasmLists(" + q + ") " + e.getMessage();
        GermplasmListDAO.LOG.error(errorMessage);
        throw new MiddlewareQueryException(errorMessage, e);
    }
}

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

License:Open Source License

/**
 * Get the names associated with a GID//  w ww .  ja va2  s.  c  om
 *
 * @param gid the gid for which we are getting names
 * @param status the status of the gid. Note if status is null or 0 we will omit deleted values i.e. status will be set to 9
 * @param type a list of name types to retrieve. Note if type is null or empty it will be omited from the query
 * @return
 */
@SuppressWarnings("unchecked")
public List<Name> getByGIDWithListTypeFilters(final Integer gid, final Integer status,
        final List<Integer> type) {
    try {
        if (gid != null) {
            final StringBuilder queryString = new StringBuilder();
            queryString.append("SELECT ");
            queryString.append("CASE n.nstat ");
            queryString.append("   WHEN NOT 1 THEN 9999 ");
            queryString.append("   ELSE n.nstat ");
            queryString.append("END AS 'nameOrdering', ");
            queryString.append("{n.*} from names n WHERE n.gid = :gid ");

            if (status != null && status != 0) {
                queryString.append("AND n.nstat = :nstat ");
            } else {
                queryString.append("AND n.nstat != 9 ");
            }

            if (type != null && !type.isEmpty()) {
                queryString.append("AND n.ntype IN (:ntype) ");
            }

            queryString.append("ORDER BY nameOrdering, n.nval");

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

            if (status != null && status != 0) {
                query.setParameter("nstat", status);
            }

            if (type != null && !type.isEmpty()) {
                query.setParameterList("ntype", type);
            }

            return query.list();
        }

    } catch (final HibernateException e) {
        final String message = "Error with getByGIDWithFilters(gid=" + gid + ", status=" + status + ", type="
                + type + ") query from Name " + e.getMessage();
        NameDAO.LOG.error(message);
        throw new MiddlewareQueryException(message, e);
    }
    return new ArrayList<>();
}

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

License:Open Source License

@SuppressWarnings("unchecked")
public List<Name> getPreferredIdsByListId(final Integer listId) {
    try {/*from  w  ww . j  ava  2s.  co  m*/
        if (listId != null) {
            final SQLQuery query = this.getSession().createSQLQuery(Name.GET_PREFERRED_IDS_BY_LIST_ID);
            query.setParameter("listId", listId);
            query.addEntity("n", Name.class);
            return query.list();
        }
    } catch (final HibernateException e) {
        final String message = "Error with getPreferredIdsByListId(listId=" + listId + ") query from Name "
                + e.getMessage();
        NameDAO.LOG.error(message);
        throw new MiddlewareQueryException(message, e);
    }
    return new ArrayList<>();
}

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

License:Open Source License

@SuppressWarnings("unchecked")
public List<Name> getNamesByTypeAndGIDList(final Integer nameType, final List<Integer> gidList) {
    List<Name> returnList = new ArrayList<>();
    if (gidList != null && !gidList.isEmpty()) {
        try {/*from  www .  ja  v  a  2s.  c o  m*/
            final String sql = "SELECT {n.*}" + " FROM names n" + " WHERE n.ntype = :nameType"
                    + " AND n.gid in (:gidList)";
            final SQLQuery query = this.getSession().createSQLQuery(sql);
            query.addEntity("n", Name.class);
            query.setParameter("nameType", nameType);
            query.setParameterList("gidList", gidList);
            returnList = query.list();
        } catch (final HibernateException e) {
            throw new MiddlewareQueryException("Error with getNamesByTypeAndGIDList(nameType=" + nameType
                    + ", gidList=" + gidList + "): " + e.getMessage(), e);
        }
    }
    return returnList;
}

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

License:Open Source License

@SuppressWarnings("unchecked")
public List<NumericLevel> getByFactorAndDatasetID(Factor factor, Integer datasetId)
        throws MiddlewareQueryException {
    try {//from   ww w  .ja v  a2 s.  c om
        SQLQuery query = getSession().createSQLQuery(NumericLevel.GET_BY_FACTOR_AND_REPRESNO);
        query.setParameter("factorid", factor.getFactorId());
        query.setParameter("labelid", factor.getId());
        query.setParameter("represno", datasetId);

        query.addEntity("ln", NumericLevel.class);

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

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

License:Open Source License

public Project getLastOpenedProject(final Integer userId) throws MiddlewareQueryException {
    try {//from   w w  w .  j  av a  2 s. c  o  m
        if (userId != null) {
            final StringBuilder sb = new StringBuilder();
            sb.append("SELECT {w.*} FROM workbench_project w ")
                    .append("INNER JOIN workbench_project_user_info r ON w.project_id = r.project_id ")
                    .append("WHERE r.user_id = :userId AND r.last_open_date IS NOT NULL ORDER BY r.last_open_date DESC LIMIT 1 ;");

            final SQLQuery query = this.getSession().createSQLQuery(sb.toString());
            query.addEntity("w", Project.class);
            query.setParameter("userId", userId);

            @SuppressWarnings("unchecked")
            final List<Project> projectList = query.list();

            return !projectList.isEmpty() ? projectList.get(0) : null;
        }
    } catch (final HibernateException e) {
        throw new MiddlewareQueryException(
                "Error with getLastOpenedProject(userId=" + userId + ") query from Project " + e.getMessage(),
                e);
    }
    return null;
}

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

License:Open Source License

public Project getLastOpenedProjectAnyUser() throws MiddlewareQueryException {
    try {// www .j  a va 2  s  . c o m

        final StringBuilder sb = new StringBuilder();
        sb.append("SELECT {w.*} FROM workbench_project w ")
                .append("INNER JOIN workbench_project_user_info r ON w.project_id = r.project_id ")
                .append("WHERE r.last_open_date IS NOT NULL ORDER BY r.last_open_date DESC LIMIT 1 ;");

        final SQLQuery query = this.getSession().createSQLQuery(sb.toString());
        query.addEntity("w", Project.class);

        @SuppressWarnings("unchecked")
        final List<Project> projectList = query.list();

        return !projectList.isEmpty() ? projectList.get(0) : null;

    } catch (final HibernateException e) {
        throw new MiddlewareQueryException(
                "Error with getLastOpenedProjectAnyUser(" + ") query from Project " + e.getMessage(), e);
    }
}