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:com.connexience.server.model.metadata.MetadataQueryBuilder.java

License:Open Source License

/** Create the Hibernate Query */
public SQLQuery createSQLQuery(Session session) throws Exception {
    // Metadata query
    String queryText = buildMetaDataSearchQuery();

    // Full query
    String fullQuery = "select * from objectsflat where id in (" + queryText + ")";
    SQLQuery q = session.createSQLQuery(fullQuery);

    Iterator<MetadataQueryItem> items = metadataQuery.items();
    MetadataQueryItem i;/*from   w  w  w  . j  a v a 2 s  .  c  om*/

    // Set category if needed 
    Object[][] params;
    int positionCounter = 0;
    while (items.hasNext()) {
        i = items.next();
        i.setPositionCounter(positionCounter);
        params = i.getParameters();
        for (int j = 0; j < params.length; j++) {
            q.setParameter((String) params[j][0], params[j][1]);
        }
        positionCounter = i.getPositionCounter();
    }
    q.addEntity(ServerObject.class);
    return q;
}

From source file:com.daro.persistence.generic.dao.GenericDaoImpl.java

License:GNU General Public License

/**
 * Get Entity T using a SQL query string and SqlQuery object.
 * //  w  w w .  j  av  a 2  s.  co m
 * @param sql
 * @return
 * @throws PersistenceException
 */
@SuppressWarnings("unchecked")
public T executeSqlQuery(String sql) throws PersistenceException {
    T t = null;

    SQLQuery query = createSqlQuery(sql);

    try {
        query.addEntity(clazz);
        t = (T) query.uniqueResult();
    } catch (Exception e) {
        throw new PersistenceException(e);
    }

    return t;
}

From source file:com.daro.persistence.generic.dao.GenericDaoImpl.java

License:GNU General Public License

/**
 * Get Entity T using a SQL query string and SqlQuery object with parameters list.
 * //from w  w  w .  j a v  a 2 s .  c  o m
 * @param sql For example "SELECT * FROM table WHERE id = ?"
 * @param params Parameters list to match with ? in SQL query
 * @return Entity T type
 * @throws PersistenceException
 */
@SuppressWarnings("unchecked")
public T executeSqlQuery(String sql, List<String> params) throws PersistenceException {
    T t = null;

    SQLQuery query = createSqlQuery(sql);

    try {
        query.addEntity(clazz);

        //inserts parameters into sql string replacing ? symbols by order
        for (int i = 0; i < params.size(); i++) {
            query.setParameter(i, params.get(i));
        }

        t = (T) query.uniqueResult();
    } catch (Exception e) {
        throw new PersistenceException(e);
    }

    return t;
}

From source file:com.eryansky.common.orm.core.hibernate.support.BasicHibernateDao.java

License:Apache License

/**
 * ?HQL?Query//from w w  w .ja  v a2s.com
 *
 * @param queryOrSqlQuery query  NamedSQLQuery
 * @param values
 *            ???,??.
 *
 * @return {@link org.hibernate.Query}
 *
 */
protected SQLQuery createSQLQuery(String queryOrSqlQuery, Map<String, ?> values) {
    SQLQuery query = createSQLQuery(queryOrSqlQuery);
    if (values != null) {
        query.setProperties(values);
    }
    return query.addEntity(entityClass);
}

From source file:com.eryansky.common.orm.core.hibernate.support.BasicHibernateDao.java

License:Apache License

/**
 * ?SQL?SQLQuery/*from  ww  w . j  a v a2 s  . c om*/
 *
 * @param queryOrNamedSQLQuery query  NamedSQLQuery
 * @param values ????,?.
 *
 * @return {@link org.hibernate.SQLQuery}
 */
protected SQLQuery createSQLQuery(String queryOrNamedSQLQuery, Object... values) {
    Assert.hasText(queryOrNamedSQLQuery, "queryOrNamedSQLQuery?");
    SessionFactoryImpl factory = (SessionFactoryImpl) sessionFactory;
    NamedSQLQueryDefinition nsqlqd = factory.getNamedSQLQuery(queryOrNamedSQLQuery);
    Query query = null;

    if (nsqlqd != null) {
        query = getSession().getNamedQuery(queryOrNamedSQLQuery);
    } else {
        query = getSession().createSQLQuery(queryOrNamedSQLQuery);
    }

    setQueryValues(query, values);
    SQLQuery sqlQuery = (SQLQuery) query;

    return sqlQuery.addEntity(entityClass);
}

From source file:com.globalsight.everest.permission.PermissionManagerLocal.java

License:Apache License

/**
 * Queries all permissiongroups for this user.
 * //from   ww w .java2s  .co m
 * @param p_userId
 *            user
 * @return Collection of PermissionGroups
 */
@SuppressWarnings("unchecked")
public Collection<PermissionGroup> getAllPermissionGroupsForUser(String p_userId)
        throws PermissionException, RemoteException {
    String sql = "select p.* from permissiongroup p, "
            + " permissiongroup_user pu where p.id = pu.permissiongroup_id "
            + " and pu.user_id = :USER_ID_ARG ";

    Session session = HibernateUtil.getSession();
    SQLQuery query = session.createSQLQuery(sql);
    query.addEntity(PermissionGroupImpl.class);
    query.setString("USER_ID_ARG", p_userId);

    try {
        return query.list();
    } catch (Exception e) {
        throw new PermissionException(e);
    }
}

