java - Hibernate Criteria API - adding a criterion: string should be in collection -
i have following entity object
@entity public class foobar { ... private list<string> uuids; ... }
now i'd make criteria query fetch foobar pojos uuids list contains string "abc123", i'm not sure how make appropriate criterion.
i assume using version of hibernate implements jpa 2.0. here's jpa 2.0 solution should work compliant implementation.
please annotate uuids
jpa's @elementcollection
annotation. don't use hibernate's @collectionofelements
mentioned in of other answer comments. latter has equivalent functionality being deprecated.
foobar.java
approximately this:
@entity public class foobar implements serializable { // might have other id @id private long id; @elementcollection private list<string> uuids; // getters/setters, serialversionuid, ... }
here's how can build criteriaquery
select foobar
s uuids
contain "abc123".
public void getfoobars() { { entitymanager em = ... // em injection, entitymanagerfactory, whatever criteriabuilder b = em.getcriteriabuilder(); criteriaquery<foobar> cq = b.createquery(foobar.class); root<foobar> foobar = cq.from(foobar.class); typedquery<foobar> q = em.createquery( cq.select(foobar) .where(b.ismember("abc123", foobar.<list<string>>get("uuids")))); (foobar f : q.getresultlist()) { // stuff f, have "abc123" in uuids } }
i made self-contained proof-of-concept program while playing this. can't push out right now. please comment if want poc pushed github.
Comments
Post a Comment