2016-12-10 10 views
0

SpringData/QueryDSLベースリポジトリを構築しています。私は以下のクラスを持っています:SpringData/QueryDSLにクエリヒントを適用する方法findAll

@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は、必要に応じて正常に動作しています。クエリヒントを適用することができるようにfindAll方法のJPAQueryへの参照を取得するために

//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 

それは可能です:しかし、私のようなEntityGraphのものを使用してクエリヒントを追加したいですか?

findAllにクエリヒントを適用するにはどうすればよいですか?

JPDAエンジンとしてQueryDSL v3.6.5とSpringData v1.8.2をHibernate v4.3.11.Finalで使用しています。

ご協力いただきありがとうございます。

ありがとうございます。 マリオ

答えて

1

はい可能です。あなたがあなた自身の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インスタンスを持っているので、任意のオプションとヒントを設定できます。

+0

ご返信ありがとうございます。私はQパスを通過していないので、あなたの解決策を微調整するだけで十分です。それにもかかわらず、解決策は私が探していたものであり、うまくいきました。どうもありがとう。 –

関連する問題