如何按集合值查询实体

时间:2012-05-22 19:30:43

标签: jpa collections criteria-api

我正在使用jpa,我有以下实体:

@Entity
@Table(name="favorites_folders")
public class FavoritesFolder {

     private static final long serialVersionUID = 1L;

     @Id
     private String id;

     @NotNull
     @Size(min = 1, max = 50)
     public String name;

     @ElementCollection(fetch = FetchType.LAZY)
     @CollectionTable(
        name="favorites_products",
        joinColumns=@JoinColumn(name="folder_id")
        )
     @Column(name="product_id")
     @NotNull
     private Set<String> productsIds = new HashSet<String>();
}

我想要做的是获取一组FavoritesFolder个实体,这些实体在productsIds成员集中包含字符串“favorite-id”。

有谁知道如何在条件api 中完成?

更新
我认为以下sql应该可以解决问题,但我不确定如何在JPQLCriteria API中执行此操作:

select * from favorites_folders join favorites_products on favorites_folders.id = favorites_products.folder_id where favorites_products.product_id = 'favorite-id'

3 个答案:

答案 0 :(得分:10)

要使用条件api在其productsIds成员集中获取一组包含字符串“favorite-id”的FavoritesFolder实体,您应该执行以下操作:

CriteriaBuilder cb = em.getCriteriaBuilder(); //em is EntityManager
CriteriaQuery<FavoritesFolder> cq = cb.createQuery(FavoritesFolder.class);
Root<FavoritesFolder> root = cq.from(FavoritesFolder.class);

Expression<Collection<String>> productIds = root.get("productsIds");
Predicate containsFavoritedProduct = cb.isMember("favorite-id", productIds);

cq.where(containsFavoritedProduct);

List<FavoritesFolder> favoritesFolders = em.createQuery(cq).getResultList();

有关Collections in JPQL and Criteria Queries的更多信息。

答案 1 :(得分:1)

使用IN

的另一种方式
@Entity
public class UserCategory implements Serializable {
private static final long    serialVersionUID    = 8261676013650495854L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@ElementCollection
private List<String> categoryName;


(...)
}

然后你可以写一个像

这样的Criteria查询
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<UserCategory> q = cb.createQuery(UserCategory.class);
    Root<UserCategory> root = q.from(UserCategory.class);

    Predicate predicate = cb.conjunction();
    Predicate p1 = cb.equal(root.get(UserCategory_.targetSiteType), siteType.getName());
    Predicate p2 = root.get(UserCategory_.categoryName).in(category);
    predicate = cb.and(p1,p2);

    q.where(predicate);

    TypedQuery<UserCategory> tq = entityManager.createQuery(q);
    List<UserCategory> all = tq.getResultList();

    if (all == null || all.size() == 0){
        return null;
    }else if (all.size() > 1){
        throw new Exception("Unexpected result - "+all.size());
    }else{
        return all.get(0);
    }

答案 2 :(得分:0)

这是我的工作。 我正在使用Springboot 1.5.9。我没有时间确定根本原因。我所知道的是,当通过Kotlin时,这种嵌套属性被忽略了。 所以我所做的解决方法是不使用由解析器创建的Sort对象。 这是pageable.sort中的代码。如果不这样做,nullPageRequest并且排序不起作用。我的代码将创建一个新的sort对象,该对象具有可用的非空 @RequestMapping("/searchAds", method = arrayOf(RequestMethod.POST)) fun searchAds( @RequestBody cmd: AdsSearchCommand, pageable: Pageable, resourceAssembler: PersistentEntityResourceAssembler, sort: String? = null ): ResponseEntity<PagedResources<Resource<Ads>>> { val page = adsService.searchAds(cmd, pageable.repairSortIfNeeded(sort)) resourceAssembler as ResourceAssembler<Ads, Resource<Ads>> return adsPagedResourcesAssembler.toResource(page, resourceAssembler).toResponseEntity() } fun Pageable.repairSortIfNeeded(sort: String?): Pageable { return if (sort.isNullOrEmpty() || this.sort != null) { this } else { sort as String val sa = sort.split(",") val direction = if (sa.size > 1) Sort.Direction.valueOf(sa[1]) else Sort.Direction.ASC val property = sa[0] PageRequest(this.pageNumber, this.pageSize, direction, property) } }

$qry=mysqli_query("INSERT INTO supplier `VALUES('sss','%$id_drugstore%',$_POST['name_suplier']','$_POST['county_supplier']','$_POST['county_supplier']','$_POST['town_supplier']','$_POST['street_supplier']','$_POST['bank_suplier']','$_POST['$no_cont_Bank']";`