From source file:com.globalsight.everest.taskmanager.TaskManagerLocal.java

License:Apache License

/**
 * @see TaskManager.removeUserAsTaskAcceptor(String)
 *//*from   w w w.j ava  2 s .co  m*/
public void removeUserAsTaskAcceptor(String p_userId) throws RemoteException, TaskException {
    // first reassign the user
    try {
        ServerProxy.getWorkflowServer().reassignUserActivitiesToPm(p_userId);
    } catch (Exception e) {
        CATEGORY.error(
                "Failed to reassign the user activities of user " + p_userId + " to the Project Manager.", e);
        return;
    }

    // now reset the acceptor, accepted_date, and task state
    // for all accepted task by this user
    Session session = HibernateUtil.getSession();
    Transaction tx = session.beginTransaction();
    try {
        SQLQuery query = session.createSQLQuery(TaskDescriptorModifier.ACCEPTED_TASK_BY_USER_ID_SQL);
        query.addEntity(TaskImpl.class);
        query.setParameter(TaskDescriptorModifier.TASK_USER_ID_ARG, p_userId);
        Collection col = query.list();

        if (col == null) {
            return;
        }

        Object[] tasks = col.toArray();

        for (int i = 0, size = tasks.length; i < size; i++) {
            Task task = (Task) tasks[i];
            task.setAcceptor(null);
            task.setAcceptedDate(null);
            task.setState(Task.STATE_ACTIVE);
            session.update(task);
        }
        tx.commit();
    } catch (Exception e) {
        tx.rollback();
        CATEGORY.error("Failed to reset task acceptor and state for the removed user " + p_userId + ".", e);
        return;
    }
}

From source file:com.globalsight.everest.workflow.WorkflowJbpmPersistenceHandler.java

License:Apache License

/**
  * Gets the task instances in all status.
  * /* ww w . ja va 2  s .  c  om*/
  * @param p_userId
  *            the user id.
  * @param p_session
  *            the hibernate session.
  * @param p_pm
  *            is pm or not (true:PM|false:user).
  * 
  * @return the task instances in all status.
  */
@SuppressWarnings("unchecked")
private static List<TaskInstance> allTaskInstances(String p_userId, Session p_session, boolean p_pm) {
    SQLQuery query = null;
    if (p_pm) {
        query = p_session.createSQLQuery(ALL_TASK_INSTANCES_PM);
        query.setString(PM, p_userId);
    } else {
        query = p_session.createSQLQuery(SqlHolder.allTask);
        query.setString(ACTOR_ID, p_userId);
        query.setString(VARIABLE_NAME, WorkflowConstants.VARIABLE_IS_REJECTED);
        query.setString(CATEGORY, VARIABLE_CATEGORY_REJECT);

    }
    query.addEntity(TaskInstance.class);

    return query.list();
}

From source file:com.globalsight.everest.workflow.WorkflowJbpmPersistenceHandler.java

License:Apache License

/**
  * Gets the completed task instances.//from  w w w. ja v  a 2 s  .  c  o m
  * 
  * @param p_userId
  *            the user id.
  * @param p_session
  *            the hibernate session.
  * @param p_pm
  *            is pm or not (true:PM|false:user).
  * 
  * @return the completed task instances.
  */
@SuppressWarnings("unchecked")
private static List<TaskInstance> completedTaskInstances(String p_userId, Session p_session, boolean p_pm) {
    SQLQuery query = null;
    if (p_pm) {
        query = p_session.createSQLQuery(COMPLETED_TASK_INSTANCES_PM);
        query.setString(PM, p_userId);
    } else {
        query = p_session.createSQLQuery(COMPLETED_TASK_INSTANCES);
        query.setString(ACTOR_ID, p_userId);
    }
    query.addEntity(TaskInstance.class);

    return query.list();
}

From source file:com.globalsight.everest.workflow.WorkflowJbpmPersistenceHandler.java

License:Apache License

/**
  * Gets the accepted task instances./* w  w w .java 2  s  .  c  o  m*/
  * 
  * @param p_userId
  *            the user id.
  * @param p_session
  *            the hibernate session.
  * @param p_pm
  *            is pm or not (true:PM|false:user).
  * 
  * @return the accepted task instances.
  */
@SuppressWarnings("unchecked")
private static List<TaskInstance> acceptedTaskInstances(String p_userId, Session p_session, boolean p_pm) {
    SQLQuery query = null;
    if (p_pm) {
        query = p_session.createSQLQuery(ACCEPTED_TASK_INSTANCES_PM);
        query.setString(PM, p_userId);
    } else {
        query = p_session.createSQLQuery(ACCEPTED_TASK_INSTANCES);
        query.setString(ACTOR_ID, p_userId);

    }
    query.addEntity(TaskInstance.class);

    return query.list();
}