Spring Data JPA规范选择特定列

17
我们可以通过在Repository接口中编写自定义的@Query方法来选择特定的列。但是,我不想为不同属性编写这么多方法。
我尝试过这个方法,但它每次都返回整个对象。
public class MySpecifications {

    public static Specification<MyInfo> propertiesWithId(final String[] properties, final Object id, final String idProperty)
    {

        return new Specification<MyInfo>() {

            @Override
            public Predicate toPredicate(Root<MyInfo> root,
                    CriteriaQuery<?> query, CriteriaBuilder cb) {

                query = cb.createTupleQuery(); //tried cb.createQuery(MyInfo.class); as well

                List<Selection<? extends Object>> selectionList = new ArrayList<Selection<? extends Object>>();

                for (String property : properties) {

                    Selection<? extends Object> selection = root.get(property);

                    selectionList.add(selection);
                }

                return query.multiselect(selectionList).where(cb.equal(root.get(idProperty), id)).getRestriction();
            }

        };
    }
}

用途:

MyInfo findOne(Specification(properties,idValue, idProperty));

这是正确的方法吗?错误在哪里?


你考虑过使用投影吗?https://www.baeldung.com/spring-data-jpa-projections - marcellorvalle
4个回答

6

2

如果我说错了,请纠正我。

通常 Speicification(规范)位于一个 XxxSpecifications 类中,并具有静态方法。它们通常用于以下两种方式:

public class SwVecSpecifications {

    // Method 1. 
    public static Specification<WhiteVecEntity> conditions(String groupId, String appId, String typeId) {
        return (Specification<WhiteVecEntity>) (root, query, cb) -> {
            Predicate p3 = cb.equal(root.get("groupId"), groupId);
            Predicate p2 = cb.equal(root.get("appId"), appId);
            Predicate condition = cb.and(p2,p3);
            if (typeId != null && !typeId.isEmpty()) {
                Predicate p1 = cb.equal(root.get("typeId"), typeId);
                condition = cb.and(condition, p1);
            }
            return query.select(root.get("id")).where(condition).getRestriction();
        };
    }

    // Method 2.
    static void findByConditions(EntityManager em) {
        String groupId = "";
        String typeId = "";

        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<SuspectVecEntity> query = cb.createQuery(SuspectVecEntity.class);
        Root<SuspectVecEntity> root = query.from(SuspectVecEntity.class);

        Predicate p1 = cb.equal(root.get("groupId"), groupId);
        Predicate p2 = cb.equal(root.get("appId"), appId);
        Predicate condition = cb.and(p2,p3);
        if (typeId != null && !typeId.isEmpty()) {
            Predicate p1 = cb.equal(root.get("typeId"), typeId);
            condition = cb.and(condition, p1);
        }
        CriteriaQuery<SuspectVecEntity> cq = query.select(root.get("id")).where(condition);
        List<SuspectVecEntity> resultList = em.createQuery(cq).getResultList(); // resultList has Entity that only contains id
    }
}

方法1的代码库:

public interface SuspectVecRepository extends JpaRepository<SuspectVecEntity, Long>, JpaSpecificationExecutor<SuspectVecEntity>  {
     List<SuspectVecEntity> findAll(Specification specs);
}

服务:

@Autowired
EntityManager em;
void foo() {
     // Method 1:
     List<SuspectVecEntity> r1 = findAll(SwVecSpecifications.conditions());
     // Method 2:
     List<SuspectVecEntity> r2 = SwVecSpecifications.findByConditions(em); 
}

这两种方法的区别在于:
方法1,数据库查询返回整个列。
原因是存储库 findAll 接受一个 Specification。该 Specification 作为 restriction 返回,因此查询 Entity 中的所有列。它有一个 select 来指定列,该列位于 selection 而不是 restriction 中。更多信息请参见类 QueryStructure
方法2,数据库查询仅返回所选列。
看起来像是返回一个 Entity,这是因为它是一个链接泛型类型调用,并且该泛型类型是 Entity

1
规格是对where子句的抽象。由于JPA标准API的设计,您可以在规范中使用各种各样的内容,但除了声明where子句之外的任何副作用行为都是未定义的。
如果您想控制选择列表,可以使用投影和非常有限的查询支持进行查询派生,或在自定义方法中构造完整的自定义查询。

1

我试过这个方法,但它总是返回整个对象。

此方法返回与给定规格匹配的单个实体。请在此处检查。

根据我的理解,这是正确的方式。您可以像平常一样访问实体的属性(例如MyInfo.getIdProperty())。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接