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.jcvi.ometa.hibernate.dao.EventDAO.java

License:Open Source License

public List<Event> getAllEvents(Long flexId, String identifier, String sSearch, String sortCol, String sortDir,
        int start, int count, String fromd, String tod, Session session) throws DAOException {
    List<Event> eventList = new ArrayList<Event>();
    try {/*  ww  w.  ja v a2  s .  c  om*/
        List results = null;

        String sql = " select E.*, S.sample_name sample, CONCAT(A.actor_last_name,',',A.actor_first_name) user, LV.lkuvlu_name eventType  "
                + " from event E " + "   left join sample S on E.event_sampl_id=S.sample_id "
                + "   left join lookup_value LV on E.event_type_lkuvl_id=LV.lkuvlu_id "
                + "   left join actor A on E.event_actor_created_by=A.actor_id where ";

        if ("Sample".equals(identifier))
            sql += "E.event_sampl_id=";
        else if ("Eventlist".equals(identifier))
            sql += "E.event_projet_id=";
        else if ("Event".equals(identifier))
            sql += "E.event_id=";
        else
            sql += "E.event_sampl_id is null and E.event_projet_id=";
        sql += flexId;

        if (sSearch != null && !sSearch.isEmpty()) {
            sSearch = "%" + sSearch + "%";
            sql += " and (LOWER(LV.lkuvlu_name) like '" + sSearch + "' or LOWER(S.sample_name) like '" + sSearch
                    + "' " + " or ((LOWER(A.actor_first_name) like '" + sSearch
                    + "' or LOWER(A.actor_last_name) like '" + sSearch + "')))";
        }

        if (fromd != null && !fromd.isEmpty())
            sql += " and date(E.event_create_date)>='" + fromd + "'";
        if (tod != null && !tod.isEmpty())
            sql += " and date(E.event_create_date)<='" + tod + "'";

        if (sortCol != null && !sortCol.isEmpty() && sortDir != null && !sortDir.isEmpty()) {
            sql += " order by";
            if (sortCol.equals("event"))
                sql += " eventType ";
            else if (sortCol.equals("user"))
                sql += " user ";
            else if (sortCol.equals("sample"))
                sql += " sample ";
            else if (sortCol.equals("date"))
                sql += " event_create_date ";
            sql += sortDir;
        }

        SQLQuery query = session.createSQLQuery(sql);
        query.addEntity("E", Event.class);
        if (start >= 0 && count >= 0) {
            query.setFirstResult(start);
            query.setMaxResults(count);
        }
        results = query.list();

        if (results != null) {
            for (Object result : results) {
                eventList.add((Event) result);
            }
        }
    } catch (Exception ex) {
        throw new DAOException(ex);
    }

    return eventList;
}

From source file:org.jcvi.ometa.hibernate.dao.EventDAO.java

License:Open Source License

public List<Event> getUniqueEventTypes(Session session) throws DAOException {
    List<Event> eventList;

    try {/*from w w w  .j  av a  2  s. c o  m*/
        String sql = " select E.* from event E, lookup_value LV " + " where E.event_type_lkuvl_id=LV.lkuvlu_id "
                + " group by E.event_type_lkuvl_id order by LV.lkuvlu_name asc ";
        SQLQuery query = session.createSQLQuery(sql);
        query.addEntity("E", Event.class);

        eventList = query.list();
    } catch (Exception ex) {
        throw new DAOException(ex);
    }

    return eventList;
}

From source file:org.jcvi.ometa.hibernate.dao.EventMetaAttributeDAO.java

License:Open Source License

/** get all unique meta-attributes */
public List<EventMetaAttribute> readAllUnique(Session session) throws DAOException {
    List<EventMetaAttribute> attributeList = new ArrayList<EventMetaAttribute>();
    try {/*www . ja v  a  2 s . c o  m*/
        String sql = " select EMA.*,LV.lkuvlu_name " + " from event_meta_attribute EMA, "
                + " (select evenma_id, max(evenma_create_date) from event_meta_attribute group by evenma_lkuvlu_attribute_id) EMAU, "
                + " lookup_value LV "
                + " where EMA.evenma_id = EMAU.evenma_id and EMA.evenma_lkuvlu_attribute_id=LV.lkuvlu_id "
                + " order by LV.lkuvlu_name ";
        SQLQuery query = session.createSQLQuery(sql);
        query.addEntity("EMA", EventMetaAttribute.class);

        attributeList = query.list();
    } catch (Exception ex) {
        throw new DAOException(ex);
    }

    return attributeList;
}

