com.querydsl.core.types.EntityPath Java Examples

The following examples show how to use com.querydsl.core.types.EntityPath. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BaseService.java    From ZTuoExchange_framework with MIT License 7 votes vote down vote up
/**
 * @param expressions        查询列表
 * @param entityPaths        查询表
 * @param predicates         条件
 * @param orderSpecifierList 排序
 * @param pageNo             页码
 * @param pageSize           页面大小
 */
@Transactional(readOnly = true)
public PageListMapResult queryDslForPageListResult(
        List<Expression> expressions,
        List<EntityPath> entityPaths,
        List<Predicate> predicates,
        List<OrderSpecifier> orderSpecifierList,
        Integer pageNo,
        Integer pageSize) {
    JPAQuery<Tuple> jpaQuery = queryFactory.select(expressions.toArray(new Expression[expressions.size()]))
            .from(entityPaths.toArray(new EntityPath[entityPaths.size()]))
            .where(predicates.toArray(new Predicate[predicates.size()]));
    List<Tuple> tuples = jpaQuery.orderBy(orderSpecifierList.toArray(new OrderSpecifier[orderSpecifierList.size()]))
            .offset((pageNo - 1) * pageSize).limit(pageSize)
            .fetch();
    List<Map<String, Object>> list = new LinkedList<>();//返回结果
    //封装结果
    for (int i = 0; i < tuples.size(); i++) {
        //遍历tuples
        Map<String, Object> map = new LinkedHashMap<>();//一条信息
        for (Expression expression : expressions) {
            map.put(expression.toString().split(" as ")[1],//别名作为Key
                    tuples.get(i).get(expression));//获取结果
        }
        list.add(map);
    }
    PageListMapResult pageListMapResult = new PageListMapResult(list, pageNo, pageSize, jpaQuery.fetchCount());//分页封装
    return pageListMapResult;
}
 
Example #2
Source File: CatalogItemFinder.java    From jeeshop with Apache License 2.0 6 votes vote down vote up
public <T extends CatalogItem> List<T> findVisibleCatalogItems(EntityPath<T> entityPath, List<T> items, String locale) {
    QCatalogItem qCatalogItem = new QCatalogItem(entityPath);
    Date now = new Date();
    List<T> results = new JPAQueryFactory(entityManager)
            .selectFrom(entityPath).where(
                    qCatalogItem.disabled.isFalse(),
                    qCatalogItem.endDate.after(now).or(qCatalogItem.endDate.isNull()),
                    qCatalogItem.startDate.before(now).or(qCatalogItem.startDate.isNull()),
                    qCatalogItem.in(items)
            )
            .fetch();

    results.forEach((catalogItem) -> catalogItem.setLocalizedPresentation(locale));

    return results;

}
 
Example #3
Source File: QuerydslKeyValueRepository.java    From spring-data-keyvalue with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new {@link QuerydslKeyValueRepository} for the given {@link EntityInformation},
 * {@link KeyValueOperations} and {@link EntityPathResolver}.
 *
 * @param entityInformation must not be {@literal null}.
 * @param operations must not be {@literal null}.
 * @param resolver must not be {@literal null}.
 */
public QuerydslKeyValueRepository(EntityInformation<T, ID> entityInformation, KeyValueOperations operations,
		EntityPathResolver resolver) {

	super(entityInformation, operations);

	Assert.notNull(resolver, "EntityPathResolver must not be null!");

	EntityPath<T> path = resolver.createPath(entityInformation.getJavaType());
	this.builder = new PathBuilder<>(path.getType(), path.getMetadata());
}
 
Example #4
Source File: BaseService.java    From ZTuoExchange_framework with MIT License 5 votes vote down vote up
/**
 * @param expressions        查询列表
 * @param entityPaths        查询表
 * @param predicates         条件
 * @param orderSpecifierList 排序
 * @param pageNo             页码
 * @param pageSize           页面大小
 */
