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:apm.common.core.DaoImpl.java

License:Open Source License

/**
 * /*from  w  w w .j  a  va2  s.  c  o m*/
 * @param query
 * @param resultClass
 */
private void setResultTransformer(SQLQuery query, Class<?> resultClass) {
    if (resultClass != null) {
        if (resultClass == Map.class) {
            query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        } else if (resultClass == List.class) {
            query.setResultTransformer(Transformers.TO_LIST);
        } else {
            query.addEntity(resultClass);
        }
    }
}

From source file:at.ac.tuwien.ifs.tita.dao.issuetracker.IssueTrackerProjectDao.java

License:Apache License

/** {@inheritDoc} */
@Override/* www . j  a  va 2  s  . com*/
public IssueTrackerProject findIssueTrackerProjectForTiTAProject(Long tp, Long issueTrackerId, Long itp) {
    String queryString = "select * from issue_tracker_project itp "
            + "join tita_project tp on itp.tita_project_id = tp.id "
            + "where tp.id = ? and itp.isst_id = ? and itp.isst_project_id = ? ";

    org.hibernate.SQLQuery q = getSession().createSQLQuery(queryString.toUpperCase());
    q.addEntity(IssueTrackerProject.class);
    // CHECKSTYLE:OFF
    q.setParameter(0, tp);
    q.setParameter(1, issueTrackerId);
    q.setParameter(2, itp);
    // CHECKSTYLE:ON

    return (IssueTrackerProject) q.list().get(0);
}

From source file:at.ac.tuwien.ifs.tita.dao.issuetracker.task.IssueTrackerTaskDao.java

License:Apache License

/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override/*  w w  w  .j a va2  s . co m*/
public IssueTrackerTask findIssueTrackerTask(Long tp, Long it, Long itp, Long itt) {
    String queryString = "select * from issue_tracker_task itt "
            + "join issue_tracker_project itp on itt.issue_tracker_project_id = itp.id "
            + "join tita_project tp on itp.tita_project_id = tp.id "
            + "where tp.id = ? and itp.isst_id = ? and itp.isst_project_id = ? " + "and itt.isst_task_id = ? ";

    org.hibernate.SQLQuery q = getSession().createSQLQuery(queryString.toUpperCase());
    q.addEntity(IssueTrackerTask.class);
    // CHECKSTYLE:OFF
    q.setParameter(0, tp);
    q.setParameter(1, it);
    q.setParameter(2, itp);
    q.setParameter(3, itt);
    // CHECKSTYLE:ON

    List<IssueTrackerTask> task = q.list();

    if (task.size() != 0) {
        return task.get(0);
    } else {
        return null;
    }
}

From source file:at.ac.tuwien.ifs.tita.dao.project.TiTAProjectDao.java

License:Apache License

/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override//from www  .  j a v a 2  s. c  o m
public List<TiTAProject> findTiTAProjectsForUsername(String username) {
    String queryString = "select * from TITA_PROJECT tp, USER_PROJECT up, TITA_USER tu "
            + " where tp.id = up.tita_project_id and up.user_id = tu.id and tu.username = ? ";

    org.hibernate.SQLQuery query = getSession().createSQLQuery(queryString.toUpperCase());
    query.setParameter(0, username);
    query.addEntity(TiTAProject.class);

    return query.list();
}

From source file:at.ac.tuwien.ifs.tita.dao.time.EffortDao.java

License:Apache License