From source file:org.jcvi.ometa.hibernate.dao.ProjectDAO.java

License:Open Source License

public List<Project> getChildProjects(Long projectId, Session session) throws DAOException {
    List<Project> returnVal = new ArrayList<Project>();
    try {//w  ww  .j  a  va 2 s .  c  o m
        SQLQuery query = session.createSQLQuery(CHILD_PROJECTS_SQL_QUERY);
        /*query.addScalar(RTN_PROJECT_NAME, Hibernate.STRING );
        query.addScalar(RTN_PROJECT_ID, Hibernate.LONG );*/
        query.setLong("parantId", projectId); //ParentProjectId
        query.addEntity("P", Project.class);
        returnVal = query.list();

    } catch (Exception ex) {
        throw new DAOException(ex);
    }

    return returnVal;
}

From source file:org.jcvi.ometa.hibernate.dao.ProjectMetaAttributeDAO.java

License:Open Source License

/**
 * get all unique meta-attributes/*from   w  ww  .j a va2s  .  co  m*/
 */
public List<ProjectMetaAttribute> readAllUnique(Session session) throws DAOException {
    List<ProjectMetaAttribute> attributeList = new ArrayList<ProjectMetaAttribute>();
    try {
        String sql = " select PMA.* from project_meta_attribute PMA, "
                + " (select projma_id, max(projma_create_date) "
                + "     from project_meta_attribute group by projma_lkuvlu_attribute_id) PMAU "
                + " where PMA.projma_id = PMAU.projma_id ";
        SQLQuery query = session.createSQLQuery(sql);
        query.addEntity("PMA", ProjectMetaAttribute.class);

        attributeList = query.list();
    } catch (Exception ex) {
        throw new DAOException(ex);
    }

    return attributeList;
}

From source file:org.jcvi.ometa.hibernate.dao.SampleDAO.java

License:Open Source License

public List<Sample> getAllSamples(Long flexId, String type, String sSearch, String sortCol, String sortDir,
        Session session) throws DAOException {
    List<Sample> sampleList = new ArrayList<Sample>();
    try {/*from  w  w w .j a  va2 s .c o  m*/
        List results = null;

        String sql = " select S1.*, S2.sample_name parent, CONCAT(A.actor_last_name,',',A.actor_first_name) user "
                + " from sample S1 " + " left join sample S2 on S1.sample_sample_parent_id=S2.sample_id "
                + " left join actor A on S1.sample_created_by=A.actor_id where ";

        if ("sample".equals(type))
            sql += "S1.sample_id=";
        else
            sql += "S1.sample_projet_id=";
        sql += flexId;

        if (sSearch != null && !sSearch.isEmpty()) {
            sSearch = "%" + sSearch + "%";
            sql += " and (LOWER(S1.sample_name) like '" + sSearch + "' "
                    + " or (S1.sample_id in (select SA.sampla_sample_id from sample_attribute SA, lookup_value LV "
                    + "   where LOWER(SA.sampla_attribute_str) like '" + sSearch
                    + "' or (SA.sampla_lkuvlu_attribute_id=LV.lkuvlu_id and LOWER(LV.lkuvlu_name) like '"
                    + sSearch + "')) " + " or LOWER(S2.sample_name) like '" + sSearch
                    + "' or ((LOWER(A.actor_first_name) like '" + sSearch
                    + "' or LOWER(A.actor_last_name) like '" + sSearch + "'))))";
        }

        if (sortCol != null && !sortCol.isEmpty() && sortDir != null && !sortDir.isEmpty()) {
            sql += " order by";
            if (sortCol.equals("sample"))
                sql += " sample_name ";
            else if (sortCol.equals("parent"))
                sql += " parent ";
            else if (sortCol.equals("user"))
                sql += " user ";
            else if (sortCol.equals("date"))
                sql += " sample_create_date ";
            sql += sortDir;
        }

        SQLQuery query = session.createSQLQuery(sql);
        query.addEntity("S", Sample.class);
        results = query.list();

        if (results != null) {
            for (Object result : results) {
                sampleList.add((Sample) result);
            }
        }
    } catch (Exception ex) {
        throw new DAOException(ex);
    }

    return sampleList;
}