@Transactional(readOnly = true)
public PageListMapResult queryDslForPageListResult(
        List<Expression> expressions,
        List<EntityPath> entityPaths,
        List<Predicate> predicates,
        List<OrderSpecifier> orderSpecifierList,
        Integer pageNo,
        Integer pageSize) {
    JPAQuery<Tuple> jpaQuery = queryFactory.select(expressions.toArray(new Expression[expressions.size()]))
            .from(entityPaths.toArray(new EntityPath[entityPaths.size()]))
            .where(predicates.toArray(new Predicate[predicates.size()]));
    List<Tuple> tuples = jpaQuery.orderBy(orderSpecifierList.toArray(new OrderSpecifier[orderSpecifierList.size()]))
            .offset((pageNo - 1) * pageSize).limit(pageSize)
            .fetch();
    List<Map<String, Object>> list = new LinkedList<>();//返回结果
    //封装结果
    for (int i = 0; i < tuples.size(); i++) {
        //遍历tuples
        Map<String, Object> map = new LinkedHashMap<>();//一条信息
        for (Expression expression : expressions) {
            map.put(expression.toString().split(" as ")[1],//别名作为Key
                    tuples.get(i).get(expression));//获取结果
        }
        list.add(map);
    }
    PageListMapResult pageListMapResult = new PageListMapResult(list, pageNo, pageSize, jpaQuery.fetchCount());//分页封装
    return pageListMapResult;
}
 
Example #5
Source File: CatalogItemFinder.java    From jeeshop with Apache License 2.0 5 votes vote down vote up
public Long countBySearchCriteria(EntityPath<? extends CatalogItem> entityPath, String searchCriteria) {
    QCatalogItem qCatalogItem = new QCatalogItem(entityPath);
    JPAQuery query = new JPAQueryFactory(entityManager)
            .selectFrom(qCatalogItem)
            .where(buildSearchPredicate(searchCriteria, qCatalogItem));
    return query.fetchCount();
}
 
Example #6
Source File: CatalogItemFinder.java    From jeeshop with Apache License 2.0 5 votes vote down vote up
public <T extends CatalogItem> List<T> findBySearchCriteria(EntityPath<T> entityPath, String searchCriteria,
                                                            Integer offset, Integer limit, String orderBy, Boolean isDesc) {
    QCatalogItem qCatalogItem = new QCatalogItem(entityPath);

    JPAQuery<T> query = new JPAQueryFactory(entityManager).selectFrom(entityPath)
            .where(buildSearchPredicate(searchCriteria, qCatalogItem));

    addOffsetAndLimitToQuery(offset, limit, query, orderBy, isDesc, qCatalogItem);

    return query.fetch();
}
 
Example #7
Source File: CatalogItemFinder.java    From jeeshop with Apache License 2.0 5 votes vote down vote up
public <T extends CatalogItem, P extends CatalogItem> List<P> findForeignHolder(EntityPath<P> hp,
                                                                                ListPath<T, ? extends SimpleExpression<T>> h, T c) {

    return new JPAQueryFactory(entityManager)
            .selectFrom(hp)
            .where(h.contains(c))
            .fetch();
}
 
Example #8
Source File: QuerydslUtils.java    From crnk-framework with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> EntityPath<T> getEntityPath(Class<T> entityClass) {
	Class<?> queryClass = getQueryClass(entityClass);
	try {
		String fieldName = firstToLower(entityClass.getSimpleName());
		Field field = queryClass.getField(fieldName);
		return (EntityPath<T>) field.get(entityClass);
	} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
		throw new IllegalStateException("failed to access query class " + queryClass.getName(), e);
	}
}
 