/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override/*from  ww w  . j  av a 2 s.  c o m*/
public List<UserProjectEffort> findEffortsForTiTAProjectAndTimeConsumerId(List<String> projectIds,
        List<String> tIds, String grouping) {
    String pIds = StringUtil.generateIdStringFromStringList(projectIds);
    String tcIds = StringUtil.generateIdStringFromStringList(tIds);

    String queryString = "select UUID() as ID, "
            + " sum(duration) as DURATION, username as USERNAME, project as PROJECT";

    if (grouping.equals("month")) {
        queryString += ", year as YEAR, month as MONTH, null as DAY";
    } else if (grouping.equals("day")) {
        queryString += ", year as YEAR, month as MONTH, day as DAY";
    } else if (grouping.equals("overall")) {
        queryString += ", null as YEAR, null as MONTH, null as DAY";
    }

    queryString += " from (select sum(e1.duration) as duration, tu.username as username,"
            + " tp.name as project ";

    if (grouping.equals("month")) {
        queryString += ", year(e1.date) as YEAR, " + " month(e1.date) as MONTH, null as DAY";
    } else if (grouping.equals("day")) {
        queryString += ", year(e1.date) as YEAR, " + " month(e1.date) as MONTH, " + " day(e1.date) as DAY";
    } else if (grouping.equals("overall")) {
        queryString += ", null as YEAR, null as MONTH, null as DAY";
    }

    queryString += " from effort e1 " + "join tita_task tt on e1.tita_task_id = tt.id "
            + "join tita_project tp on tt.tita_project_id = tp.id " + "join tita_user tu on tu.id = e1.user_id "
            + "where tp.name in (" + pIds + ") and tu.username in (" + tcIds + ") ";

    if (grouping.equals("month")) {
        queryString += " group by tp.name, tu.username, year(e1.date), " + " month(e1.date) ";
    } else if (grouping.equals("day")) {
        queryString += " group by tp.name, tu.username, year(e1.date), " + " month(e1.date), day(e1.date) ";
    } else if (grouping.equals("overall")) {
        queryString += " group by tp.name, tu.username ";
    }

    queryString += " union all" + " select sum(e2.duration) as duration, tu1.username as username, "
            + " tp2.name as project ";

    if (grouping.equals("month")) {
        queryString += ", year(e2.date) as YEAR, " + " month(e2.date) as MONTH, null as DAY";
    } else if (grouping.equals("day")) {
        queryString += ", year(e2.date) as YEAR, " + " month(e2.date) as MONTH, day(e2.date) as DAY";
    } else if (grouping.equals("overall")) {
        queryString += ", null as YEAR, null as MONTH, null as DAY";
    }

    queryString += " from effort e2 " + "join issue_tracker_task it on e2.issuet_task_id = it.id "
            + "join issue_tracker_project itp on it.issue_tracker_project_id ="
            + "itp.id join tita_project tp2 on tp2.id = itp.tita_project_id "
            + "join tita_user tu1 on tu1.id = e2.user_id " + "where tp2.name in (" + pIds
            + ") and tu1.username in (" + tcIds + ")";

    if (grouping.equals("month")) {
        queryString += " group by tp2.name, tu1.username, year(e2.date), " + " month(e2.date)";
    } else if (grouping.equals("day")) {
        queryString += " group by tp2.name, tu1.username, year(e2.date), " + " month(e2.date), day(e2.date)";
    } else if (grouping.equals("overall")) {
        queryString += " group by tp2.name, tu1.username";
    }

    queryString += ") as U group by project, username, year, month, day "
            + " order by project, year, month, day, duration, username";

    org.hibernate.SQLQuery q = getSession().createSQLQuery(queryString.toUpperCase());
    q.addEntity(UserProjectEffort.class);
    q.setFetchSize(C_FETCHSIZE);

    List<UserProjectEffort> efforts = null;

    try {
        efforts = q.list();
    } catch (NoResultException e) {
        // nothing to do
    }
    return efforts;
}

From source file:at.ac.tuwien.ifs.tita.dao.time.EffortDao.java

License:Apache License

