Example usage for javax.persistence TypedQuery setHint

List of usage examples for javax.persistence TypedQuery setHint

Introduction

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

Prototype

TypedQuery<X> setHint(String hintName, Object value);

Source Link

Document

Set a query property or hint.

Usage

From source file:org.cherchgk.services.SecurityService.java

/**
 *  ? ( ?? {@link org.cherchgk.domain.security.User})
 *  .//from ww w .  j a va 2s  .c o m
 *
 * @param userId  ?.
 * @return   null, ?  ?  
 *  .
 */
public User getUserById(Long userId) {
    TypedQuery<User> userQuery = entityManager
            .createQuery("select user " + "from User user " + "where user.id = :userId", User.class)
            .setParameter("userId", userId);
    userQuery.setHint("org.hibernate.cacheable", true);
    List<User> users = userQuery.getResultList();
    return users.isEmpty() ? null : users.get(0);
}

From source file:org.cherchgk.services.SecurityService.java

/**
 *     ./* ww  w .  j  av  a 2 s.  c  om*/
 *
 * @param roleName ? .
 * @return   null, ?  ?    ??.
 */
public Role getRoleByName(String roleName) {
    TypedQuery<Role> roleQuery = entityManager
            .createQuery("select role " + "from Role role " + "where role.name = :roleName", Role.class)
            .setParameter("roleName", roleName);
    roleQuery.setHint("org.hibernate.cacheable", true);
    List<Role> roles = roleQuery.getResultList();
    return roles.isEmpty() ? null : roles.get(0);
}

From source file:org.querybyexample.jpa.GenericRepository.java

/**
 * Set hints for 2d level cache./*  w w  w .j a  v a2 s . co  m*/
 */
protected void applyCacheHints(TypedQuery<?> typedQuery, SearchParameters sp) {
    if (sp.isCacheable()) {
        typedQuery.setHint("org.hibernate.cacheable", true);

        if (sp.hasCacheRegion()) {
            typedQuery.setHint("org.hibernate.cacheRegion", sp.getCacheRegion());
        } else {
            typedQuery.setHint("org.hibernate.cacheRegion", cacheRegion);
        }
    }
}

From source file:org.cherchgk.services.SecurityService.java

/**
 *  ? ( ?? {@link org.cherchgk.domain.security.User})
 *    ().// ww w .  j  av  a2 s.  c o  m
 *
 * @param username ? () ?.
 * @return   null, ? ? ?    .
 */
public User getUserByName(String username) {
    TypedQuery<User> userQuery = entityManager
            .createQuery("select user " + "from User user " + "where user.username = :username", User.class)
            .setParameter("username", username);
    userQuery.setHint("org.hibernate.cacheable", true);
    List<User> users = userQuery.getResultList();
    return users.isEmpty() ? null : users.get(0);
}

From source file:org.cherchgk.services.SecurityService.java

/**
 * ?    .//  w  w  w. jav a  2 s.c  o m
 *
 * @param tokenUUID  .
 * @return ?   null.
 */
private Token getTokenByUUID(String tokenUUID) {
    TypedQuery<Token> tokenQuery = entityManager
            .createQuery("select token " + "from Token token " + "where uuid = :tokenUUID", Token.class)
            .setParameter("tokenUUID", tokenUUID);
    tokenQuery.setHint("org.hibernate.cacheable", true);
    List<Token> tokens = tokenQuery.getResultList();

    if (tokens.isEmpty()) {
        return null;
    }

    return tokens.get(0);
}

From source file:org.cherchgk.services.SecurityService.java

/**
 *  ? ( ?? {@link org.cherchgk.domain.security.User}
 *  ? ? ./*from   www.j av  a  2 s .co  m*/
 *
 * @param email ?? ? .
 * @return   null, ?  ?  ?
 * ?   .
 */
public User getUserByEmail(String email) {
    TypedQuery<User> userQuery = entityManager
            .createQuery("select user " + "from User user " + "where user.email = :email", User.class)
            .setParameter("email", email.trim().toLowerCase());
    userQuery.setHint("org.hibernate.cacheable", true);
    List<User> users = userQuery.getResultList();
    return users.isEmpty() ? null : users.get(0);
}