Example #9
Source File: QuerydslQueryBackend.java    From crnk-framework with Apache License 2.0 5 votes vote down vote up
public QuerydslQueryBackend(QuerydslQueryImpl<T> queryImpl, Class<T> clazz, MetaDataObject parentMeta,
		MetaAttribute parentAttr, boolean addParentSelection) {
	this.queryImpl = queryImpl;

	JPAQueryFactory queryFactory = queryImpl.getQueryFactory();

	if (parentMeta != null) {
		parentFrom = QuerydslUtils.getEntityPath(parentMeta.getImplementationClass());
		root = QuerydslUtils.getEntityPath(clazz);

		Path joinPath = (Path) QuerydslUtils.get(parentFrom, parentAttr.getName());
		joinHelper = new JoinRegistry<>(this, queryImpl);

		joinHelper.putJoin(new MetaAttributePath(), root);

		if (addParentSelection) {
			Expression<Object> parentIdExpr = getParentIdExpression(parentMeta, parentAttr);
			querydslQuery = queryFactory.select(parentIdExpr, root);
		}
		else {
			querydslQuery = queryFactory.select(root);
		}

		querydslQuery = querydslQuery.from(parentFrom);
		if (joinPath instanceof CollectionExpression) {
			querydslQuery = querydslQuery.join((CollectionExpression) joinPath, root);
		}
		else {
			querydslQuery = querydslQuery.join((EntityPath) joinPath, root);
		}
	}
	else {
		root = QuerydslUtils.getEntityPath(clazz);
		joinHelper = new JoinRegistry<>(this, queryImpl);
		joinHelper.putJoin(new MetaAttributePath(), root);
		querydslQuery = queryFactory.select(root);
		querydslQuery = querydslQuery.from((EntityPath) root);
	}
}
 
Example #10
Source File: QuerydslQueryBackend.java    From katharsis-framework with Apache License 2.0 5 votes vote down vote up
public QuerydslQueryBackend(QuerydslQueryImpl<T> queryImpl, Class<T> clazz, MetaDataObject parentMeta,
		MetaAttribute parentAttr, boolean addParentSelection) {
	this.queryImpl = queryImpl;

	JPAQueryFactory queryFactory = queryImpl.getQueryFactory();

	if (parentMeta != null) {
		parentFrom = QuerydslUtils.getEntityPath(parentMeta.getImplementationClass());
		root = QuerydslUtils.getEntityPath(clazz);

		Path joinPath = (Path) QuerydslUtils.get(parentFrom, parentAttr.getName());
		joinHelper = new JoinRegistry<>(this, queryImpl);

		joinHelper.putJoin(new MetaAttributePath(), root);

		if (addParentSelection) {
			Expression<Object> parentIdExpr = getParentIdExpression(parentMeta, parentAttr);
			querydslQuery = queryFactory.select(parentIdExpr, root);
		}
		else {
			querydslQuery = queryFactory.select(root);
		}

		querydslQuery = querydslQuery.from(parentFrom);
		if (joinPath instanceof CollectionExpression) {
			querydslQuery = querydslQuery.join((CollectionExpression) joinPath, root);
		}
		else {
			querydslQuery = querydslQuery.join((EntityPath) joinPath, root);
		}
	}
	else {
		root = QuerydslUtils.getEntityPath(clazz);
		joinHelper = new JoinRegistry<>(this, queryImpl);
		joinHelper.putJoin(new MetaAttributePath(), root);
		querydslQuery = queryFactory.select(root);
		querydslQuery = querydslQuery.from((EntityPath) root);
	}
}
 
Example #11
Source File: QuerydslUtils.java    From katharsis-framework with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> EntityPath<T> getEntityPath(Class<T> entityClass) {
	Class<?> queryClass = getQueryClass(entityClass);
	try {
		String fieldName = firstToLower(entityClass.getSimpleName());
		Field field = queryClass.getField(fieldName);
		return (EntityPath<T>) field.get(entityClass);
	}
	catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
		throw new IllegalStateException("failed to access query class " + queryClass.getName(), e);
	}
}
 
Example #12
Source File: QueryDslContext.java    From ZTuoExchange_framework with MIT License 4 votes vote down vote up
public EntityPath[] entityPathToArray() {
    return entityPaths.toArray(new EntityPath[entityPaths.size()]);
}
 
Example #13
Source File: CatalogItemFinder.java    From jeeshop with Apache License 2.0 4 votes vote down vote up
public Long countAll(EntityPath<? extends CatalogItem> entityPath) {
    QCatalogItem qCatalogItem = new QCatalogItem(entityPath);
    JPAQuery query = new JPAQueryFactory(entityManager).selectFrom(qCatalogItem);
    return query.fetchCount();
}
 
