Java 类org.hibernate.jpa.QueryHints 实例源码

项目:xm-ms-entity    文件:EntityGraphRepositoryImpl.java   
@Override
@Transactional(readOnly = true)
public T findOne(I id, List<String> embed) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<T> criteriaQuery = builder.createQuery(domainClass);
    Root<T> root = criteriaQuery.from(domainClass);
    criteriaQuery.where(builder.equal(root.get("id"), id));

    TypedQuery<T> query = entityManager
        .createQuery(criteriaQuery)
        .setHint(QueryHints.HINT_LOADGRAPH, createEnitityGraph(embed));

    List<T> resultList = query.getResultList();
    if (CollectionUtils.isEmpty(resultList)) {
        return null;
    }
    return resultList.get(0);
}
项目:high-performance-java-persistence    文件:ProjectionTest.java   
@Test
public void testFetchSize() {
    int pageStart = 20;
    int pageSize = 50;

    doInJPA(entityManager -> {
        List<PostCommentSummary> summaries = entityManager.createQuery(
            "select new " +
            "   com.vladmihalcea.book.hpjp.hibernate.fetching.PostCommentSummary( " +
            "       p.id, p.title, c.review ) " +
            "from PostComment c " +
            "join c.post p")
        .setFirstResult(pageStart)
        .setMaxResults(pageSize)
        .setHint(QueryHints.HINT_FETCH_SIZE, pageSize)
        .getResultList();
        assertEquals(pageSize, summaries.size());
    });
}
项目:high-performance-java-persistence    文件:PostgreSQLScrollableResultsStreamingTest.java   
@Test
public void testStream() {
    List<Post> posts = doInJPA(entityManager -> {
        try(Stream<Post> postStream = entityManager
            .createQuery(
                "select p " +
                "from Post p " +
                "order by p.createdOn desc", Post.class)
            .setHint( QueryHints.HINT_FETCH_SIZE, 50 )
            .unwrap(Query.class)
            .stream()
        ) {
            return postStream.limit( 50 ).collect( Collectors.toList() );
        }
    });

    assertEquals(50, posts.size());
}
项目:high-performance-java-persistence    文件:PostgreSQLScrollableResultsStreamingTest.java   
@Test
public void testStreamExecutionPlan() {
    List<Object[]> executionPlanLines = doInJPA(entityManager -> {
        try(Stream<Object[]> postStream = entityManager
            .createNativeQuery(
                "EXPLAIN ANALYZE " +
                "SELECT p " +
                "FROM post p " +
                "ORDER BY p.created_on DESC")
            .setHint( QueryHints.HINT_FETCH_SIZE, 50 )
            .unwrap(Query.class)
            .stream()
        ) {
            return postStream.collect( Collectors.toList() );
        }
    });

    LOGGER.info( "Execution plan: {}",
                 executionPlanLines
                 .stream()
                 .map( line -> (String) line[0] )
                 .collect( Collectors.joining( "\n" ) )
    );
}
项目:EasyEE    文件:CommonDAOSpringImpl.java   
/**
 * CommonDAO 内部方法,NamedQuery
 * 
 * @param entityManager
 *            entityManager
 * @param name
 *            name
 * @param cacheable
 *            cacheable
 * @param cacheRegion
 *            cacheRegion
 * @param values
 *            values
 * @return Query对象
 */
private Query createNamedQuery(EntityManager entityManager, String name, boolean cacheable, String cacheRegion,
        Map<String, Object> values) {
    Query query = entityManager.createNamedQuery(name.trim());
    if (cacheable) {
        query.setHint(QueryHints.HINT_CACHEABLE, "true");
        if (cacheRegion != null && (!cacheRegion.equals(""))) {
            query.setHint(QueryHints.HINT_CACHE_REGION, cacheRegion);
        }
    }
    if (values != null && values.size() > 0) {
        for (Entry<String, Object> e : values.entrySet()) {
            query.setParameter(e.getKey(), e.getValue());
        }
    }
    return query;
}
项目:EasyEE    文件:CommonDAOSpringImpl.java   
/**
 * CommonDAO 内部方法,Query
 * 
 * @param entityManager
 *            entityManager
 * @param jpql
 *            jqpl
 * @param cacheable
 *            cacheable
 * @param cacheRegion
 *            cacheRegion
 * @param values
 *            values
 * @return Query对象
 */
private Query createQuery(EntityManager entityManager, String jpql, boolean cacheable, String cacheRegion,
        Map<String, Object> values) {
    Query query = entityManager.createQuery(jpql);
    if (cacheable) {
        query.setHint(QueryHints.HINT_CACHEABLE, "true");
        if (cacheRegion != null && (!cacheRegion.equals(""))) {
            query.setHint(QueryHints.HINT_CACHE_REGION, cacheRegion);
        }
    }
    if (values != null && values.size() > 0) {
        for (Entry<String, Object> e : values.entrySet()) {
            query.setParameter(e.getKey(), e.getValue());
        }
    }
    return query;
}
项目:EasyEE    文件:CommonDAOSpringImpl.java   
/**
 * CommonDAO 内部方法,NamedQuery
 * 
 * @param entityManager
 *            entityManager
 * @param name
 *            name
 * @param cacheable
 *            cacheable
 * @param cacheRegion
 *            cacheRegion
 * @param values
 *            values
 * @return Query对象
 */
