I try to use different combinations of HQL and Criteria , and I could not avoid unnecessary connections (in both cases) and some unnecessary ones are selected (in criteria).
In our scenario, we have the @ManyToMany connection between the Segment and Applications objects (navigation is carried out from segment to applications).
First I tried this Criteria :
Application app = ...
List<Segment> segments = session.createCriteria(Segment.class)
.createCriteria(Segment.APPLICATIONS)
.add(Restrictions.idEq(app.getId()))
.list();
What type of SQL does this SQL execute:
select
this_.id as id1_1_,
this_.description as descript2_1_1_,
this_.name as name1_1_,
applicatio3_.segment_id as segment1_1_,
applicatio1_.id as app2_, <==== unnecessary APPLICATIONS columns
applicatio1_.id as id7_0_,
applicatio1_.name as name7_0_,
applicatio1_.accountId as accountId7_0_,
applicatio1_.applicationFlags as applicat5_7_0_,
applicatio1_.description_ as descript6_7_0_,
from
SEGMENTS this_
inner join
SEGMENTS_APPLICATIONS applicatio3_
on this_.id=applicatio3_.segment_id
inner join <==== unnecessary join
APPLICATIONS applicatio1_
on applicatio3_.app_id=applicatio1_.id
where
applicatio1_.id = ?
, , . ( ?). , , , , , SEGMENTS_APPLICATIONS ( HQL).
( , , app.getId(). , HQL)
( ), HQL "select":
Application app = ...
List<Segment> segments = session.createQuery(
"select s from Segment s join s.applications as app where app = :app")
.setParameter("app", app)
.list();
:
select
segment0_.id as id1_,
segment0_.description as descript2_1_,
segment0_.name as name1_,
from
SEGMENTS segment0_
inner join
SEGMENTS_APPLICATIONS applicatio1_
on segment0_.id=applicatio1_.segment_id
inner join <==== unnecessary join
APPLICATIONS applicatio2_
on applicatio1_.app_id=applicatio2_.id
where
applicatio2_.id=?
, HQL ( "select s" ), APPLICATIONS, , , . ?
( , HQL , app.getId(), )
"" "" HQL?
( @ManyToMany, , @OneToMany, @ManyToOne @OneToOne, fetch = LAZY).
,