下面列出了怎么用javax.persistence.QueryHint的API类实例代码及写法,或者点击链接到github查看源代码。
private static void buildQueryHints(List<Element> elements, AnnotationDescriptor ann){
List<QueryHint> queryHints = new ArrayList<>( elements.size() );
for ( Element hint : elements ) {
AnnotationDescriptor hintDescriptor = new AnnotationDescriptor( QueryHint.class );
String value = hint.attributeValue( "name" );
if ( value == null ) {
throw new AnnotationException( "<hint> without name. " + SCHEMA_VALIDATION );
}
hintDescriptor.setValue( "name", value );
value = hint.attributeValue( "value" );
if ( value == null ) {
throw new AnnotationException( "<hint> without value. " + SCHEMA_VALIDATION );
}
hintDescriptor.setValue( "value", value );
queryHints.add( AnnotationFactory.create( hintDescriptor ) );
}
ann.setValue( "hints", queryHints.toArray( new QueryHint[queryHints.size()] ) );
}
public XAnnotation<javax.persistence.NamedQuery> createNamedQuery(
NamedQuery source) {
return source == null ? null :
//
new XAnnotation<javax.persistence.NamedQuery>(
javax.persistence.NamedQuery.class,
//
AnnotationUtils.create("query", source.getQuery()),
//
AnnotationUtils.create("hints",
createQueryHint(source.getHint()),
QueryHint.class),
//
AnnotationUtils.create("name", source.getName()),
AnnotationUtils.create("lockMode",
createLockMode(source.getLockMode()))
//
);
}
@Query("select q from Question q where q.id in :questionIds")
@QueryHints(value = { @QueryHint(name = org.eclipse.persistence.config.QueryHints.BATCH_TYPE, value = "IN"),
@QueryHint(name = org.eclipse.persistence.config.QueryHints.BATCH, value = "q.questionOptions"),
@QueryHint(name = org.eclipse.persistence.config.QueryHints.BATCH, value = "q.subquestions"),
@QueryHint(name = org.eclipse.persistence.config.QueryHints.BATCH, value = "q.translations"),
@QueryHint(name = org.eclipse.persistence.config.QueryHints.BATCH, value = "questionOptions.translations"),
@QueryHint(name = org.eclipse.persistence.config.QueryHints.BATCH, value = "subquestions.translations"),
@QueryHint(name = org.eclipse.persistence.config.QueryHints.BATCH, value = "subquestions.questionOptions"), }, forCounting = false)
List<Question> findInList(@Param("questionIds")
List<Integer> questionIds);
public Query applyRestrictions(Query query)
{
Parameters params = getParams();
Method method = getMethod();
if (params.hasSizeRestriction())
{
query.setMaxResults(params.getSizeRestriciton());
}
if (params.hasFirstResult())
{
query.setFirstResult(params.getFirstResult());
}
LockModeType lockMode = extractLockMode();
if (lockMode != null)
{
query.setLockMode(lockMode);
}
QueryHint[] hints = extractQueryHints();
if (hints != null)
{
for (QueryHint hint : hints)
{
query.setHint(hint.name(), hint.value());
}
}
applyEntityGraph(query, method);
query = applyJpaQueryPostProcessors(query);
return query;
}
private QueryHint[] extractQueryHints()
{
org.apache.deltaspike.data.api.Query query = getRepositoryMethodMetadata().getQuery();
if (query != null && query.hints().length > 0)
{
return query.hints();
}
return null;
}
@QueryHints({@QueryHint(name = "org.hibernate.cacheable", value = "true")})
List<Car> findAll();
@QueryHints(@QueryHint(name = CACHEABLE, value = "true"))
Optional<T> findById(ID id);
@QueryHints(@QueryHint(name = CACHEABLE, value = "true"))
boolean existsById(ID id);
@QueryHints(@QueryHint(name = CACHEABLE, value = "true"))
List<T> findAll();
@QueryHints(@QueryHint(name = CACHEABLE, value = "true"))
List<T> findAllById(Iterable<ID> ids);
@QueryHints(@QueryHint(name = CACHEABLE, value = "true"))
<S extends T> List<S> findAll(Example<S> example);
@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value ="true") })
public List<Dictionary> findByType(int type);
@Query("SELECT distinct p FROM Post p LEFT JOIN FETCH p.comments JOIN FETCH p.details d where d.createdBy = :user")
@QueryHints(@QueryHint(name = org.hibernate.jpa.QueryHints.HINT_PASS_DISTINCT_THROUGH, value = "false"))
List<Post> findByDetailsCreatedBy(@Param("user") String userName);
@Query("SELECT distinct p FROM Post p LEFT JOIN FETCH p.tags pt LEFT JOIN FETCH pt.tag JOIN p.details where p in :posts")
@QueryHints(@QueryHint(name = org.hibernate.jpa.QueryHints.HINT_PASS_DISTINCT_THROUGH, value = "false"))
List<Post> findPostsWithAllDetails(@Param("posts") List<Post> postList);
@Query("from RunningExecutionPlan r where r.flowUUID = :flowUUID")
@QueryHints({@QueryHint(name = "org.hibernate.cacheable", value = "true")})
public List<RunningExecutionPlan> findByUuidCached(@Param("flowUUID") String flowUUID);
@Query(hints = {
@QueryHint(name = "openjpa.hint.OptimizeResultCount", value = "some.invalid.value"),
@QueryHint(name = "org.hibernate.comment", value = "I'm a little comment short and stout")
})
Simple findBy(Long id);
/**
* 获取组织架构层级关系(用于更新组织架构层级关系)
*
* @param tenantId
* @return
*/
@QueryHints({@QueryHint(name = "org.hibernate.cacheable", value = "true")})
@Query("select s.fdId, p.fdId, p.fdHierarchyId, p.fdParentOrg.fdId, p.fdOrgType, p.fdTreeLevel from SysOrgElement s inner join s.fdParent p where s.fdOrgType > 1 and s.fdOrgType < 16 and s.fdIsAvailable = true and s.fdTenantId = :tenantId and (p.fdOrgType = 1 and (s.fdParentOrg is null or s.fdParentOrg <> p) or p.fdOrgType > 1 and (s.fdParentOrg is null and p.fdParentOrg is not null or s.fdParentOrg is not null and p.fdParentOrg is null or s.fdParentOrg <> p.fdParentOrg) or s.fdHierarchyId <> concat(concat(p.fdHierarchyId, s.fdId), 'x'))")
List<Object[]> findElementRelation(@Param("tenantId") int tenantId);
/**
* 获取群组关联关系
*
* @param tenantId
* @return
*/
@QueryHints({@QueryHint(name = "org.hibernate.cacheable", value = "true")})
@Query(value = "select fd_group_id, fd_element_id from sys_org_group_element left join sys_org_element on fd_element_id = fd_id where fd_org_type = 16 and fd_tenant_id = :tenantId", nativeQuery = true)
List<String[]> loadManyToMany(@Param("tenantId") int tenantId);
/**
* Retrieve users by their lastname. The finder {@literal User.findByLastname} is declared in
* {@literal META-INF/orm.xml} .
*
* @param lastname
* @return all users with the given lastname
*/
@QueryHints({@QueryHint(name = "foo", value = "bar")})
List<User> findByLastname(String lastname);