/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override//from w  ww .  ja  v  a 2s .  c  om
public List<UserProjectEffort> findEffortsForTiTAProjectId(List<String> projectIds, String grouping) {
    String pIds = StringUtil.generateIdStringFromStringList(projectIds);

    String queryString = "select UUID() as ID,"
            + " sum(duration) as DURATION, project as PROJECT, null as USERNAME";

    if (grouping.equals("month")) {
        queryString += ", year as YEAR, month as MONTH, null as DAY";
    } else if (grouping.equals("day")) {
        queryString += ", year as YEAR, month as MONTH, day as DAY";
    } else if (grouping.equals("overall")) {
        queryString += ", null as YEAR, null as MONTH, null as DAY";
    }

    queryString += " from (select sum(e1.duration) as duration, tp.name as project ";

    if (grouping.equals("month")) {
        queryString += ", year(e1.date) as YEAR, " + " month(e1.date) as MONTH, null as DAY ";
    } else if (grouping.equals("day")) {
        queryString += ", year(e1.date) as YEAR, " + " month(e1.date) as MONTH, day(e1.date) as DAY";
    } else if (grouping.equals("overall")) {
        queryString += ", null as YEAR, null as MONTH, null as DAY";
    }

    queryString += " from effort e1 join tita_task tt on e1.tita_task_id = tt.id "
            + "join tita_project tp on tt.tita_project_id = tp.id " + "where tp.name in (" + pIds + ") ";

    if (grouping.equals("month")) {
        queryString += " group by tp.name, year(e1.date), " + " month(e1.date) ";
    } else if (grouping.equals("day")) {
        queryString += " group by tp.name, year(e1.date), " + " month(e1.date), day(e1.date) ";
    } else if (grouping.equals("overall")) {
        queryString += " group by tp.name ";
    }

    queryString += " union all" + " select sum(e2.duration) as duration, tp2.name as project ";

    if (grouping.equals("month")) {
        queryString += ", year(e2.date) as year, " + " month(e2.date) as month, null as day ";
    } else if (grouping.equals("day")) {
        queryString += ", year(e2.date) as year, " + "month(e2.date) as month, day(e2.date) as day";
    } else if (grouping.equals("overall")) {
        queryString += ", null as year, null as month, null as day";
    }

    queryString += " from effort e2 " + "join issue_tracker_task it on e2.issuet_task_id = it.id "
            + "join issue_tracker_project itp on it.issue_tracker_project_id ="
            + "itp.id join tita_project tp2 on tp2.id = itp.tita_project_id " + "where tp2.name in (" + pIds
            + ") ";

    if (grouping.equals("month")) {
        queryString += " group by tp2.name, year(e2.date), " + " month(e2.date) ";
    } else if (grouping.equals("day")) {
        queryString += " group by tp2.name, year(e2.date), " + " month(e2.date), day(e2.date) ";
    } else if (grouping.equals("overall")) {
        queryString += " group by tp2.name ";
    }

    queryString += ") as U group by project, year, month, day, username "
            + " order by project, year, month, day, duration";

    org.hibernate.SQLQuery q = getSession().createSQLQuery(queryString.toUpperCase());
    q.addEntity(UserProjectEffort.class);
    q.setFetchSize(C_FETCHSIZE);
    List<UserProjectEffort> efforts = new ArrayList<UserProjectEffort>();

    try {
        efforts = q.list();
    } catch (NoResultException e) {
        // nothing to do
    }
    return efforts;
}

From source file:at.ac.tuwien.ifs.tita.dao.time.EffortDao.java

License:Apache License

/** {@inheritDoc} */
@Override/*  w  ww  .  j av a  2s  . c  om*/
public List<Effort> findEffortsForTiTAProjectAndTiTAUserOrdered(Long projectId, Long userId) {
    List<Effort> returnValue = new ArrayList<Effort>();

    String queryString = "select e1.ID, e1.DESCRIPTION, e1.TITA_TASK_ID, e1.ISSUET_TASK_ID, "
            + "e1.DATE, e1.START_TIME, e1.END_TIME, e1.DURATION, e1.DELETED, "
            + "e1.USER_ID, e1.COST_CENTER from Effort e1 " + "join tita_task tt on e1.tita_task_id = tt.id "
            + "join tita_project tp on tt.tita_project_id = tp.id " + "join tita_user tu on tu.id = e1.user_id "
            + "where tu.id = " + userId + " and tp.id = " + projectId + " and e1.deleted != true " + "UNION "
            + "select e2.ID, e2.DESCRIPTION, e2.TITA_TASK_ID, e2.ISSUET_TASK_ID, e2.DATE, "
            + "e2.START_TIME, e2.END_TIME, e2.DURATION, e2.DELETED, "
            + "e2.USER_ID, e2.COST_CENTER from Effort e2 "
            + "join issue_tracker_task itt on e2.issuet_task_id = itt.id "
            + "join issue_tracker_project itp on itt.issue_tracker_project_id = itp.id "
            + "join tita_project tp2 on tp2.id = itp.tita_project_id "
            + "join tita_user tu2 on tu2.id = e2.user_id " + "where tu2.id = " + userId + " and tp2.id = "
            + projectId + " and e2.deleted != true " + "order by date desc, end_time desc";

    org.hibernate.SQLQuery q = getSession().createSQLQuery(queryString.toUpperCase());
    q.addEntity(Effort.class);
    q.setFetchSize(C_FETCHSIZE);

    try {
        returnValue = q.list();
    } catch (NoResultException e) {
        // no results
    }
    return returnValue;
}

