java - Select elements from joined ElementCollection -
problem description
suppose have data model looks (simplified keep short):
@entity class unit { ... } @embeddable class numberwithunit { double value; @manytoone ( fetch = fetchtype.lazy, optional = false ) @joincolumn(name = "unitid") unit unit; } @entity class product { @id int id; @elementcollection @collectiontable( name = "sisu" ) list<numberwithunit> sizeinseveralunits; }
that model might not make sense shows structure need work with.
now we'd issue query this:
select p.id, s product p join p.sizeinseveralunits s
the problem, however, hibernate 4.3.11 doesn't produce correct sql statement query , don't know (yet) how fix that.
the sql produced looks
select product_.id, sisu_.id product product_ join sisu sisu_ on product_.id = sisu_.productid
that query fails of course, since there no column sisu_.id
. instead hibernate should solve select sisu_.value, sisu_.unitid
. in fact, if we'd use single numberwithunit
, query hibernate correctly resolve , generate correct sql - if we'd join (it's unnecessary tested anyways).
question
how can individual elements of elementcollection selected in hql/jpql?
what we've tried far
- making use of hibernate specific
elements()
function, i.e. rewrite queryselect p.id, elements(p.sizeinseveralunits) product p
. works due other reasons we'd use last resort (reasons being we're building framework generates queries , should able create join whenever sees collection in entity etc.). - we've debugged hqlsqlwalker etc. find out when selecting "one-to-one"
numberwithunit
ast node typecomponenttype
correct. when using join select elements joined collection type getsbagtype
correct collection. after hibernate seems lost , assumes want join entity , tries load id. - we guessed (but assumed guessing wrong) might
unit
relation insidenumberwithunit
causing problem changed simple scalar value test. expected didn't change thing.
what we've though not tried yet
- providing custom user type knows how handle
numberwithunit
collections of it. if there no standard/better way of solving our problem , works we'd go this. - testing simpler types, e.g.
list<string>
. - upgrading hibernate 5. since ast handling redesigned in hibernate 5 might working in version. upgrading means quite effort , risk break other things in our not-so-small system we'd later when have more resources available.
Comments
Post a Comment