private Query createNamedQuery(EntityManager entityManager, String name, boolean cacheable, String cacheRegion,
        Map<String, Object> values) {
    Query query = entityManager.createNamedQuery(name.trim());
    if (cacheable) {
        query.setHint(QueryHints.HINT_CACHEABLE, "true");
        if (cacheRegion != null && (!cacheRegion.equals(""))) {
            query.setHint(QueryHints.HINT_CACHE_REGION, cacheRegion);
        }
    }
    if (values != null && values.size() > 0) {
        for (Entry<String, Object> e : values.entrySet()) {
            query.setParameter(e.getKey(), e.getValue());
        }
    }
    return query;
}
项目:EasyEE    文件:CommonDAOSpringImpl.java   
/**
 * CommonDAO 内部方法,Query
 * 
 * @param entityManager
 *            entityManager
 * @param jpql
 *            jqpl
 * @param cacheable
 *            cacheable
 * @param cacheRegion
 *            cacheRegion
 * @param values
 *            values
 * @return Query对象
 */
private Query createQuery(EntityManager entityManager, String jpql, boolean cacheable, String cacheRegion,
        Map<String, Object> values) {
    Query query = entityManager.createQuery(jpql);
    if (cacheable) {
        query.setHint(QueryHints.HINT_CACHEABLE, "true");
        if (cacheRegion != null && (!cacheRegion.equals(""))) {
            query.setHint(QueryHints.HINT_CACHE_REGION, cacheRegion);
        }
    }
    if (values != null && values.size() > 0) {
        for (Entry<String, Object> e : values.entrySet()) {
            query.setParameter(e.getKey(), e.getValue());
        }
    }
    return query;
}
项目:EasyEE    文件:CommonDAOSpringImpl.java   
/**
 * CommonDAO 内部方法,NamedQuery
 * 
 * @param entityManager
 *            entityManager
 * @param name
 *            name
 * @param cacheable
 *            cacheable
 * @param cacheRegion
 *            cacheRegion
 * @param values
 *            values
 * @return Query对象
 */
private Query createNamedQuery(EntityManager entityManager, String name, boolean cacheable, String cacheRegion,
        Map<String, Object> values) {
    Query query = entityManager.createNamedQuery(name.trim());
    if (cacheable) {
        query.setHint(QueryHints.HINT_CACHEABLE, "true");
        if (cacheRegion != null && (!cacheRegion.equals(""))) {
            query.setHint(QueryHints.HINT_CACHE_REGION, cacheRegion);
        }
    }
    if (values != null && values.size() > 0) {
        for (Entry<String, Object> e : values.entrySet()) {
            query.setParameter(e.getKey(), e.getValue());
        }
    }
    return query;
}
项目:EasyEE    文件:CommonDAOSpringImpl.java   
/**
 * CommonDAO 内部方法,Query
 * 
 * @param entityManager
 *            entityManager
 * @param jpql
 *            jqpl
 * @param cacheable
 *            cacheable
 * @param cacheRegion
 *            cacheRegion
 * @param values
 *            values
 * @return Query对象
 */
