java - Retrieving object children while using Transformers.aliasToBean -
i've got question regarding children retrieval when using transformers.aliastobean function in hibernate. i've wrote query populates object, when try populate children query doesn't work. i'll post sample code first , add generated sql , exception below.
parent
public class parent { @id @column(name="id") @generatedvalue private long id; @column(name="name") private string name; @onetomany(cascade = cascadetype.all) @joincolumn(name="parent_id") @lazycollection(lazycollectionoption.false) private list<child> children; @transient private long somecount; // public constructor + getters , setters }
child
public class child { @id @column(name="id") @generatedvalue private long id; // public constructor + getters , setters }
query
query query = session.createquery("select p.id id, p.name name, " + " (select count(*) stat parent_id=p.id) somecount, " + " " + parent.class.getsimplename() + " p ") .setresulttransformer(transformers.aliastobean(parent.class));
basically when try include p.children children
right in first line of query following sql generated hibernate:
select parent0_.id col_0_0_, parent0_.name col_1_0_, . col_2_0_, (select count(*) stats stat3_ parent_id=parent0_.id) col_3_0_, child2_.id id1_2_1_ inner join child child2_ on parent0_.id=child2_.parent_id
and error is:
warn: sql error: 0, sqlstate: 42601 error: error: syntax error @ or near "." @ position 60
that position 60 corresponds , . col_2_0_,
position before dot in first line of query. seems retrieve children, generated sql prevents executing query.
any appreciated!
simply speaking, can not add p.children
result columns of select
, because it's collection, , entities or scalars allowed here. can try alternative:
query query = session.createquery("select p, " + " (select count(*) stat parent_id=p.id) " + " " + parent.class.getsimplename() + " p " + + " left join fetch p.children "); (object[] row : (list<object[]>)query.list()) { parent p = (parent)row[0]; p.setsomecount(((number)row[1]).longvalue()); }
please tell if works you.
Comments
Post a Comment