我的实体类
@Data
@Entity
@Table(name = "pl_transactions")
public class PlTransactions{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Version
@Column(name = "version")
private Long version;
@Column(name = "id", insertable = false, updatable = false)
private Integer _id;
@Column(name = "version", insertable = false, updatable = false)
private Long _version;
@Column(name = "no_ref_no")
private String noRefNo;
}
我的主存储库
@RepositoryRestResource(collectionResourceRel = "pl-transactions", path = "pl-transactions")
public interface PlTransactionsRepository extends PagingAndSortingRepository<PlTransactions, Integer>, JpaSpecificationExecutor<PlTransactions>,PlTransactionsRepositoryCustom {
}
我的自定义存储库
@Repository
public class PlTransactionsRepositoryCustomImpl implements PlTransactionsRepositoryCustom{
@Autowired
@PersistenceContext
EntityManager entityManager;
@Override
public PlTransactionReport getTrxAmtOnly(Specification spec){
PlTransactionReport plTransactionReport = new PlTransactionReport();
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<BigDecimal> query = builder.createQuery(BigDecimal.class);
Root<PlTransactions> root = this.applySpecificationToCriteria(spec,query);
query.select(builder.sum(root.get("trxAmt").as(BigDecimal.class)));
TypedQuery<BigDecimal> typedQuery = entityManager.createQuery(query);
typedQuery.getSingleResult();
return plTransactionReport;
}
private Root<PlTransactions> applySpecificationToCriteria(Specification spec, CriteriaQuery<BigDecimal> query){
Root<PlTransactions> root = query.from(PlTransactions.class);
if(spec == null){
return root;
}
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
Predicate predicate = spec.toPredicate(root,query,builder);
if(predicate != null){
query.where(predicate);
}
return root;
}
}
我在网上搜索了一下,发现我必须使用persistance.xml注册它,但我手动尝试了一下,但仍然出现了这个错误,我尝试使用intellij生成它,但我的intellij idea marketplace中不存在jpa persistence的插件。请参考我使用的Intellij Idea 2020.3.4(社区版)
1条答案
按热度按时间lndjwyie1#
我认为您的存储库中缺少“JpaSpecificationExecutor”。
另外,请查看这个链接,获取示例代码-〉https://wesem.org/spring-data-jpa-specifications #:~:text= SPring%20Data%20Jpa%20规范帮助,类和创建查询。