【问题标题】:Select a row based on List in column根据列中的列表选择一行
【发布时间】:2021-03-25 11:14:11
【问题描述】:

我需要选择如下行

ID   | Name | Course
------------------------------------------------
01   | ABC  | ['Science','Maths','English']
02   | DEF  | ['Social', 'French', 'Computer']

案例 1-> 我需要根据下面的列表获取行 我的清单[科学、Java、.Net]

结果应该是 -->

 01   | ABC  | ['Science','Maths','English']

因为在 MyList 我有共同的科学

【问题讨论】:

  • 分享你的实体类
  • @krishnkantjaiswal 感谢您的解决方案,实际上我正在寻找 namedQuery,例如 Select o from Student o where o.name='ABC' and o.course=:courseList where my courseList is a list ['Science ','数学','英语']

标签: java sql hibernate spring-data-jpa


【解决方案1】:

试试这个:

    public static Specification<Student> filterPostByTagIdList(List<String> courses) {
        if (courses == null) {
            return null;
        }
        return ((root, criteriaQuery, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<>();
            for(String course : courses){
                predicates.add(criteriaBuilder.equal(root.join("courses").get("name"), course));
            }
            return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
        });
    }
@Repository
public interface StudentRepository extends JpaRepository<Student, Long>, JpaSpecificationExecutor<Student> {
}
List<String> courses = ...;
@Autowired
StudentRepository studentRepo;

List<Student> students = studentRepo.findAll(filterPostByTagIdList(courses));

【讨论】:

  • 感谢您的解决方案,实际上我正在寻找 namedQuery,例如 Select o from Student o where o.name='ABC' and o.course=:courseList where my courseList is a list ['Science', '数学','英语']
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-30
相关资源
最近更新 更多