java - selecting a list from a object in JPA -
java - selecting a list from a object in JPA -
i have object contains list on of fields, object mapped db. trying create query select few fields object , fore mentioned list.
the object mapping:
@entity @table(name="buys") @primarykeyjoincolumn(name="buy_id") public class purchase extends domainobject implements serializable { @column(name = "buy_name") private string buyname; @onetomany( cascade = {cascadetype.persist, cascadetype.merge}) @joincolumn(name="buy_id", referencedcolumnname = "buy_id") private list<insersionorder> insertionorders; //other fields omitted }
the query using:
"select new com.dtos.domainobjects.buydto(b.id, b.buyname, b.insertionorders) purchase b b.buygroupid = :groupid , b.isdeleted = false"
when running query hibernate (my jpa vendor) generates faulty query:
hibernate: select buy0_.buy_id col_0_0_, buy0_.buy_name col_1_0_, . col_2_0_ buys buy0_ inner bring together domain_objects buy0_1_ on buy0_.buy_id=buy0_1_.id inner bring together insertion_orders insertiono1_ on buy0_.buy_id=insertiono1_.buy_id inner bring together domain_objects insertiono1_1_ on insertiono1_.io_id=insertiono1_1_.id buy0_.buy_group_id=? , buy0_1_.is_deleted=0
the insertionorder field inherits domainobject.
if omit list , select "simple" fields purchase object(i.e. id,name) query works fine.
what missing?
thanks.
this syntax creates buydto instance each of rows of resultset. if worked, not homecoming want return. collection field may not part of select clause of hql query.
first solution: don't homecoming dto, homecoming entity itself.
second solution: select entities, , build dto instances yourself:
select b purchase b left bring together fetch b.insertionorders b.buygroupid = :groupid , b.isdeleted = false (buy b : list) { dtolist.add(new buydto(b.getid(), b.getbuyname(), b.getinsertionorders()); }
third solution: select want, , assemble dtos yourself:
select b.id, b.buyname, purchase b left bring together b.insertionorders b.buygroupid = :groupid , b.isdeleted = false
in each row of result, find 1 id, 1 buyname, , 1 insertionorder. you'll have create 1 dto first time meet new id, , add together order constructed dto thenext times meet id.
java hibernate jpa orm
Comments
Post a Comment