如何将查询提示应用于SpringData / QueryDSL findAll

时间:2016-12-10 11:33:07

标签: hibernate jpa spring-data-jpa querydsl

我正在构建一个SpringData / QueryDSL基础存储库。我有以下几组课程:

@Entity
@Table(name="t_ows_jo")         
@NamedEntityGraphs({
    @NamedEntityGraph(
        name="graph.Jo",                                            
        attributeNodes={
            ...
        },
        subgraphs={
            ...
        }
    )
})
public class Jo  {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id")
    protected Long id;

    private String  joNo;
    private String  vin;
    private Date    dateFiled;
    private Integer mileage;

    private Vehicle vehicle;
    private Dealer  dealer;
    private List<JoJobRequest>  jobRequest;
    private List<JoJobDone>     jobDone;

    ...
}

@NoRepositoryBean
public interface BaseRepository<Entity, ID extends Serializable> extends JpaRepository<Entity, ID>, QueryDslPredicateExecutor<Entity> {
    Entity retrieve(Map<String, Object> filters);

    ... more custom methods here


    List<Entity> listAll(Map<String, Object> qryParam, int pageStart, int pageSize, Sort sortOrder);
}

public class BaseRepositoryImpl<Entity, ID extends Serializable> extends QueryDslJpaRepository<Entity, ID> 
    implements  BaseRepository<Entity, ID> {

    @PersistenceContext
    private EntityManager entityManager;


    //...   more codes here


    @Override
    public List<Entity> listAll(Map<String, Object> qryParam, int pageStart, int pageSize, Sort sortOrder) {
        Page<Entity> entities = null;

        Pageable pageReq = new PageRequest(pageStart, pageSize, sortOrder); 

        BooleanBuilder where = new BooleanBuilder();

        //build where here using qryParam

        ... some more codes here

        //get a reference to JPAQuery and apply query hints
        //EntityGraph graph = getEm().getEntityGraph("graph.Jo");
        //query.setHint("javax.persistence.fetchgraph",graph);

        entities = findAll(where.getValue(), pageRequest); //<<-----<<<< I want to apply query hints here

        return entities.getContent();
    }   
}

上面的BaseRepositoryImpl可以正常工作。但是,我想使用EntityGraph添加查询提示,例如:

//get a reference to JPAQuery and apply query hints
EntityGraph graph = getEntityManager().getEntityGraph("graph.Jo");
query.setHint("javax.persistence.fetchgraph",graph);

entities = findAll(where.getValue(), pageRequest); //<<-----<<<< I want to apply query hints here

是否可以获得对findAll方法的JPAQuery的引用,以便可以应用查询提示?

如何将查询提示应用于findAll

我正在使用QueryDSL v3.6.5和SpringData v1.8.2以及Hibernate v4.3.11.Final作为JPA引擎。

非常感谢任何帮助/建议。

提前致谢。 马里奥

1 个答案:

答案 0 :(得分:2)

是的,这是可能的。你可以编写自己的findAll(),这很简单:

@PersistenceContext
private EntityManager em;

public <T, Q extends EntityPathBase<T>> Page<T> findAll(Q path, Predicate predicate, Pageable pageable, EntityGraph fetchGraph) {
    Querydsl querydsl = new Querydsl(em, new PathBuilder<>(path.getType(), path.getMetadata()));
    JPAQuery countQuery = new JPAQuery(em).from(path).where(predicate);
    long total = countQuery.count();

    JPAQuery query = new JPAQuery(em).from(path).where(predicate);
    if (fetchGraph != null) {
        query.setHint(QueryHints.FETCHGRAPH, fetchGraph);
    }
    if ((pageable == null) || (total > pageable.getOffset())) {
        return new PageImpl<>(querydsl.applyPagination(pageable, query).list(path), pageable, total);
    } else {
        return new PageImpl<>(Collections.emptyList(), pageable, total);
    }
}

路径 - 您查询的实体生成的Q对应项。例如。 QJo.jo。

如果你有JPAQuery实例,你可以设置你想要的任何选项和提示。