From source file:org.jcvi.ometa.hibernate.dao.SampleMetaAttributeDAO.java

License:Open Source License

/**
 * get all unique meta-attributes//  w  w w .  j av a 2 s.c o m
 */
public List<SampleMetaAttribute> readAllUnique(Session session) throws DAOException {
    List<SampleMetaAttribute> attributeList = new ArrayList<SampleMetaAttribute>();
    try {
        String sql = " select SMA.* from sample_meta_attribute SMA, "
                + "  (select sampma_id, max(sampma_create_date) "
                + "  from sample_meta_attribute group by sampma_lkuvlu_attribute_id) SMAU "
                + "  where SMA.sampma_id = SMAU.sampma_id ";
        SQLQuery query = session.createSQLQuery(sql);
        query.addEntity("SMA", SampleMetaAttribute.class);

        attributeList = query.list();
    } catch (Exception ex) {
        throw new DAOException(ex);
    }

    return attributeList;
}

From source file:org.jcvi.ometa.hibernate.dao.SecurityDAO.java

License:Open Source License

/**
 * Helper to enforce authorization by name, to users.
 *//*  ww  w.j av  a 2 s .  c  o m*/
public List<Project> getListOfAuthorizedProjects(String username, AccessLevel accessLevel, Session session)
        throws Exception {

    String queryStr = AUTHORIZED_FOR_USER_SQL_QUERY;

    if (accessLevel == AccessLevel.View) {
        queryStr = queryStr.replace(PROJ_GRP_SUBST_STR, VIEW_PROJECT_GROUP_FIELD);
    } else {
        queryStr = queryStr.replace(PROJ_GRP_SUBST_STR, EDIT_PROJECT_GROUP_FIELD);
    }

    SQLQuery query = session.createSQLQuery(queryStr);
    String queryUsername = username == null ? UNLOGGED_IN_USER : username;
    query.setParameter(USERNAME_PARAM, queryUsername);
    query.addEntity("P", Project.class);
    List<Project> rtnVal = query.list();
    return rtnVal;

}

From source file:org.squale.jraf.provider.persistence.hibernate.AbstractDAOImpl.java

License:Open Source License

/**
 * cration d'une query  partir d'une requte sql, reprise d'une fonctionnalit d'Hibernate pour viter d'avoir 
 * importer les packages Hibernate dans les DAO de l'application cf API de la classe org.hibernate.Session
 * /*from   w ww. j  a  v  a2 s . co m*/
 * @param session
 * @param sql
 * @param returnAliases
 * @param returnClasses
 * @return
 */
public SQLQuery createSQLQuery(ISession session, String sql, String[] returnAliases, Class[] returnClasses) {
    SessionImpl sessionImpl = (SessionImpl) session;
    SQLQuery query = sessionImpl.getSession().createSQLQuery(sql);
    for (int i = 0; i < returnAliases.length; i++) {
        query.addEntity(returnAliases[i], returnClasses[i]);
    }
    return query;
}

From source file:org.telscenter.sail.webapp.dao.workgroup.impl.HibernateWISEWorkgroupDao.java

License:LGPL

/**
 * @see net.sf.sail.webapp.dao.workgroup.WorkgroupDao#getListByOfferingAndUser(net.sf.sail.webapp.domain.Offering,
 *      net.sf.sail.webapp.domain.User)//ww w .java2  s. c o m
 */
@SuppressWarnings("unchecked")
public List<WISEWorkgroup> getListByOfferingAndUser(Offering offering, User user) {
    Session session = this.getSession();
    SQLQuery sqlQuery = session.createSQLQuery("SELECT w.*, g.*, ww.* FROM workgroups as w, groups as g, "
            + "groups_related_to_users as g_r_u, wiseworkgroups as ww " + "WHERE w.group_fk = g.id "
            + "AND g_r_u.group_fk = w.group_fk " + "AND g_r_u.user_fk = :user_param "
            + "AND w.offering_fk = :offering_param " + "AND w.id = ww.id");

    sqlQuery.addEntity("wiseworkgroup", WISEWorkgroupImpl.class);
    sqlQuery.setParameter("offering_param", offering.getId(), Hibernate.LONG);
    sqlQuery.setParameter("user_param", user.getId(), Hibernate.LONG);
    return sqlQuery.list();
}