Example #14
Source File: QDataTablesRepositoryImpl.java    From spring-data-jpa-datatables with Apache License 2.0 4 votes vote down vote up
public QDataTablesRepositoryImpl(JpaEntityInformation<T, ID> entityInformation,
    EntityManager entityManager, EntityPathResolver resolver) {
  super(entityInformation, entityManager);
  EntityPath<T> path = resolver.createPath(entityInformation.getJavaType());
  this.builder = new PathBuilder<>(path.getType(), path.getMetadata());
}
 
Example #15
Source File: QuerydslQueryBackend.java    From katharsis-framework with Apache License 2.0 4 votes vote down vote up
@Override
public <E> EntityPath<E> getJoin(MetaAttributePath path) {
	return (EntityPath<E>) joinHelper.getOrCreateJoin(path);
}
 
Example #16
Source File: QuerydslQueryBackend.java    From katharsis-framework with Apache License 2.0 4 votes vote down vote up
@Override
public EntityPath getParentRoot() {
	return parentFrom;
}
 
Example #17
Source File: QuerydslQueryBackend.java    From crnk-framework with Apache License 2.0 4 votes vote down vote up
@Override
public <E> EntityPath<E> getJoin(MetaAttributePath path, JoinType defaultJoinType) {
	return (EntityPath<E>) joinHelper.getOrCreateJoin(path, defaultJoinType);
}
 
Example #18
Source File: QuerydslQueryBackend.java    From crnk-framework with Apache License 2.0 4 votes vote down vote up
@Override
public EntityPath getParentRoot() {
	return parentFrom;
}
 
Example #19
Source File: QueryDslContext.java    From ZTuoExchange_framework with MIT License 4 votes vote down vote up
public EntityPath[] entityPathToArray() {
    return entityPaths.toArray(new EntityPath[entityPaths.size()]);
}
 
Example #20
Source File: QueryDslContext.java    From ZTuoExchange_framework with MIT License 4 votes vote down vote up
public void add(EntityPath entityPath) {
    entityPaths.add(entityPath);
}
 
Example #21
Source File: QueryDslContext.java    From ZTuoExchange_framework with MIT License 4 votes vote down vote up
public void add(EntityPath entityPath) {
    entityPaths.add(entityPath);
}
 
Example #22
Source File: CatalogItemFinder.java    From jeeshop with Apache License 2.0 3 votes vote down vote up
public <T extends CatalogItem> List<T> findAll(EntityPath<T> entityPath, Integer offset, Integer limit, String orderBy, Boolean isDesc) {
    QCatalogItem qCatalogItem = new QCatalogItem(entityPath);

    JPAQuery<T> query = new JPAQueryFactory(entityManager).selectFrom(entityPath);

    addOffsetAndLimitToQuery(offset, limit, query, orderBy, isDesc, qCatalogItem);

    return query.fetch();
}
 
Example #23
Source File: QueryDSLRepository.java    From library with Apache License 2.0 2 votes vote down vote up
/**
 * Create a new root query using {@link #jpaQuery()}
 *
 * @param entityPath represents the entity type to be used on the FROM clause in the SQL query
 * @param <V> value type of this {@link EntityPath}
 * @return a new {@link JPAQuery} typed by the {@link EntityPath}
 */
default <V> JPAQuery<T> jpaQueryFor(EntityPath<V> entityPath) {
    return this.jpaQuery().from(entityPath);
}
 
Example #24
Source File: QuerydslTranslationContext.java    From katharsis-framework with Apache License 2.0 votes vote down vote up
public <E> EntityPath<E> getJoin(MetaAttributePath path); 
Example #25
Source File: QuerydslTranslationContext.java    From katharsis-framework with Apache License 2.0 votes vote down vote up
<P> EntityPath<P> getParentRoot(); 
Example #26
Source File: QuerydslTranslationContext.java    From crnk-framework with Apache License 2.0 votes vote down vote up
<E> EntityPath<E> getJoin(MetaAttributePath path, JoinType defaultJoinType); 
Example #27
Source File: QuerydslTranslationContext.java    From crnk-framework with Apache License 2.0 votes vote down vote up
<P> EntityPath<P> getParentRoot();