java - Using same table twice in JPA Criteria -
i have sql query:
select lf.load_factor, ls.load_incidences, avg(lsa.accumulated_load_incidences) load_factor lf inner join load_spectrum ls on (lf.load_factor_id = ls.load_factor_id) inner join load_spectrum_aircraft_history lsa on (lf.load_factor_id = lsa.load_factor_id) inner join flight f on (lsa.flight_id = f.flight_id) inner join aircraft on (f.aircraft_id = a.aircraft_id) a.aircraft_id = x group (lf.load_factor_id) having count(lf.load_factor_id) > 1;
on clause "x" variable. so, x can assume number.
i tried same query using jpa criteria.
so wrote code:
// criteria builder criteriabuilder cb = entitymanager.getcriteriabuilder(); criteriaquery<object[]> query = cb.createquery(object[].class); // root root<loadspectrumaircrafthistory> loadspectrumaircraftroot = query.from(loadspectrumaircrafthistory.class); root<loadspectrum> loadspectrumroot = query.from(loadspectrum.class); // join declarations join<loadspectrumaircrafthistory, loadfactor> loadfactor = loadspectrumaircraftroot.join("loadfactor"); join<loadspectrum, loadfactor> loadspectrum = loadspectrumroot.join("loadfactor"); join<loadspectrumaircrafthistory, flight> flight = loadspectrumaircraftroot.join("flight"); join<flight, aircraft> aircraft = flight.join("aircraft"); // expressions select clause expression<double> loadfactorexp = loadfactor.get("loadfactor"); expression<double> loadincidences = loadspectrumroot.get("loadincidences"); expression<double> accumulatedloadincidences = loadspectrumaircraftroot.get("accumulatedloadincidences"); expression<double> avg = cb.avg(accumulatedloadincidences); // predicates clause predicate whereclause = cb.equal(aircraft.get("id"), aircraftid); // predicates having clause predicate havingclause = cb.gt(cb.count(loadspectrumaircraftroot), 1); query.multiselect(loadfactorexp, loadincidences, avg) .where(whereclause) .groupby(loadfactor.get("loadfactorid")) .having(havingclause); typedquery<object[]> qry = entitymanager.createquery(query);
but, unfortunately, code not generate sql query expected. below there sql query generated jpa code:
select loadfactor2_.load_factor col_0_0_, loadspectr1_.load_incidences col_1_0_, avg(loadspectr0_.accumulated_load_incidences) col_2_0_ load_spectrum_aircraft_history loadspectr0_ inner join load_factor loadfactor2_ on loadspectr0_.load_factor_id=loadfactor2_.load_factor_id inner join flight flight3_ on loadspectr0_.flight_id=flight3_.flight_id inner join aircraft aircraft4_ on flight3_.aircraft_id=aircraft4_.aircraft_id cross join load_spectrum loadspectr1_ inner join load_factor loadfactor5_ on loadspectr1_.load_factor_id=loadfactor5_.load_factor_id aircraft4_.aircraft_id=1 group loadfactor2_.load_factor_id having count(*)>1
as can see, join between loadspectrum , loadfactor related new load factor (loadfactor5_), not same load factor used in join between loadspectrumaircrafthistory , loadfactor (loadfactor2_).
they different tables, factor id not same these joins.
how can use same table (loadfactor2_)??
*excuse english
Comments
Post a Comment