Example usage for org.hibernate Query setEntity

List of usage examples for org.hibernate Query setEntity

Introduction

In this page you can find the example usage for org.hibernate Query setEntity.

Prototype

@Deprecated
@SuppressWarnings("unchecked")
Query<R> setEntity(String name, Object val);

Source Link

Document

Bind an instance of a mapped persistent class to a named query parameter.

Usage

From source file:com.bloatit.data.DaoTeam.java

License:Open Source License

public long getRecentHistoryCount(final int numberOfDays) {
    final Query size = SessionManager.getNamedQuery("team.getRecentHistory.size");
    size.setEntity("team", this);
    size.setDate("date", DateUtils.nowMinusSomeDays(numberOfDays));
    return (Long) size.uniqueResult();
}

From source file:com.bloatit.data.DaoTeamMembership.java

License:Open Source License

/**
 * Get a TeamMembership line using its composite key. (HQL request)
 *//*w w  w.j  a  v  a  2s.  c o  m*/
protected static DaoTeamMembership get(final DaoTeam team, final DaoMember member) {
    final Session session = SessionManager.getSessionFactory().getCurrentSession();
    final Query q = session.getNamedQuery("teamMembership.byTeamMember");
    q.setEntity("bloatitTeam", team);
    q.setEntity("member", member);
    return (DaoTeamMembership) q.uniqueResult();
}

From source file:com.bluexml.side.Framework.alfresco.jbpm.CustomJBPMEngine.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<WorkflowTimer> getTimers(final String workflowId) {
    try {/*w  ww  .  j av  a 2 s  .  c  o  m*/
        return (List<WorkflowTimer>) jbpmTemplate.execute(new JbpmCallback() {
            public List<WorkflowTimer> doInJbpm(JbpmContext context) {
                // retrieve process
                GraphSession graphSession = context.getGraphSession();
                ProcessInstance process = getProcessInstance(graphSession, workflowId);

                // retrieve timers for process
                Session session = context.getSession();
                Query query = session.createQuery(PROCESS_TIMERS_QUERY);
                query.setEntity("process", process);
                List<Timer> timers = query.list();

                // convert timers to appropriate service response format 
                List<WorkflowTimer> workflowTimers = new ArrayList<WorkflowTimer>(timers.size());
                for (Timer timer : timers) {
                    WorkflowTimer workflowTimer = createWorkflowTimer(timer);
                    workflowTimers.add(workflowTimer);
                }
                return workflowTimers;
            }
        });
    } catch (JbpmException e) {
        String msg = messageService.getMessage(ERR_GET_TIMERS, workflowId);
        throw new WorkflowException(msg, e);
    }
}

From source file:com.cloud.bridge.util.QueryHelper.java

License:Open Source License

public static void bindParameters(Query query, Object[] params) {
    int pos = 0;/*from  ww  w .  jav a 2  s  .  c  o  m*/
    if (params != null && params.length > 0) {
        for (Object param : params) {
            if (param instanceof Byte)
                query.setByte(pos++, ((Byte) param).byteValue());
            else if (param instanceof Short)
                query.setShort(pos++, ((Short) param).shortValue());
            else if (param instanceof Integer)
                query.setInteger(pos++, ((Integer) param).intValue());
            else if (param instanceof Long)
                query.setLong(pos++, ((Long) param).longValue());
            else if (param instanceof Float)
                query.setFloat(pos++, ((Float) param).floatValue());
            else if (param instanceof Double)
                query.setDouble(pos++, ((Double) param).doubleValue());
            else if (param instanceof Boolean)
                query.setBoolean(pos++, ((Boolean) param).booleanValue());
            else if (param instanceof Character)
                query.setCharacter(pos++, ((Character) param).charValue());
            else if (param instanceof Date)
                query.setDate(pos++, (Date) param);
            else if (param instanceof Calendar)
                query.setCalendar(pos++, (Calendar) param);
            else if (param instanceof CalendarDateParam)
                query.setCalendarDate(pos++, ((CalendarDateParam) param).dateValue());
            else if (param instanceof TimestampParam)
                query.setTimestamp(pos++, ((TimestampParam) param).timestampValue());
            else if (param instanceof TimeParam)
                query.setTime(pos++, ((TimeParam) param).timeValue());
            else if (param instanceof String)
                query.setString(pos++, (String) param);
            else if (param instanceof TextParam)
                query.setText(pos++, ((TextParam) param).textValue());
            else if (param instanceof byte[])
                query.setBinary(pos++, (byte[]) param);
            else if (param instanceof BigDecimal)
                query.setBigDecimal(pos++, (BigDecimal) param);
            else if (param instanceof BigInteger)
                query.setBigInteger(pos++, (BigInteger) param);
            else if (param instanceof Locale)
                query.setLocale(pos++, (Locale) param);
            else if (param instanceof EntityParam)
                query.setEntity(pos++, ((EntityParam) param).entityValue());
            else if (param instanceof Serializable)
                query.setSerializable(pos++, (Serializable) param);
            else
                query.setEntity(pos++, param);
        }
    }
}

