Example usage for javax.persistence TypedQuery getSingleResult

List of usage examples for javax.persistence TypedQuery getSingleResult

Introduction

In this page you can find the example usage for javax.persistence TypedQuery getSingleResult.

Prototype

X getSingleResult();

Source Link

Document

Execute a SELECT query that returns a single result.

Usage

From source file:org.apache.rave.portal.repository.impl.JpaPageRepository.java

@Override
public PageUser getSingleRecord(String userId, String pageId) {
    TypedQuery<JpaPageUser> query = manager.createNamedQuery(JpaPageUser.GET_SINGLE_RECORD, JpaPageUser.class);
    query.setParameter("userId", userId);
    query.setParameter("pageId", pageId == null ? null : Long.parseLong(pageId));
    return expandPageUserProperties(query.getSingleResult());
}

From source file:org.apache.rave.portal.repository.impl.JpaPageTemplateRepository.java

@Override
public JpaPageTemplate getDefaultPage(String pageType) {
    TypedQuery<JpaPageTemplate> query = manager
            .createNamedQuery(JpaPageTemplate.PAGE_TEMPLATE_GET_DEFAULT_PAGE_BY_TYPE, JpaPageTemplate.class);
    query.setParameter("pageType", pageType.toUpperCase());
    return expandProperties(query.getSingleResult());
}

From source file:org.apache.syncope.core.persistence.jpa.dao.JPAAccessTokenDAO.java

@Transactional(readOnly = true)
@Override/*from  w ww  .  j  a  v a 2 s .c  o m*/
public AccessToken findByOwner(final String username) {
    TypedQuery<AccessToken> query = entityManager().createQuery(
            "SELECT e FROM " + JPAAccessToken.class.getSimpleName() + " e " + "WHERE e.owner=:username",
            AccessToken.class);
    query.setParameter("username", username);

    AccessToken result = null;
    try {
        result = query.getSingleResult();
    } catch (NoResultException e) {
        LOG.debug("No token for user {} could be found", username, e);
    }

    return result;
}

From source file:org.apache.syncope.core.persistence.jpa.dao.JPAAnyObjectDAO.java

@Override
public AnyObject findByName(final String name) {
    TypedQuery<AnyObject> query = entityManager().createQuery(
            "SELECT e FROM " + JPAAnyObject.class.getSimpleName() + " e WHERE e.name = :name", AnyObject.class);
    query.setParameter("name", name);

    AnyObject result = null;/*from   w  w  w .j  ava  2s.  c o  m*/
    try {
        result = query.getSingleResult();
    } catch (NoResultException e) {
        LOG.debug("No any object found with name {}", name, e);
    }

    return result;
}

From source file:org.apache.syncope.core.persistence.jpa.dao.JPAGroupDAO.java

@Override
public Group findByName(final String name) {
    TypedQuery<Group> query = entityManager().createQuery(
            "SELECT e FROM " + JPAGroup.class.getSimpleName() + " e WHERE e.name = :name", Group.class);
    query.setParameter("name", name);

    Group result = null;/*ww  w . j  av  a  2  s  .c  o m*/
    try {
        result = query.getSingleResult();
    } catch (NoResultException e) {
        LOG.debug("No group found with name {}", name, e);
    }

    return result;
}

From source file:org.apache.syncope.core.persistence.jpa.dao.JPARealmDAO.java

@Override
public Realm getRoot() {
    TypedQuery<Realm> query = entityManager().createQuery(
            "SELECT e FROM " + JPARealm.class.getSimpleName() + " e WHERE e.parent IS NULL", Realm.class);

    Realm result = null;/*from   w w  w. j  a v a2s .c  om*/
    try {
        result = query.getSingleResult();
    } catch (NoResultException e) {
        LOG.debug("Root realm not found", e);
    }

    return result;
}

From source file:org.apache.syncope.core.persistence.jpa.dao.JPAUserDAO.java

@Override
public User findByUsername(final String username) {
    TypedQuery<User> query = entityManager().createQuery(
            "SELECT e FROM " + JPAUser.class.getSimpleName() + " e WHERE e.username = :username", User.class);
    query.setParameter("username", username);

    User result = null;//from  w  ww .  ja  va 2  s  . c  o  m
    try {
        result = query.getSingleResult();
    } catch (NoResultException e) {
        LOG.debug("No user found with username {}", username, e);
    }

    return result;
}

From source file:org.apache.syncope.core.persistence.jpa.dao.JPAUserDAO.java

@Override
public User findByToken(final String token) {
    TypedQuery<User> query = entityManager().createQuery(
            "SELECT e FROM " + JPAUser.class.getSimpleName() + " e WHERE e.token LIKE :token", User.class);
    query.setParameter("token", token);

    User result = null;/*from w ww.ja v  a 2s  .co m*/
    try {
        result = query.getSingleResult();
    } catch (NoResultException e) {
        LOG.debug("No user found with token {}", token, e);
    }

    return result;
}

From source file:org.broadleafcommerce.cms.admin.server.handler.PageItemCriteriaCustomPersistenceHandler.java

