Just answering myself, yes it's possible

Suppose

@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;


(...)
}

Then you can write a Criteria query like

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



[]

Leo


On Thu, Nov 28, 2013 at 3:53 PM, Leonardo K. Shikida <shik...@gmail.com>wrote:

> Hi
>
> is it possible to use Criteria to query by an @ElementCollection element
> using OpenJPA? (I guess it's not possible with std JPA)
>
> []
>
> Leo
>

Reply via email to