2012-05-22 148 views
5

我使用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", 
     [email protected](name="folder_id") 
     ) 
    @Column(name="product_id") 
    @NotNull 
    private Set<String> productsIds = new HashSet<String>(); 
} 

我想要做的是讓一組包含字符串FavoritesFolder實體的「最愛-id「在他們的productsIds成員集中。

有沒有人知道如何在準則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' 

回答

8

要弄一套FavoritesFolder的在使用標準API的成員集中包含字符串「favorite-id」的實體您應該執行以下操作:

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(); 

更多信息o n Collections in JPQL and Criteria Queries

+1

我知道這個話題,我們reffering爲ID的,因此他們必須等於匹配,但假設我們有一個'Collection ',我們可以做基本相同的事情,但是不需要一個項目是'相等的',我們可以使用'LIKE'來匹配項目MATCH? – dominicbri7

+3

@ dominicbri7在相關[問題](http://stackoverflow.com/questions/7066122/how-to-make-a-like-query-to-elementcollection-of-type-map?rq=1)我發現解決方案。使用root類'cb.like(from.join(「apples」)。get(「color」),textParameter)上的join方法' – cirovladimir

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; 


(...) 
} 

然後,你可以寫一個標準查詢像

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); 
    }