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:gov.nih.nci.caarray.dao.VocabularyDaoImpl.java

License:BSD License

/**
 * {@inheritDoc}/*from  ww w .j  a  v  a2  s.  c  o m*/
 */
@SuppressWarnings(UNCHECKED)
public List<Category> searchForCharacteristicCategory(Experiment experiment,
        Class<? extends AbstractCharacteristic> characteristicClass, String keyword) {
    StringBuffer sb = new StringBuffer();

    sb.append("SELECT DISTINCT c.category FROM ").append(characteristicClass.getName());
    sb.append(" c join c.bioMaterial bm WHERE (1=1) ");

    if (keyword != null) {
        sb.append(" AND c.category.name like :keyword");
    }

    if (experiment != null) {
        sb.append(" AND bm.experiment = :experiment");
    }

    sb.append(" ORDER BY c.category.name");

    Query q = getCurrentSession().createQuery(sb.toString());
    if (keyword != null) {
        q.setString("keyword", keyword + "%");
    }
    if (experiment != null) {
        q.setEntity("experiment", experiment);
    }

    return q.list();
}

From source file:gov.nih.nci.firebird.service.investigatorprofile.InvestigatorProfileServiceBean.java

License:Open Source License

@Override
public InvestigatorProfile getByPerson(Person person) {
    if (person == null) {
        throw new IllegalArgumentException("Person was null");
    }/*from   w w  w. j a v  a2  s . c o  m*/
    if (person.getId() == null) {
        return null;
    }
    Query q = getSession().createQuery(PROFILE_BY_PERSON_QUERY);
    q.setEntity("person", person);
    return (InvestigatorProfile) q.uniqueResult();
}

From source file:gov.nih.nci.firebird.service.investigatorprofile.InvestigatorProfileServiceBean.java

License:Open Source License

@SuppressWarnings("unchecked")
// hibernate does not provide typed arguments
@Override/*  w w  w .  j  a  v a2s.  co  m*/
public List<CertifiedSpecialtyType> getSpecialtiesByBoard(CertifiedSpecialtyBoard board) {
    if (board == null) {
        throw new IllegalArgumentException("Certification Board was null");
    }
    List<CertifiedSpecialtyType> types = null;
    Query q = getSession().createQuery(SPECIALTIES_BY_BOARD);
    q.setEntity("board", board);
    types = q.list();
    Collections.sort(types, new Comparator<CertifiedSpecialtyType>() {

        @Override
        public int compare(CertifiedSpecialtyType o1, CertifiedSpecialtyType o2) {
            int ret = o1.getDesignation().name().compareTo(o2.getDesignation().name());

            if (ret == 0) {
                ret = o1.getName().compareTo(o2.getName());
            }

            return ret;
        }
    });
    return types;
}

From source file:gov.nih.nci.firebird.service.user.FirebirdUserServiceBean.java

License:Open Source License

@Override
@SuppressWarnings("PMD.UnnecessaryLocalBeforeReturn")
// provides clarity
public boolean checkPersonAssociated(Person person) {
    if (person == null) {
        throw new IllegalArgumentException("Invalid Person provided!");
    }/*from  w w  w . j a v a2  s  . c  om*/
    if (person.getId() == null) {
        return false;
    }
    Query query = getSession().createQuery(FIND_USER_BY_INVESTIGATOR_PERSON_QUERY);
    query.setEntity("person", person);
    boolean isAssociated = query.uniqueResult() != null;
    return isAssociated;
}

From source file:itensil.repository.hibernate.EntityManager.java

License:Open Source License