From source file:com.dell.asm.asmcore.asmmanager.db.DeviceInventoryComplianceDAO.java

License:Open Source License

public DeviceInventoryComplianceEntity findDeviceInventoryCompliance(String deviceInventoryId,
        String firmwareRepositoryId) {

    if (StringUtils.isBlank(deviceInventoryId) || StringUtils.isBlank(firmwareRepositoryId)) {
        return null;
    }/*from   www  .j a  va  2s. c  o  m*/
    Session session = null;
    Transaction tx = null;

    try {
        session = dao.getNewSession();
        tx = session.beginTransaction();
        final DeviceInventoryEntity deviceInventory = (DeviceInventoryEntity) session
                .get(DeviceInventoryEntity.class, deviceInventoryId);
        if (deviceInventory == null) {
            return null;
        }
        final FirmwareRepositoryEntity firmwareRepository = (FirmwareRepositoryEntity) session
                .get(FirmwareRepositoryEntity.class, firmwareRepositoryId);
        if (firmwareRepository == null) {
            return null;
        }

        final String hql = "from DeviceInventoryComplianceEntity dic"
                + " where dic.deviceInventoryComplianceId.deviceInventory = :deviceInventory"
                + " and dic.deviceInventoryComplianceId.firmwareRepository = :firmwareRepository";
        final Query query = session.createQuery(hql);
        query.setEntity("deviceInventory", deviceInventory);
        query.setEntity("firmwareRepository", firmwareRepository);
        return (DeviceInventoryComplianceEntity) query.uniqueResult();

    } catch (Exception e) {
        logger.warn("Caught exception during get: " + e);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction during findDeviceInventoryCompliance: " + ex);
        }
        throw new AsmManagerInternalErrorException("findDeviceInventoryCompliance",
                "DeviceInventoryComplianceDAO", e);
    } finally {
        dao.cleanupSession(session, "findDeviceInventoryCompliance");
    }
}

From source file:com.dell.asm.asmcore.asmmanager.db.DeviceInventoryComplianceDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<DeviceInventoryComplianceEntity> findByDeviceInventory(final String deviceInventoryId) {
    if (StringUtils.isBlank(deviceInventoryId)) {
        return Collections.emptyList();
    }/*from  www  .  ja  v a2 s. c  o m*/
    Session session = null;
    Transaction tx = null;
    final List<DeviceInventoryComplianceEntity> entities = new ArrayList<DeviceInventoryComplianceEntity>();

    try {
        session = dao.getNewSession();
        tx = session.beginTransaction();
        final DeviceInventoryEntity deviceInventory = (DeviceInventoryEntity) session
                .get(DeviceInventoryEntity.class, deviceInventoryId);
        if (deviceInventory == null) {
            return Collections.emptyList();
        }

        final String hql = "from DeviceInventoryComplianceEntity dic"
                + " where dic.deviceInventoryComplianceId.deviceInventory = :deviceInventory";
        final Query query = session.createQuery(hql);
        query.setEntity("deviceInventory", deviceInventory);
        entities.addAll(query.list());
    } catch (Exception e) {
        logger.warn("Caught exception during findByDeviceInventory: " + e);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction during findByDeviceInventory: " + ex);
        }
        throw new AsmManagerInternalErrorException("findByDeviceInventory", "DeviceInventoryComplianceDAO", e);
    } finally {
        dao.cleanupSession(session, "findByDeviceInventory");
    }

    return entities;
}

