【发布时间】:2019-06-17 15:58:19
【问题描述】:
我有一个查询,它已经写在扩展 JpaRepository 的存储库中。但我需要在这里添加逻辑运算符。
select ac from Accounts ac where ac.userId = :userId and ac.accountId = :accountID
当客户端提供 userId 或 accountId 时,要求获取帐户详细信息。
public interface GoalPlanRepository extends JpaRepository<GoalPlan, String>{
@Query("select ac from Accounts ac where ac.userId = :accountID ")
public List<Accounts> getCalculateRecurringAmount(@Param("accountID") String accountID, @Param("userId") String userId);
这是我的存储库。我在goalPlanDaoImplementation中实现这个方法
public List<Accounts> getCalculateRecurringAmount(String accountID) {
// CriteriaBuilder cb = em.getCriteriaBuilder();
// CriteriaQuery<Accounts> cq = cb.createQuery(Accounts.class);
//
// Root<Accounts> acc = cq.from(Accounts.class);
//
// Predicate accountIDPredicate = null;
// Predicate userIdPredicate = null;
//
// if(accountID != null) {
// accountIDPredicate = cb.equal(acc.get("accountID"), accountID);
// }
//
// if(userId != null) {
// userIdPredicate = cb.equal(acc.get("userId"), userId);
// }
//
//
// cq.where(accountIDPredicate, userIdPredicate);
//
// TypedQuery<Accounts> query = em.createQuery(cq);
List<Accounts> goalPlan = null;
goalPlan = goalPlanRepository.getCalculateRecurringAmount(accountID);
return goalPlan;
//return query.getResultList();
}
评论部分是我尝试做的。提前致谢。 :)
【问题讨论】:
标签: java jpa spring-data-jpa criteria-api