在JPA/JPQL中过滤集合,可以使用Criteria API或JPQL查询。下面是两种方法的示例:
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.persistence.EntityManager;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.CriteriaDelete;
EntityManager entityManager = ...; // 获取EntityManager实例
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<YourEntity> cq = cb.createQuery(YourEntity.class);
Root<YourEntity> root = cq.from(YourEntity.class);
// 添加过滤条件
Predicate predicate = cb.equal(root.get("propertyName"), "value");
cq.where(predicate);
// 执行查询
List<YourEntity> resultList = entityManager.createQuery(cq).getResultList();
import javax.persistence.EntityManager;
import javax.persistence.Query;
EntityManager entityManager = ...; // 获取EntityManager实例
String jpql = "SELECT e FROM YourEntity e WHERE e.propertyName = :value";
Query query = entityManager.createQuery(jpql);
query.setParameter("value", "value");
// 执行查询
List<YourEntity> resultList = query.getResultList();
在这两个示例中,请将YourEntity
替换为实际的实体类名,将propertyName
替换为实际的属性名,将value
替换为实际的过滤值。
推荐的腾讯云相关产品:
推荐的产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云