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("rawtypes")
public Germplasm getByGIDWithPrefName(final Integer gid) {
    try {/*w w w .ja  v  a  2  s  .  c  o m*/
        if (gid != null) {
            final SQLQuery query = this.getSession().createSQLQuery(Germplasm.GET_BY_GID_WITH_PREF_NAME);
            query.addEntity("g", Germplasm.class);
            query.addEntity("n", Name.class);
            query.setParameter("gid", gid);
            final List results = query.list();

            if (!results.isEmpty()) {
                final Object[] result = (Object[]) results.get(0);
                if (result != null) {
                    final Germplasm germplasm = (Germplasm) result[0];
                    final Name prefName = (Name) result[1];
                    germplasm.setPreferredName(prefName);
                    return germplasm;
                }
            }
        }
    } catch (final HibernateException e) {
        final String errorMessage = "Error with getByGIDWithPrefName(gid=" + gid + ") from Germplasm: "
                + e.getMessage();
        GermplasmDAO.LOG.error(errorMessage, e);
        throw new MiddlewareQueryException(errorMessage, e);
    }
    return null;
}

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

License:Open Source License

@SuppressWarnings("rawtypes")
public Germplasm getByGIDWithPrefAbbrev(final Integer gid) {
    try {//from w  w w. jav a2 s . co m
        if (gid != null) {
            final SQLQuery query = this.getSession().createSQLQuery(Germplasm.GET_BY_GID_WITH_PREF_ABBREV);
            query.addEntity("g", Germplasm.class);
            query.addEntity("n", Name.class);
            query.addEntity("abbrev", Name.class);
            query.setParameter("gid", gid);
            final List results = query.list();

            if (results.isEmpty()) {
                return null;
            }
            final Object[] result = (Object[]) results.get(0);
            final Germplasm germplasm = (Germplasm) result[0];
            final Name prefName = (Name) result[1];
            final Name prefAbbrev = (Name) result[2];
            germplasm.setPreferredName(prefName);
            if (prefAbbrev != null) {
                germplasm.setPreferredAbbreviation(prefAbbrev.getNval());
            }
            return germplasm;
        }
    } catch (final HibernateException e) {
        final String errorMessage = "Error with getByGIDWithPrefAbbrev(gid=" + gid
                + GermplasmDAO.QUERY_FROM_GERMPLASM + e.getMessage();
        GermplasmDAO.LOG.error(errorMessage, e);
        throw new MiddlewareQueryException(errorMessage, e);
    }
    return null;
}

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

License:Open Source License

