java - In a optional OneToOne join scenario, how to get entity A that does not have a B? -
i have class a, has optional link instance of b class.
for sake of user-friendliness, want filter output of grid displaying instances of class, switch allowing display of as, b or without b.
my abstract dao taking care of process dynamically building criteria queries according user desire sorting/filtering.
works great 2 first scenarios, however, last 1 returns empty list.
looking @ generated sql code, i've got (stripped down on purpose):
select t0.id id [...] person t0, user_profile t1 t1.person_id = t0.id , t1.people_id null;
i see logic behind this. however, never work last case. wonder how around use case ? ?
the criteria built programmatically, here how if built manually:
final criteriabuilder cb = getentitymanager().getcriteriabuilder(); criteriaquery<t> cq = getcontextquery(context); // juste create base criteria root<t> root = findroot(cq); // retrieve root. path p = root.join(part, jointype.left); // 'part' entity join on, got metamodel. cq.where(cb.isnull(p)); return getentitymanager().createquery(cq).getresultlist();
what entities (stripped down meaningful part):
@entity @table(name = "person") @getter @setter public class person extends abstractentity { @id @generatedvalue(strategy = generationtype.identity) @column(name = "id") protected integer id; @xmltransient @onetoone(optional = true, mappedby = "person") private userprofile user; } @entity @table(name = "user_profile") public class userprofile extends abstractentity implements authenticated { @id @generatedvalue(strategy = generationtype.identity) protected integer id; @onetoone(optional = false) @joincolumn(name = "person_id", referencedcolumnname = "id") @getter @setter private person person; }
using entities asumming = userprofile , b = person
i want filter output of grid displaying instances of class, switch allowing display of as,
criteriabuilder cb = ... criteriaquery<userprofile> q = cb.createquery(userprofile .class); root<userprofile> userprofile= q.from(userprofile.class); q.fetch(userprofile.get("person"), jointype.left); q.select(userprofile)
only b
criteriabuilder cb = ... criteriaquery<userprofile> q = cb.createquery(userprofile .class); root<userprofile> userprofile= q.from(userprofile .class); q.select(userprofile) q.where(cb.isnotempty(userprofile.get("person"));
or
criteriabuilder cb = ... criteriaquery<userprofile> q = cb.createquery(userprofile .class); root<userprofile> userprofile= q.from(userprofile .class); join<userprofile, person> person = userprofile.join("person", jointype.inner); q.select(userprofile)
or without b
criteriabuilder cb = ... criteriaquery<userprofile> q = cb.createquery(userprofile .class); root<userprofile> userprofile= q.from(userprofile .class); q.select(userprofile) q.where(cb.isempty(userprofile.get("person"));
or
criteriabuilder cb = ... criteriaquery<userprofile> q = cb.createquery(userprofile .class); root<userprofile> userprofile= q.from(userprofile .class); join<userprofile, person> person = userprofile.join("person", jointype.left); q.select(userprofile)
and
return getentitymanager().createquery(q).getresultlist();
that should work.
Comments
Post a Comment