From source file:com.dell.asm.asmcore.asmmanager.db.DeviceInventoryComplianceDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<DeviceInventoryComplianceEntity> findByFirmwareRepository(final String firmwareRepositoryId) {
    if (StringUtils.isBlank(firmwareRepositoryId)) {
        return Collections.emptyList();
    }/*w  w  w . j  a va  2  s.c om*/
    Session session = null;
    Transaction tx = null;
    final List<DeviceInventoryComplianceEntity> entities = new ArrayList<DeviceInventoryComplianceEntity>();

    try {
        session = dao.getNewSession();
        tx = session.beginTransaction();
        final FirmwareRepositoryEntity firmwareRepository = (FirmwareRepositoryEntity) session
                .get(FirmwareRepositoryEntity.class, firmwareRepositoryId);
        if (firmwareRepository == null) {
            return Collections.emptyList();
        }

        final String hql = "from DeviceInventoryComplianceEntity dic"
                + " where dic.deviceInventoryComplianceId.firmwareRepository = :firmwareRepository";
        final Query query = session.createQuery(hql);
        query.setEntity("firmwareRepository", firmwareRepository);
        entities.addAll(query.list());
    } catch (Exception e) {
        logger.warn("Caught exception during findByFirmwareRepository: " + e);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction during findByFirmwareRepository: " + ex);
        }
        throw new AsmManagerInternalErrorException("findByFirmwareRepository", "DeviceInventoryComplianceDAO",
                e);
    } finally {
        dao.cleanupSession(session, "findByFirmwareRepository");
    }

    return entities;
}

From source file:com.edgenius.wiki.dao.hibernate.HistoryDAOHibernate.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<History> getUserContributedHistories(final User user) {
    Query query;
    boolean anonymous = false;
    if (user == null || user.isAnonymous()) {
        anonymous = true;/* ww  w.j a  v a2 s  .  com*/
        query = getCurrentSesssion().createQuery(GET_ANONYMOUS_CONTRIBUTED_HISTORIES);
    } else {
        query = getCurrentSesssion().createQuery(GET_USER_CONTRIBUTED_HISTORIES);
        query.setEntity("user", user);
    }
    List<History> histories = new ArrayList<History>();
    List<Object[]> list = query.list();
    for (Object[] obj : list) {
        History his = new History();
        Space space = new Space();
        User creator = new User();
        User modifier = new User();
        int idx = 0;

        his.setUid((Integer) obj[idx++]);
        his.setTitle((String) obj[idx++]);
        his.setPageUuid((String) obj[idx++]);
        his.setVersion((Integer) obj[idx++]);

        space.setUnixName((String) obj[idx++]);
        his.setSpace(space);

        if (!anonymous) {
            creator.setUsername((String) obj[idx++]);
        } else {
            creator.setUsername(User.ANONYMOUS_USERNAME);
        }
        his.setCreator(creator);
        ;
        his.setCreatedDate((Date) obj[idx++]);
        if (!anonymous) {
            modifier.setUsername((String) obj[idx++]);
        } else {
            modifier.setUsername(User.ANONYMOUS_USERNAME);
        }
        his.setModifier(modifier);
        ;
        his.setModifiedDate((Date) obj[idx++]);

        histories.add(his);
    }

    return histories;
}

From source file:com.edgenius.wiki.dao.hibernate.PageDAOHibernate.java

License:Open Source License

public List<Page> getUserUpdatedPagesInSpace(final String spaceUname, final User user, final int returnNum) {

    Query query;
    if (user == null || user.isAnonymous()) {
        query = getCurrentSesssion().createQuery(GET_ANONYMOUS_PAGES);
        query.setString(0, spaceUname);/*from   www  .j  a  v  a2  s.  c  om*/
    } else {
        query = getCurrentSesssion().createQuery(GET_USER_PAGES);
        query.setEntity(0, user);
        query.setEntity(1, user);
        query.setString(2, spaceUname);
    }
    if (returnNum > 0)
        query.setMaxResults(returnNum);

    return query.list();
}

From source file:com.edgenius.wiki.dao.hibernate.PageDAOHibernate.java

License:Open Source License

public List<Page> getUserContributedPages(final User user, final int limit) {
    Query query;
    if (user == null || user.isAnonymous()) {
        query = getCurrentSesssion().createQuery(GET_ANONYMOUS_CONTRIBUTED_PAGES);
    } else {//from  w  ww .jav  a  2  s  . c o m
        query = getCurrentSesssion().createQuery(GET_USER_CONTRIBUTED_PAGES);
        query.setEntity("user", user);
    }
    if (limit > 0)
        query.setMaxResults(limit);
    return query.list();
}