java - JPA 2.0 CriteriaBuilder : SELECT FROM … IN -
i want create following sql query using jpa 2.0 criteriabuilder :
select * profil profilid in (select profilid roleprofil roleid = roleid)
here classes :
profil :
public class profil extends abstractdomain<long> { @id @column @generatedvalue(strategy = generationtype.sequence, generator = "idgen_x") @uiinfo(name = "identifiant") private long profilid; @column(nullable = false) @uiinfo(name = "libellé") private string lib; }
role :
public class role extends abstractdomain<long> { @id @column @generatedvalue(strategy = generationtype.sequence, generator = "idgen_x") @uiinfo(name = "identifiant") private long roleid; }
roleprofil :
public class roleprofil extends abstractdomain<long> { @id @column @uiinfo(name = "identifiant") private long roleprofilid; @manytoone(fetch = fetchtype.lazy) @joincolumn(nullable = false) @uiinfo(name = "profil") private profil profilid; @manytoone(fetch = fetchtype.lazy) @joincolumn(nullable = false) @uiinfo(name = "rôle") private role roleid; }
what want create function profils
role
id, using jpa.
this started, i'm new jpa , couldn't figure out how :
public list<profil> findprofilsbyroleid(long roleid) { final criteriabuilder builder = getcriteriabuilder(); final criteriaquery<profil> criteriaquery = builder.createquery(profil.class); final root<profil> = criteriaquery.from(profil.class); //todo typedquery<profil> query = getentitymanager().createquery(criteriaquery); return query.getresultlist(); }
how can ?
select pf.* profil pf inner join roleprofil rpf on rpf.profilid = pf.profilid rpf.roleid = {roleid}
might more easy implement query. think code in java (i have not tested proposed solution):
public list<profil> findprofilsbyroleid(long roleid) { final criteriabuilder builder = getcriteriabuilder(); final criteriaquery<profil> query = builder.createquery(profil.class); final root<roleprofil> = query.from(roleprofil.class); query.select(from.get(roleprofil_.profilid)); query.where(builder.equal(from.get(roleprofil_.roleid).get(role_.roleid), roleid)); final typedquery<profil> typedquery = getentitymanager().createquery(query); return typedquery.getresultlist(); }
Comments
Post a Comment