private Query createQuery(EntityManager entityManager, String jpql, boolean cacheable, String cacheRegion,
        Map<String, Object> values) {
    Query query = entityManager.createQuery(jpql);
    if (cacheable) {
        query.setHint(QueryHints.HINT_CACHEABLE, "true");
        if (cacheRegion != null && (!cacheRegion.equals(""))) {
            query.setHint(QueryHints.HINT_CACHE_REGION, cacheRegion);
        }
    }
    if (values != null && values.size() > 0) {
        for (Entry<String, Object> e : values.entrySet()) {
            query.setParameter(e.getKey(), e.getValue());
        }
    }
    return query;
}
项目:high-performance-java-persistence    文件:ReadOnlyQueryTest.java   
@Test
public void testReadOnly() {
    doInJPA(entityManager -> {
        List<Post> posts = entityManager.createQuery(
            "select p from Post p", Post.class)
        .setHint(QueryHints.HINT_READONLY, true)
        .getResultList();
    });
}
项目:high-performance-java-persistence    文件:QueryCacheTest.java   
public List<PostComment> getLatestPostComments(EntityManager entityManager) {
    return entityManager.createQuery(
        "select pc " +
        "from PostComment pc " +
        "order by pc.post.id desc", PostComment.class)
    .setMaxResults(10)
    .setHint(QueryHints.HINT_CACHEABLE, true)
    .getResultList();
}
项目:high-performance-java-persistence    文件:QueryCacheTest.java   
private List<PostComment> getLatestPostCommentsByPostId(EntityManager entityManager) {
    return entityManager.createQuery(
        "select pc " +
        "from PostComment pc " +
        "where pc.post.id = :postId", PostComment.class)
    .setParameter("postId", 1L)
    .setMaxResults(10)
    .setHint(QueryHints.HINT_CACHEABLE, true)
    .getResultList();
}
项目:high-performance-java-persistence    文件:QueryCacheTest.java   
private List<PostComment> getLatestPostCommentsByPost(EntityManager entityManager) {
    Post post = entityManager.find(Post.class, 1L);
    return entityManager.createQuery(
        "select pc " +
        "from PostComment pc " +
        "where pc.post = :post ", PostComment.class)
        .setParameter("post", post)
    .setMaxResults(10)
    .setHint(QueryHints.HINT_CACHEABLE, true)
    .getResultList();
}
项目:high-performance-java-persistence    文件:QueryCacheTest.java   
private List<PostCommentSummary> getPostCommentSummaryByPost(EntityManager entityManager) {
    return entityManager.createQuery(
        "select new com.vladmihalcea.book.hpjp.hibernate.cache.QueryCacheTest$PostCommentSummary(pc.id, p.title, pc.review) " +
        "from PostComment pc " +
        "left join pc.post p " +
        "where p.id = :postId ", PostCommentSummary.class)
        .setParameter("postId", 1L)
    .setMaxResults(10)
    .setHint(QueryHints.HINT_CACHEABLE, true)
    .getResultList();
}
项目:high-performance-java-persistence    文件:MySQLScrollableResultsStreamingTest.java   
private void stream(EntityManager entityManager) {
    final AtomicLong sum = new AtomicLong();
    try(Stream<Post> postStream = entityManager
            .createQuery("select p from Post p", Post.class)
            .setMaxResults(resultSetSize)
            .setHint(QueryHints.HINT_FETCH_SIZE, Integer.MIN_VALUE)
            .unwrap(Query.class)
            .stream()) {
        postStream.forEach(post -> sum.incrementAndGet());
    }
    assertEquals(resultSetSize, sum.get());
}
项目:high-performance-java-persistence    文件:PlanCacheSizePerformanceTest.java   
protected Query createEntityQuery1(EntityManager entityManager) {
    return entityManager.createQuery(
        "select new " +
        "   com.vladmihalcea.book.hpjp.hibernate.fetching.PostCommentSummary( " +
        "       p.id, p.title, c.review ) " +
        "from PostComment c " +
        "join c.post p")
    .setFirstResult(10)
    .setMaxResults(20)
    .setHint(QueryHints.HINT_FETCH_SIZE, 20);
}
项目:high-performance-java-persistence    文件:PlanCacheSizePerformanceTest.java   
protected Query createNativeQuery1(EntityManager entityManager) {
    return entityManager.createNativeQuery(
        "select p.id, p.title, c.review * " +
        "from post_comment c " +
        "join post p on p.id = c.post_id ")
    .setFirstResult(10)
    .setMaxResults(20)
    .setHint(QueryHints.HINT_FETCH_SIZE, 20);
}
项目:high-performance-java-persistence    文件:DistinctTest.java   
@Test
public void testWithDistinctAndQueryHint() {
    doInJPA(entityManager -> {
        List<Post> posts = entityManager.createQuery(
            "select distinct p " +
            "from Post p " +
            "left join fetch p.comments " +
            "where p.title = :title", Post.class)
        .setParameter("title", "High-Performance Java Persistence")
        .setHint(QueryHints.HINT_PASS_DISTINCT_THROUGH, false)
        .getResultList();

        LOGGER.info("Fetched {} post entities: {}", posts.size(), posts);
    });
}
项目:Spring-Data-JPA-Demo    文件:BaseRepositoryImpl.java   
@SuppressWarnings("unchecked")
@Override
public List<T> getAllUsingCache(Pageable page)
{
    Query query = entityManager.createQuery("from " + clazz.getName());
    query.setHint(QueryHints.HINT_CACHEABLE, true);
    if (page != null)
    {
        query.setFirstResult(page.getPageNumber()*page.getPageSize());
        query.setMaxResults(page.getPageSize());
    }
    return query.getResultList();
}
项目:Spring-3---JPA-2---Hibernate-4---c3p0---EHCache---log4j-Integration-Demo    文件:CountryDaoImpl.java   
@SuppressWarnings("unchecked")
@Override
public List<Country> getAll()
{
    Query query = entityManager.createQuery("from Country");
    //stores the query results in the second level cache (if enabled)
    query.setHint(QueryHints.HINT_CACHEABLE, true);
    return query.getResultList();
}