public RepositoryNode move(Repository srcRepository, String srcNodeId, Repository dstRepository, String dstUri)
        throws AccessDeniedException, NotFoundException, DuplicateException, LockException {

    User caller = SecurityAssociation.getUser();
    authorizedCheck(caller);//  w ww  . j  av  a2 s .  c o  m

    if (!(dstRepository instanceof RepositoryEntity) || !(srcRepository instanceof RepositoryEntity)) {

        // this is slow and looses version history
        RepositoryNode resNode = copy(srcRepository, srcNodeId, dstRepository, dstUri, true);
        srcRepository.removeNode(srcNodeId);
        return resNode;
    }

    NodeEntity srcNode = (NodeEntity) srcRepository.getNode(srcNodeId, true);

    String dstParentUri = UriHelper.getParent(dstUri);
    String srcParentUri = UriHelper.getParent(srcNode.getUri());

    // check if inplace move
    boolean inplace = srcParentUri.equals(dstParentUri);
    NodeEntity dstParentNode = null;
    if (!inplace) {

        dstParentNode = (NodeEntity) dstRepository.getNodeByUri(dstParentUri, false);
        if (!dstParentNode.isCollection()) {
            throw new AccessDeniedException(dstParentUri, "collection");
        }

        // recursive check
        NodeEntity dstAncestor = dstParentNode;
        while (dstAncestor != null) {
            if (srcNodeId.equals(dstAncestor.getNodeId()))
                throw new DuplicateException("recursive");
            dstAncestor = dstAncestor.getParentNode();
        }

        // need manage for relocating
        srcNode.checkPermission(DefaultNodePermission.MANAGE);

        // and no locks
        srcNode.checkLocked();

        // and can create
        dstParentNode.checkPermission(DefaultNodePermission.CREATE);
    }

    // duplicate check
    try {

        dstRepository.getNodeByUri(dstUri, false);
        throw new DuplicateException(dstUri);

    } catch (NotFoundException nfe) {
        // this is good
    }

    String localDstUri = UriHelper.localizeUri(dstRepository.getMount(), dstUri);

    Session session = HibernateUtil.getSession();
    if (srcNode.isCollection()) {
        Query qry = session.getNamedQuery("Repo.moveCollection");
        qry.setEntity("repo", srcRepository);
        qry.setString("uriPat", srcNode.getLocalUri() + "/%");
        qry.setEntity("dstRepo", dstRepository);
        qry.setString("dstUri", localDstUri);
        qry.setInteger("subUriLen", srcNode.getLocalUri().length());
        qry.executeUpdate();
    }

    srcNode.setLocalUri(localDstUri);
    if (!inplace) {
        Set<NodeEntity> parKids = srcNode.getParentNode().getChildEntities();
        if (Hibernate.isInitialized(parKids)) {
            parKids.remove(srcNode);
        }
        srcNode.setParentNode(dstParentNode);
        srcNode.setRepoEntity((RepositoryEntity) dstRepository);

        parKids = dstParentNode.getChildEntities();
        if (Hibernate.isInitialized(parKids)) {
            parKids.add(srcNode);
        }
    }

    session.update(srcNode);
    return srcNode;
}

From source file:itensil.repository.hibernate.NodeEntity.java

License:Open Source License

/**
 * /* w ww  . ja  v  a2  s.  c om*/
 * @param version
 * @return ver object
 */
protected VersionEntity resolveVersion(NodeVersion version) {
    VersionEntity def = getDefaultVersionEnt();
    if (def == null) {
        return null;
    }

    Session session = HibernateUtil.getSession();
    String num = version.getNumber();
    if (!Check.isEmpty(num)) {

        if (num.equals(def.getNumber())) {
            return def;
        }
        Query qry = session.getNamedQuery("Repo.resolveVerNumber");
        qry.setEntity("node", this);
        qry.setString("number", num);
        return (VersionEntity) qry.uniqueResult();
    } else if (!Check.isEmpty(version.getLabel())) {

        if (version.getLabel().equals(def.getLabel())) {
            return def;
        }
        Query qry = session.getNamedQuery("Repo.resolveVerLabel");
        qry.setEntity("node", this);
        qry.setString("label", version.getLabel());
        return (VersionEntity) qry.uniqueResult();
    } else if (version.isDefault()) {

        // label & number both empty, so it must mean the plain-old default
        return def;
    }
    return null;
}

From source file:itensil.repository.hibernate.NodeEntity.java

License:Open Source License

public void pruneVersions(int keepRecentCount) throws AccessDeniedException, LockException {
    checkPermission(DefaultNodePermission.WRITE);
    checkLocked();//w ww .j  ava2  s. c  o  m
    int remain = keepRecentCount - 1;
    Session sess = HibernateUtil.getSession();
    Query qry = sess.getNamedQuery("Repo.deleteContent");
    for (VersionEntity ve : getVersionEntities()) {
        if (!ve.isDefault() && remain > 0) {
            remain--;
        } else {
            qry.setEntity("ver", ve);
            qry.executeUpdate();
            sess.delete(ve);
        }
    }
}

From source file:itensil.repository.hibernate.RepositoryEntity.java

License:Open Source License

/**
 *
 * @param uri//from  w w w  .  ja  v  a 2 s. co  m
 * @return node object
 * @throws AccessDeniedException
 * @throws NotFoundException
 */