From source file:at.ac.tuwien.ifs.tita.dao.user.UserDAO.java

License:Apache License

/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override/*from w  w  w. j a v  a  2s.  c  o m*/
public List<TiTAUser> findUsersForProjectNames(List<String> projects) {
    String names = StringUtil.generateIdStringFromStringList(projects);

    String queryString = "select u.* from tita_user u join user_project up on "
            + "u.id = up.user_id join tita_project tp on up.tita_project_id = tp.id " + " where tp.name in ("
            + names + ")";

    org.hibernate.SQLQuery query = getSession().createSQLQuery(queryString.toUpperCase());

    List<TiTAUser> users = new ArrayList<TiTAUser>();

    query.addEntity(TiTAUser.class);

    try {
        users = query.list();
    } catch (NoResultException e) {
        // nothing to do
    }
    return users;
}

From source file:br.com.jn.dao.CandidatoDAO.java

public List<Candidato> listar() {
    Authentication a = SecurityContextHolder.getContext().getAuthentication();
    //Authentication authentication = context.getAuthentication();
    Object principal = a.getPrincipal();
    String username = "";
    username = (((User) a.getPrincipal()).getUsername());
    //username = ((UserDetails) principal).getUsername();
    System.out.println("login selecionado:" + username);

    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();// w  w  w  . java  2  s.  com

    String sql = "select * from users where username = :username";
    SQLQuery query = session.createSQLQuery(sql);
    query.addEntity(Users.class);
    query.setParameter("username", username);
    //Users usuario = (Users) query.uniqueResult();
    List logado = query.list();
    //List<Users> results = query.getResultList();

    String UserId = logado.get(0).toString();
    String usuario = "4";
    //System.out.println("Id do usuario selecionado:" + usuario.getUserId());
    //System.out.println("Id do usuario selecionado:" + usuario.getNome());

    //Integer UserId = usuario.getUserId();
    System.out.println("Id do usuario selecionado:" + UserId);

    Criteria crit = session.createCriteria(Candidato.class);
    crit.add(Restrictions.ge("notaEliminatoria", (5.0)));
    crit.add(Restrictions.eq("orientador1", username));
    crit.addOrder(Order.asc("nome"));

    List<Candidato> lista = crit.list();
    session.close();
    return lista;
}

From source file:ch.algotrader.dao.AbstractDaoTest.java

License:Open Source License

@Test
public void testFindBySQLQueryAsEntity() throws Exception {

    GenericItem stuff1 = new GenericItem("this");
    stuff1.setActive(true);//from ww w  .  j  a  v  a  2 s  .  co m
    stuff1.setBroker(Broker.DC.name());

    this.dao.save(stuff1);

    long id1 = stuff1.getId();

    Assert.assertNotEquals(0, id1);

    this.dao.flush();

    SQLQuery query = this.dao.prepareSQLQuery(null, "select s.* from GenericItem s where s.active = ?",
            Boolean.TRUE);
    query.addEntity(GenericItem.class);

    final List<?> list1 = query.list();

    Assert.assertNotNull(list1);

    Assert.assertEquals(1, list1.size());

    GenericItem stuff = (GenericItem) list1.get(0);

    Assert.assertSame(stuff, stuff1);
}