java - JPA Criteria API - Setup Joins and Fetch data from Joined Entity -
i have 2 entities foo
, fooderiveddata
, described below.
entity foo:
@entity @table(name="foo") public class foo { // skipping property declarations @onetoone @primarykeyjoincolumn(name = "foo_id") @fetch(fetchmode.join) public getfooderiveddata() { return fooderiveddata; }
entity fooderiveddata
@entity @table(name = "foo_derived_vw") @immutable public class fooderiveddata { @id @column(name = "foo_id") long fooid; @column(name = "foo_count") private integer foocount; @column(name = "foo_sum") private bigdecimal foosum; // getters left out here
i working jpa criteria api (hibernate jpa implementation) , need instance of fooderiveddata
loaded in foo
. fetched eagerly default, want fetched using inner join, not separate select statement every instance of fooderiveddata
. able add inner join against fooderiveddata
.
criteriaquery<foo> criteriaquery = criteriabuilder.createquery(foo.class); root<foo> root = criteriaquery.from(foo.class); root.join("fooderiveddata"); predicate p = getpredicate(); // details omitted here criteriaquery.select(root).where(p);
although can see sql in logs inner join added foo_derived_vw, none of columns selected. every instance of foo
, still need have separate select statement data have readily available.
currently query this:
select f.foo_id, f.foo_bar from foo f inner join foo_derived_vw d on d.foo_id = f.foo_id; select d.foo_id, d.foo_count, d.foo_sum foo_derived_vw d foo_id = ?; select d.foo_id, d.foo_count, d.foo_sum foo_derived_vw d foo_id = ?;
note not selecting data joined foo_derived_vw , data being fulfilled additional database hits. want query this:
select f.foo_id, f.foo_bar, d.foo_count, d.foo_sum from foo f inner join foo_derived_vw d on d.foo_id = f.foo_id;
how select properties joined entity in criteria api? can instruct javax.persistence.criteria.root select attributes joining?
i have attempted changing join fetch, or fetch specified join type. in either case, it's failing hibernate queryexception.
root<foo> root = criteriaquery.from(foo.class); root.fetch("fooderiveddata");
caused by: org.hibernate.queryexception: query specified join fetching, owner of fetched association not present in select list
i believe don't need specify fetchmode
getfooderiveddata()
default fetch type onetoone relationship eager
. hope helps!
Comments
Post a Comment