From source file:cn.guoyukun.spring.jpa.repository.support.SimpleBaseRepository.java

private TypedQuery<M> applyRepositoryMethodMetadata(TypedQuery<M> query) {

    if (getRepositoryMethodMetadata() == null) {
        return query;
    }/*from  ww w. ja va  2  s.  c o m*/

    LockModeType type = getRepositoryMethodMetadata().getLockModeType();
    TypedQuery<M> toReturn = type == null ? query : query.setLockMode(type);

    for (Entry<String, Object> hint : getRepositoryMethodMetadata().getQueryHints().entrySet()) {
        query.setHint(hint.getKey(), hint.getValue());
    }

    return Jpa21Utils.tryConfigureFetchGraph(em, toReturn, getRepositoryMethodMetadata().getEntityGraph());
}

From source file:org.broadleafcommerce.cms.file.dao.StaticAssetDaoImpl.java

@Override
public StaticAsset readStaticAssetById(Long id) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<StaticAsset> criteria = builder.createQuery(StaticAsset.class);
    Root<StaticAssetImpl> handler = criteria.from(StaticAssetImpl.class);
    criteria.select(handler);//from w w  w  .ja  v  a 2s.  c  o  m
    List<Predicate> restrictions = new ArrayList<Predicate>();
    restrictions.add(builder.equal(handler.get("id"), id));

    if (queryExtensionManager != null) {
        queryExtensionManager.getProxy().setup(StaticAssetImpl.class, null);
        queryExtensionManager.getProxy().refineRetrieve(StaticAssetImpl.class, null, builder, criteria, handler,
                restrictions);
    }
    criteria.where(restrictions.toArray(new Predicate[restrictions.size()]));

    TypedQuery<StaticAsset> query = em.createQuery(criteria);
    query.setHint(QueryHints.HINT_CACHEABLE, true);
    List<StaticAsset> response = query.getResultList();
    if (response.size() > 0) {
        return response.get(0);
    }
    return null;
}

From source file:org.broadleafcommerce.cms.file.dao.StaticAssetDaoImpl.java

@Override
public StaticAsset readStaticAssetByFullUrl(String fullUrl) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<StaticAsset> criteria = builder.createQuery(StaticAsset.class);
    Root<StaticAssetImpl> handler = criteria.from(StaticAssetImpl.class);
    criteria.select(handler);//from w w w  .  jav a 2 s . c o  m

    List<Predicate> restrictions = new ArrayList<Predicate>();
    List<Order> sorts = new ArrayList<Order>();
    restrictions.add(builder.equal(handler.get("fullUrl"), fullUrl));
    try {
        if (queryExtensionManager != null) {
            queryExtensionManager.getProxy().setup(StaticAssetImpl.class, null);
            queryExtensionManager.getProxy().refineRetrieve(StaticAssetImpl.class, null, builder, criteria,
                    handler, restrictions);
            queryExtensionManager.getProxy().refineOrder(StaticAssetImpl.class, null, builder, criteria,
                    handler, sorts);
        }
        criteria.where(restrictions.toArray(new Predicate[restrictions.size()]));
        if (!org.apache.commons.collections.CollectionUtils.isEmpty(sorts)) {
            criteria.orderBy(sorts);
        }

        TypedQuery<StaticAsset> query = em.createQuery(criteria);
        query.setHint(QueryHints.HINT_CACHEABLE, true);
        List<StaticAsset> response = query.getResultList();
        if (response.size() > 0) {
            return response.get(0);
        }
        return null;
    } finally {
        if (queryExtensionManager != null) {
            queryExtensionManager.getProxy().breakdown(StaticAssetImpl.class, null);
        }
    }
}

From source file:org.broadleafcommerce.common.config.dao.SystemPropertiesDaoImpl.java

@Override
public List<SystemProperty> readAllSystemProperties() {
    TypedQuery<SystemProperty> query = em.createNamedQuery("BC_READ_ALL_SYSTEM_PROPERTIES",
            SystemProperty.class);
    query.setHint(QueryHints.HINT_CACHEABLE, true);
    return query.getResultList();
}