public MutableRepositoryNode getNodeByUri(String uri, boolean forUpdate)
        throws AccessDeniedException, NotFoundException {

    User caller = SecurityAssociation.getUser();
    authorizedCheck(caller);

    String localUri = localizeUri(uri);

    Session session = HibernateUtil.getSession();
    Query qry = session.getNamedQuery("Repo.getNodeByUri");
    qry.setEntity("repo", this);
    qry.setString("uri", localUri);
    if (forUpdate)
        qry.setLockMode("node", LockMode.UPGRADE);
    NodeEntity node = (NodeEntity) qry.uniqueResult();
    if (node == null) {
        if (!existingAncestorBlock(caller, localUri)) {
            throw new NotFoundException(uri);
        } else {
            throw new AccessDeniedException(uri, "read");
        }
    }
    if (!hasPermission(caller, node, DefaultNodePermission.READ)) {
        throw new AccessDeniedException(uri, "read");
    }
    node.setRepoEntity(this);
    return node;
}

From source file:itensil.repository.hibernate.RepositoryEntity.java

License:Open Source License

/**
 *
 * @param nodeId/*from w ww  . j a v a2 s  .  com*/
 * @return node object
 * @throws AccessDeniedException
 * @throws NotFoundException
 */
public MutableRepositoryNode getNode(String nodeId, boolean forUpdate)
        throws AccessDeniedException, NotFoundException {

    User caller = SecurityAssociation.getUser();
    authorizedCheck(caller);
    Session session = HibernateUtil.getSession();

    Query qry = session.getNamedQuery("Repo.getNode");
    qry.setString("id", nodeId);
    qry.setEntity("repo", this);
    if (forUpdate)
        qry.setLockMode("node", LockMode.UPGRADE);

    NodeEntity node = (NodeEntity) qry.uniqueResult();

    if (node == null) {
        throw new NotFoundException(nodeId);
    }
    if (!hasPermission(caller, node, DefaultNodePermission.READ)) {
        throw new AccessDeniedException(nodeId, "read");
    }
    node.setRepoEntity(this);
    return node;
}

From source file:itensil.repository.hibernate.RepositoryEntity.java

License:Open Source License

/**
 *
 * @param uri/*w w w  . j a  va 2  s .  com*/
 * @param isCollection
 * @param owner
 * @return node object
 * @throws AccessDeniedException
 * @throws NotFoundException
 * @throws DuplicateException
 * @throws LockException
 */
public MutableRepositoryNode createNode(String uri, boolean isCollection, User owner)
        throws AccessDeniedException, NotFoundException, DuplicateException, LockException {

    User caller = SecurityAssociation.getUser();
    authorizedCheck(caller);
    Session session = HibernateUtil.getSession();

    uri = localizeUri(uri);

    // check parent elegability
    String parentUri = UriHelper.getParent(uri);
    if (parentUri.equals(uri)) {
        throw new DuplicateException(parentUri);
    }
    Query qry = session.getNamedQuery("Repo.getNodeByUri");
    qry.setEntity("repo", this);
    qry.setString("uri", parentUri);
    NodeEntity parentNode = (NodeEntity) qry.uniqueResult();
    if (parentNode == null) {
        if (!existingAncestorBlock(caller, parentUri)) {
            throw new NotFoundException(absoluteUri(parentUri));
        } else {
            throw new AccessDeniedException(absoluteUri(parentUri), "read");
        }
    }
    if (!parentNode.isCollection()) {
        throw new AccessDeniedException(absoluteUri(parentUri), "collection");
    }
    if (!hasPermission(caller, parentNode, DefaultNodePermission.CREATE)) {
        throw new AccessDeniedException(absoluteUri(parentUri), "create");
    }

    // check for duplicates
    qry.setString("uri", uri);
    Iterator itr = qry.iterate();
    if (itr.hasNext()) {
        throw new DuplicateException(uri);
    }

    // parent locked?
    checkLocked(caller, parentNode);

    NodeEntity node = new NodeEntity();
    node.initNew();
    node.setOwner(owner);
    node.setParentNode(parentNode);
    node.setCollection(isCollection);
    node.setLocalUri(uri);
    node.setRepoEntity(this);
    node.setCreateTime(new Date());
    session.persist(node);

    Set<NodeEntity> parKids = parentNode.getChildEntities();
    if (Hibernate.isInitialized(parKids)) {
        parKids.add(node);
    }

    return node;
}