@Override
public Entity update(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao,
        RecordHelper helper) throws ServiceException {
    Entity entity = persistencePackage.getEntity();
    removeHtmlEncoding(entity);/* w w  w  .j a  v  a 2s .c  om*/
    try {
        PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
        Map<String, FieldMetadata> adminProperties = helper
                .getSimpleMergedProperties(PageItemCriteria.class.getName(), persistencePerspective);
        Object primaryKey = helper.getPrimaryKey(entity, adminProperties);
        PageItemCriteria adminInstance = (PageItemCriteria) dynamicEntityDao
                .retrieve(Class.forName(entity.getType()[0]), primaryKey);
        if (adminInstance.getPage().getLockedFlag()) {
            /*
            This may be an attempt to delete a target item criteria off an otherwise un-edited, production StructuredContent instance
             */
            CriteriaBuilder criteriaBuilder = dynamicEntityDao.getStandardEntityManager().getCriteriaBuilder();
            CriteriaQuery<Page> query = criteriaBuilder.createQuery(Page.class);
            Root<PageImpl> root = query.from(PageImpl.class);
            query.where(criteriaBuilder.and(criteriaBuilder.equal(root.get("archivedFlag"), Boolean.FALSE),
                    criteriaBuilder.equal(root.get("originalPageId"), adminInstance.getPage().getId())));
            query.select(root);
            TypedQuery<Page> scQuery = dynamicEntityDao.getStandardEntityManager().createQuery(query);
            try {
                checkCriteria: {
                    Page myContent = scQuery.getSingleResult();
                    for (PageItemCriteria itemCriteria : myContent.getQualifyingItemCriteria()) {
                        if (itemCriteria.getOrderItemMatchRule().equals(adminInstance.getOrderItemMatchRule())
                                && itemCriteria.getQuantity().equals(adminInstance.getQuantity())) {
                            //manually set the values - otherwise unwanted properties will be set
                            itemCriteria.setOrderItemMatchRule(
                                    entity.findProperty("orderItemMatchRule").getValue());
                            itemCriteria
                                    .setQuantity(Integer.parseInt(entity.findProperty("quantity").getValue()));
                            adminInstance = itemCriteria;
                            break checkCriteria;
                        }
                    }
                    throw new RuntimeException("Unable to find an item criteria to update");
                }
            } catch (Exception e) {
                throw new IllegalArgumentException("Unable to update a locked record");
            }
        } else {
            adminInstance = (PageItemCriteria) helper.createPopulatedInstance(adminInstance, entity,
                    adminProperties, false);
        }
        adminInstance = (PageItemCriteria) dynamicEntityDao.merge(adminInstance);
        Entity adminEntity = helper.getRecord(adminProperties, adminInstance, null, null);

        return adminEntity;
    } catch (Exception e) {
        LOG.error("Unable to execute persistence activity", e);
        throw new ServiceException("Unable to update entity for " + entity.getType()[0], e);
    }
}

From source file:org.broadleafcommerce.cms.admin.server.handler.PageItemCriteriaCustomPersistenceHandler.java

@Override
public void remove(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao,
        RecordHelper helper) throws ServiceException {
    Entity entity = persistencePackage.getEntity();
    try {/*from  w  w w .ja  v  a2  s. c  o  m*/
        PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
        Map<String, FieldMetadata> adminProperties = helper
                .getSimpleMergedProperties(PageItemCriteria.class.getName(), persistencePerspective);
        Object primaryKey = helper.getPrimaryKey(entity, adminProperties);
        PageItemCriteria adminInstance = (PageItemCriteria) dynamicEntityDao
                .retrieve(Class.forName(entity.getType()[0]), primaryKey);

        if (adminInstance.getPage().getLockedFlag()) {
            /*
            This may be an attempt to delete a target item criteria off an otherwise un-edited, production StructuredContent instance
             */
            CriteriaBuilder criteriaBuilder = dynamicEntityDao.getStandardEntityManager().getCriteriaBuilder();
            CriteriaQuery<Page> query = criteriaBuilder.createQuery(Page.class);
            Root<PageImpl> root = query.from(PageImpl.class);
            query.where(criteriaBuilder.and(criteriaBuilder.equal(root.get("archivedFlag"), Boolean.FALSE),
                    criteriaBuilder.equal(root.get("originalPageId"), adminInstance.getPage().getId())));
            query.select(root);
            TypedQuery<Page> scQuery = dynamicEntityDao.getStandardEntityManager().createQuery(query);
            try {
                Page myContent = scQuery.getSingleResult();
                for (PageItemCriteria itemCriteria : myContent.getQualifyingItemCriteria()) {
                    if (itemCriteria.getOrderItemMatchRule().equals(adminInstance.getOrderItemMatchRule())
                            && itemCriteria.getQuantity().equals(adminInstance.getQuantity())) {
                        myContent.getQualifyingItemCriteria().remove(itemCriteria);
                        return;
                    }
                }
                throw new RuntimeException("Unable to find an item criteria to delete");
            } catch (Exception e) {
                throw new IllegalArgumentException("Unable to update a locked record");
            }
        }

        dynamicEntityDao.remove(adminInstance);
    } catch (Exception e) {
        LOG.error("Unable to execute persistence activity", e);
        throw new ServiceException("Unable to remove entity for " + entity.getType()[0], e);
    }
}