使用JPA规范接口实现查询

时间:2018-10-23 15:08:02

标签: java spring spring-boot jpa

我有这个JPA查询,用于选择带有规范接口的表。

    public List<PaymentTransactions> findAll(Specification<PaymentTransactions> spec) {
        String hql = "select e from " + PaymentTransactions.class.getName() + " e";
        TypedQuery<PaymentTransactions> query = entityManager.createQuery(hql, PaymentTransactions.class);
        List<PaymentTransactions> paymentTransactions = query.getResultList();
        return paymentTransactions;
    }

使用上述查询从接口Specification中仅选择阴道环的合适方法是什么?

org.springframework.data.jpa.domain interface public interface Specification<T> extends Serializable 

1 个答案:

答案 0 :(得分:0)

我不确定我是否正确,但是您可以尝试这种方式

1)使用必需的参数创建其他构造函数

package com.foo.bar;

public class PaymentTransactions {
    // column mapping and other properties are omitted
    private String requiredProp;
    private String optionalProp;

    public PaymentTransactions() {} // default no-arg constructor

    public PaymentTransactions(String required) {
       this.requiredProp = required;
    } 
}

2)像这样修改HQL查询

select new com.foo.bar.PaymentTransactions(e.requiredProp) from PaymentTransactions e