java - Selecting only a subset of columns in linked entity in HQL -
please don't ask me why need this, if think find way solve specific problem, want understand hql , limits more.
given following entities
@entity class child {      private string someattribute1;     .....     private string someattributen;      @manytoone(eager)     private parent parent;  }  class parent {      private string someparent1;     ....     private string someparentn; } if select child hibernate automatically fetches columns child , parent in single joined sql, , typical desired case.
sometimes know that, entities mapped large number of columns, need subset.
if select item.someattribute1 someattribute1, item.someattribute2 someattribute2, item.someattribute3 someattribute3 child item etc. tied resulttransformer can let hibernate return me 3 columns sql, or more columns if list them. ok, cool , works charm.
however if need fetch only, say, 3 columns child , 2 parent, while rest can null, , materialize child entity relationship, i cannot write following
select item.someattribute1 someattribute1, item.someattribute2 someattribute2, item.someattribute3 someattribute3, item.parent.someparent1 parent.someparent1, item.parent.someparent2 parent.someparent2 child item left join item.parent the above not work because hibernate not allow alias composed. disallows me use as parent.somename clause because aliases should flat.
just tell counter example, in languages such linq problem not apply
from child c in children select new child {     someattribute1 = c.someattribute1,     someattribute2 = c.someattribute2,      parent = new parent {         attribute1 = c.parent.attribute1,         .......     } } with above statement, entity framework fetch desired columns.
i don't want make comparison or criticism between hibernate java , entity framework c#, absolutely.
i have need fetch subset of columns compose entity @manytoone relationship, in order optimize memory , bandwidth usage. columns child entity , parent.
i want know if , how possible in hibernate achieve that. populate parent attribute in result set object of class parent populated subset of columns (the rest being null no problem). using resulttransformers happily
there 2 problems it.
- hibernate doesn't allow use nested aliases - as parent.somenamein hql. produces parsing error. can use nested aliases- criteriausing- projections.property("parent.somename").
- hibernate doesn't have result transformer populate result objects using nested aliases. 
you can use criteria requests custom result transformer described here
Comments
Post a Comment