public List<Germplasm> getProgenitorsByGIDWithPrefName(final Integer gid) {
    Preconditions.checkNotNull(gid);//  w  w w. j a v a2  s .  c  o m
    try {
        final List<Germplasm> progenitors = new ArrayList<>();

        final SQLQuery query = this.getSession()
                .createSQLQuery(Germplasm.GET_PROGENITORS_BY_GIDS_WITH_PREF_NAME);
        query.addScalar("gid");
        query.addEntity("g", Germplasm.class);
        query.addEntity("n", Name.class);
        query.addScalar("malePedigree");
        query.setParameterList("gidList", Lists.newArrayList(gid));
        final List<Object[]> results = query.list();
        for (final Object[] result : results) {
            final Germplasm germplasm = (Germplasm) result[1];
            final Name prefName = (Name) result[2];
            germplasm.setPreferredName(prefName);
            progenitors.add(germplasm);
        }

        return progenitors;
    } catch (final HibernateException e) {
        final String errorMessage = "Error with getProgenitorsByGIDWithPrefName(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

public Map<Integer, List<GermplasmParent>> getParentsFromProgenitorsForGIDsMap(final List<Integer> gids) {
    Preconditions.checkNotNull(gids);/*from w w  w  .  j  a v a 2s  .c  om*/
    Preconditions.checkArgument(!gids.isEmpty());

    final Map<Integer, List<GermplasmParent>> map = new HashMap<>();
    try {
        final SQLQuery query = this.getSession()
                .createSQLQuery(Germplasm.GET_PROGENITORS_BY_GIDS_WITH_PREF_NAME);
        query.addScalar("gid");
        query.addEntity("g", Germplasm.class);
        query.addEntity("n", Name.class);
        query.addScalar("malePedigree");
        query.setParameterList("gidList", gids);
        final List<Object[]> results = query.list();

        List<GermplasmParent> progenitors = new ArrayList<>();
        Integer lastGid = 0;
        for (final Object[] result : results) {
            final Integer crossGid = (Integer) result[0];
            if (lastGid == 0) {
                lastGid = crossGid;
            }
            if (!crossGid.equals(lastGid)) {
                map.put(lastGid, progenitors);
                lastGid = crossGid;
                progenitors = new ArrayList<>();
            }
            final Germplasm germplasm = (Germplasm) result[1];
            final Name prefName = (Name) result[2];
            final String pedigree = (String) result[3];
            germplasm.setPreferredName(prefName);
            progenitors.add(new GermplasmParent(germplasm.getGid(), prefName.getNval(), pedigree));
        }
        // Set last cross GID to map
        map.put(lastGid, progenitors);

        return map;
    } catch (final HibernateException e) {
        final String errorMessage = "Error with getProgenitorsForGIDsMap(gids=" + gids
                + 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

public List<Germplasm> getManagementNeighbors(final Integer gid, final int start, final int numOfRows) {
    final List<Germplasm> toreturn = new ArrayList<>();
    try {// w  w  w  .j a v  a2s  .com
        if (gid != null) {
            final SQLQuery query = this.getSession().createSQLQuery(Germplasm.GET_MANAGEMENT_NEIGHBORS);
            query.addEntity("g", Germplasm.class);
            query.addEntity("n", Name.class);
            query.setParameter("gid", gid);

            query.setFirstResult(start);
            query.setMaxResults(numOfRows);

            for (final Object resultObject : query.list()) {
                final Object[] result = (Object[]) resultObject;
                final Germplasm germplasm = (Germplasm) result[0];
                final Name prefName = (Name) result[1];
                germplasm.setPreferredName(prefName);
                toreturn.add(germplasm);
            }
        }

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

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

License:Open Source License

public List<Germplasm> getGroupRelatives(final Integer gid, final int start, final int numRows) {
    final List<Germplasm> toreturn = new ArrayList<>();
    try {/*from  ww  w .  j a v  a 2s  .  com*/
        if (gid != null) {
            final SQLQuery query = this.getSession().createSQLQuery(Germplasm.GET_GROUP_RELATIVES);
            query.addEntity("g", Germplasm.class);
            query.addEntity("n", Name.class);
            query.setParameter("gid", gid);

            query.setFirstResult(start);
            query.setMaxResults(numRows);

            for (final Object resultObject : query.list()) {
                final Object[] result = (Object[]) resultObject;
                final Germplasm germplasm = (Germplasm) result[0];
                final Name prefName = (Name) result[1];
                germplasm.setPreferredName(prefName);
                toreturn.add(germplasm);
            }
        }
    } catch (final HibernateException e) {
        final String errorMessage = "Error with getGroupRelatives(gid=" + gid
                + GermplasmDAO.QUERY_FROM_GERMPLASM + e.getMessage();
        GermplasmDAO.LOG.error(errorMessage, e);
        throw new MiddlewareQueryException(errorMessage, e);
    }
    return toreturn;
}

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

License:Open Source License

public List<Germplasm> getChildren(final Integer gid, final char methodType) {
    final List<Germplasm> toreturn = new ArrayList<>();
    try {//ww w  . ja  v a 2 s  .  co  m
        final String queryString = methodType == 'D' ? Germplasm.GET_DERIVATIVE_CHILDREN
                : Germplasm.GET_MAINTENANCE_CHILDREN;
        final SQLQuery query = this.getSession().createSQLQuery(queryString);
        query.addEntity("g", Germplasm.class);
        query.addEntity("n", Name.class);
        query.setParameter("gid", gid);

        for (final Object resultObject : query.list()) {
            final Object[] result = (Object[]) resultObject;
            final Germplasm germplasm = (Germplasm) result[0];
            final Name prefName = (Name) result[1];
            germplasm.setPreferredName(prefName);
            toreturn.add(germplasm);
        }

    } catch (final HibernateException e) {
        final String errorMessage = "Error with getChildren(gid=" + gid + ", methodType=" + methodType
                + ") query: " + e.getMessage();
        GermplasmDAO.LOG.error(errorMessage, e);
        throw new MiddlewareQueryException(errorMessage, e);
    }
    return toreturn;

}

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

License:Open Source License

@SuppressWarnings("unchecked")
public List<Germplasm> getByLocationId(final String name, final int locationID) {
    try {//from   w  w  w .ja v a 2s . c o m
        final StringBuilder queryString = new StringBuilder();
        queryString.append("SELECT {g.*} FROM germplsm g JOIN names n ON g.gid = n.gid WHERE ");
        queryString.append("n.nval = :name ");
        queryString.append("AND g.glocn = :locationID ");

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

        return query.list();

    } catch (final HibernateException e) {
        final String message = "Error with getByLocationId(name=" + name + ", locationID=" + locationID
                + 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

@SuppressWarnings("rawtypes")
public Germplasm getByGIDWithMethodType(final Integer gid) {
    try {//from w  w w . j a v  a2 s. co  m
        if (gid != null) {
            final SQLQuery query = this.getSession().createSQLQuery(Germplasm.GET_BY_GID_WITH_METHOD_TYPE);
            query.addEntity("g", Germplasm.class);
            query.addEntity("m", Method.class);
            query.setParameter("gid", gid);
            final List results = query.list();

            if (!results.isEmpty()) {
                final Object[] result = (Object[]) results.get(0);
                if (result != null) {
                    final Germplasm germplasm = (Germplasm) result[0];
                    final Method method = (Method) result[1];
                    germplasm.setMethod(method);
                    return germplasm;
                }
            }
        }
    } catch (final HibernateException e) {
        final String message = "Error with getByGIDWithMethodType(gid=" + gid + ") from Germplasm: "
                + e.getMessage();
        GermplasmDAO.LOG.error(message, e);
        throw new MiddlewareQueryException(message, e);
    }
    return null;
}

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

License:Open Source License

@SuppressWarnings("unchecked")
public List<Germplasm> getByGIDRange(final int startGID, final int endGID) {
    try {//from   w w w .j  a va  2 s. c  o  m
        final StringBuilder queryString = new StringBuilder();
        queryString.append("SELECT {g.*} FROM germplsm g WHERE ");
        queryString.append("g.gid >= :startGID ");
        queryString.append("AND g.gid <= :endGID ");

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

        return query.list();

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