通过 JPA 规范加载 Blaze-persistence 实体视图

Loading Blaze-persistence entity view by JPA specification

我有一个为特定实体准备的“vanilla”JPA 规范。我还为同一个实体准备了 Blase-persistence 实体视图界面。如何使用准备好的 JPA 规范加载该实体视图?

编辑: 目前,我正在这样使用这个规范:

CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<EntityClass> query = cb.createQuery(EntityClass.class);
Root<EntityClass> root = query.from(EntityClass.class);
query.where(specification.toPredicate(root, query, cb));
EntityGraph graph = entityManager.getEntityGraph(entityGraphName);
TypedQuery<EntityClass> = entityManager.createQuery(query)
.setHint("javax.persistence.loadgraph", graph);
List<EntityClass> result = typedQuery.getResultList();

之后,我将实体列表转换为投影列表。 现在,我正在尝试在类似场景中用实体视图替换实体图和投影,实体视图的 class 作为参数。

您可以将 CriteriaQuery 或实际上 BlazeCriteriaQuery 转换为 Blaze-Persistence CriteriaBuilder,如 quickstart 中所述,大致如下所示:

// This is a subclass of the JPA CriteriaBuilder interface
BlazeCriteriaBuilder cb = BlazeCriteria.get(criteriaBuilderFactory);
// A subclass of the JPA CriteriaQuery interface
BlazeCriteriaQuery<EntityClass> query = cb.createQuery(EntityClass.class);
Root<EntityClass> root = query.from(EntityClass.class);
query.where(specification.toPredicate(root, query, cb));
// Finally, transform the BlazeCriteriaQuery to the Blaze-Persistence Core CriteriaBuilder
CriteriaBuilder<EntityClass> builder = query.createCriteriaBuilder(entityManager);

此时,您可以对该查询应用 EntityViewSetting 并检索 entity-view quickstart 中概述的结果:

CriteriaBuilder<MyEntityView> viewBuilder = evm.applySetting(EntityViewSetting.create(MyEntityView.class), builder );
List<MyEntityView> myViews = viewBuilder .getResultList();